473,385 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem: Retrieving Records based on the From & To Date

Hi All

I want to Retrieve Records from the table based on the DOB. All the Dateformat in mm/dd/yyyy. I am using PostGreSql 8.2

Table: Employee
-------------------------
id Bigserial
name Character Varying
DOB TimeStamp without Timezone

Normal Select Query
-------------------------------
Expand|Select|Wrap|Line Numbers
  1. select id,name,to_char(dob,'mm/dd/yyyy hh:mi:ss') as dob from employee
  2.  
Expand|Select|Wrap|Line Numbers
  1. 1;"Arun";"08/20/2007 12:30:11"
  2. 2;"Priya";"08/20/2007 02:21:41"
  3. 3;"chitra";"08/20/2007 03:52:29"
  4.  
I used this Query
Expand|Select|Wrap|Line Numbers
  1. select id,name, to_char(dob,'mm/dd/yyyy') as dob from Employee
  2. where (dob >='08/20/2006' and dob <='08/20/2007')
  3.  
By Executing this query I am not getting the Exact Records present in the Table.

can anybody suggest me how to rewrite the code. so that I can get the Exact Records.

Thanks
Kavitha
Sep 11 '07 #1
7 4670
amitpatel66
2,367 Expert 2GB
Hi All

I want to Retrieve Records from the table based on the DOB. All the Dateformat in mm/dd/yyyy. I am using PostGreSql 8.2

Table: Employee
-------------------------
id Bigserial
name Character Varying
DOB TimeStamp without Timezone

Normal Select Query
-------------------------------
Expand|Select|Wrap|Line Numbers
  1. select id,name,to_char(dob,'mm/dd/yyyy hh:mi:ss') as dob from employee
  2.  
Expand|Select|Wrap|Line Numbers
  1. 1;"Arun";"08/20/2007 12:30:11"
  2. 2;"Priya";"08/20/2007 02:21:41"
  3. 3;"chitra";"08/20/2007 03:52:29"
  4.  
I used this Query
Expand|Select|Wrap|Line Numbers
  1. select id,name, to_char(dob,'mm/dd/yyyy') as dob from Employee
  2. where (dob >='08/20/2006' and dob <='08/20/2007')
  3.  
By Executing this query I am not getting the Exact Records present in the Table.

can anybody suggest me how to rewrite the code. so that I can get the Exact Records.

Thanks
Kavitha
Please try below queries:

Expand|Select|Wrap|Line Numbers
  1. select id,name, to_char(dob,'mm/dd/yyyy') as dob from Employee
  2. where dob BETWEEN 20-AUG-2006' AND '20-AUG-2007'
  3.  
Expand|Select|Wrap|Line Numbers
  1. select id,name, to_char(dob,'mm/dd/yyyy') as dob from Employee
  2. where (trunc(dob) >='08/20/2006' and trunc(dob) <='08/20/2007')
  3.  
Sep 12 '07 #2
Please try below queries:

Expand|Select|Wrap|Line Numbers
  1. select id,name, to_char(dob,'mm/dd/yyyy') as dob from Employee
  2. where dob BETWEEN '20-AUG-2006' AND '20-AUG-2007'
  3.  
Expand|Select|Wrap|Line Numbers
  1. select id,name, to_char(dob,'mm/dd/yyyy') as dob from Employee
  2. where (trunc(dob) >='08/20/2006' and trunc(dob) <='08/20/2007')
  3.  

hi amitpatel
I tried both the above queries but when i use this query

Expand|Select|Wrap|Line Numbers
  1. select id,name, to_char(dob,'mm/dd/yyyy') as dob from Employee
  2. where dob BETWEEN '20-AUG-2006' AND '20-AUG-2007'
  3.  
records are not retrieving......
and for the next query

Expand|Select|Wrap|Line Numbers
  1. select id,name, to_char(dob,'mm/dd/yyyy') as dob from Employee
  2. where (trunc(dob) >='08/20/2006' and trunc(dob) <='08/20/2007')
  3.  
it is showing me the error as
Expand|Select|Wrap|Line Numbers
  1. ERROR: function trunc(timestamp without time zone) does not exist
  2. SQL state: 42883
  3. Hint: No function matches the given name and argument types. You may need to add explicit type casts.
  4. Character: 72
  5.  
Sep 12 '07 #3
amitpatel66
2,367 Expert 2GB
hi amitpatel
I tried both the above queries but when i use this query

Expand|Select|Wrap|Line Numbers
  1. select id,name, to_char(dob,'mm/dd/yyyy') as dob from Employee
  2. where dob BETWEEN '20-AUG-2006' AND '20-AUG-2007'
  3.  
records are not retrieving......
and for the next query

Expand|Select|Wrap|Line Numbers
  1. select id,name, to_char(dob,'mm/dd/yyyy') as dob from Employee
  2. where (trunc(dob) >='08/20/2006' and trunc(dob) <='08/20/2007')
  3.  
it is showing me the error as
Expand|Select|Wrap|Line Numbers
  1. ERROR: function trunc(timestamp without time zone) does not exist
  2. SQL state: 42883
  3. Hint: No function matches the given name and argument types. You may need to add explicit type casts.
  4. Character: 72
  5.  
For Second Query:

I think TRUNC function is not available in POSTGRESQL.

Coming to first Query:

Please check if you have any data between 20-AUG-2006 and 20-AUG-2007. If yes then those should be fetched else the Query will not return anything.
Sep 12 '07 #4
For Second Query:

I think TRUNC function is not available in POSTGRESQL.

Coming to first Query:

Please check if you have any data between 20-AUG-2006 and 20-AUG-2007. If yes then those should be fetched else the Query will not return anything.


hi amitpatel,
First query is working when i give 21 aug instead of 20 aug. Its ok but my need is that i want to get the result when i give either exact from date or to date.
ie., if i give between 20-aug-2006 and 21-aug-2007 i have to get the result having these two dates ie., my result retrieve rows having both 20-aug-06 as well as 21-aug-07 dates. for that what i have to do. Please try to solve this.
Then ,In second query you have used TRUNC is there any alternate for TRUNC in postgresql.
Sep 12 '07 #5
amitpatel66
2,367 Expert 2GB
hi amitpatel,
First query is working when i give 21 aug instead of 20 aug. Its ok but my need is that i want to get the result when i give either exact from date or to date.
ie., if i give between 20-aug-2006 and 21-aug-2007 i have to get the result having these two dates ie., my result retrieve rows having both 20-aug-06 as well as 21-aug-07 dates. for that what i have to do. Please try to solve this.
Then ,In second query you have used TRUNC is there any alternate for TRUNC in postgresql.
Why you need to use 21-AUG-2007 is because the data that you have in the table also contains TIME along with the date. So if the value is 20-AUG-2007 15:00:00 then this will not match with '20-AUG-2007' and you need to use the next day '21-AUG-2007' to get the values of '20-AUG-2007' as well.

Check for DATE functions in POSTGRESQL here
Sep 12 '07 #6
Why you need to use 21-AUG-2007 is because the data that you have in the table also contains TIME along with the date. So if the value is 20-AUG-2007 15:00:00 then this will not match with '20-AUG-2007' and you need to use the next day '21-AUG-2007' to get the values of '20-AUG-2007' as well.

Check for DATE functions in POSTGRESQL here

hi amitpatel
will u please give me an example of how to use date_trunc i can't understand. Please help me.

thanks
kavitha.
Sep 12 '07 #7
amitpatel66
2,367 Expert 2GB
hi amitpatel
will u please give me an example of how to use date_trunc i can't understand. Please help me.

thanks
kavitha.
Try using TO_CHAR in WHERE condition also as you have used in SELECT clause.

You can use DATE_TRUNC(hour,dob) to truncate the value till hour and then do comparison.

Eg:

WHERE (date_trunc(hour,dob) >= '08/20/2006' AND date_trunc(hour,dob) <= '08/20/2007')
Sep 12 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Nathan Bloom | last post by:
Hi, I have a tabular report with a date field. The report is group by weeks. Is there a way to retrive the date of the first record of each group and use that date in the group footer. I would...
4
by: zpq | last post by:
I have 5 records. What makes each record unique is the charge_start_date and the charge_end_date. what i need to do is this: rec1 csd is 04/01/03 and the ced is 03/31/05 charge_amt = 50,000...
4
by: Dave Boyd | last post by:
Hi, I have two very similar forms each with a subform. The main form gets a few fields from the user and passes this back to a query that the subform is bound to. The requery is done when the...
6
by: jstaggs39 | last post by:
I want to create a Dcount and an If...Then...Else statement to count the number of records in a table based on the date that is entered to run the form. The If....Else statment comes in because if...
3
by: StBond | last post by:
Hi everyone, I am new to Access and Visual Basic so things my be getting across a bit cloudy. I only started using VB for one week. I am having a little problem with the database that I am...
4
by: Marco | last post by:
Below I've included the code snippet that is giving me problems. In this code I am going through the arraylist operatorLineList and matching it up with records in the arraylist operatorList. My...
1
by: Kev | last post by:
Hello I have a form (RosterForm) based on a table - RosterRange RosterRange has 4 fields: RosterRangeID Autonumber RosterStartDate Date RosterEndDate Date (probably...
36
by: beebelbrox | last post by:
Hi, I am new VB programming in Access and I am requesting help with the following code. WIndows OS MSaccess 2003 This code is attached to an unbound form that will display a specific recordset...
9
ADezii
by: ADezii | last post by:
One question which pops up frequently here at TheScripts is: 'How do I retrieve data from a Recordset once I've created it?' One very efficient, and not that often used approach, is the GetRows()...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.