简体   繁体   中英

How to retrieve records from a table based on date in postgres

i have table manager where i have columns like date , phonenumber, user.

i want to retrieve records like say i want the phonenumbers from the table whos date lies between 01-01-2014 to 31-01-2014.The operation is like everyday 50 to 100 numbers will be inserted into the table i would like to view the numbers inserted in a particular week or particular date interval.

i am using postgres ans my date column is a varchar type. i am trying to do this in a java web application and the date will be picked using a jquery datepicker.

Sample Data

Date            Number        user
01-01-2014     244548845      user1
02-01-2014     545454454      user2
05-01-2014     244540045      user1
10-01-2014     244540045      user1
20-01-2014     244540000      user2

when i select 01-01-2014 to 06-01-2014 i would like to fetch first 3 records

SET DATESTYLE = "DMY, DMY";

SELECT
  *
FROM
  xxx
WHERE
  Date::date BETWEEN '01-01-2014' AND '06-01-2014'
ORDER BY
  Date::date
LIMIT
  3;

Also, mind using the type date for storing dates, and not varchar!

While creating a table the column should be mentioned as date(have to make sure column name is NOT DATE) ex : create table abc(dataentrydate Date not null);

and for retrieving the records based on date this below query can be used

SELECT  * FROM   abc WHERE   dateentrydate BETWEEN '17-01-2014' AND '22-01-2014' ORDER BY dateentrydate;

Thanks a lot for the response and help provided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM