MeowserM
asked on
SQL Where Date and Time
Hello:
I am working in a SQL database with a "OrderDate" field with the format 'YYYY-MM-DD 00:00:00'.
I am trying to write a SQL statement to get the records where the Order date is >= '2011-10-01'.
I am getting 0 records because I am not including the time but I have tried to include 00:00:00 and it hasn't worked.
What is the proper syntax to use for
WHERE OrderDate >= '2011-10-01'
Thank you
I am working in a SQL database with a "OrderDate" field with the format 'YYYY-MM-DD 00:00:00'.
I am trying to write a SQL statement to get the records where the Order date is >= '2011-10-01'.
I am getting 0 records because I am not including the time but I have tried to include 00:00:00 and it hasn't worked.
What is the proper syntax to use for
WHERE OrderDate >= '2011-10-01'
Thank you
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
try this....
DATEDIFF(dd,orderdate,'201 1-10-01') > 0
DATEDIFF(dd,orderdate,'201
The fact that SQL doesn't have "a nice TRUNC command like Oracle" has no bearing on whether the WHERE clause should work. SQL Server treats '2011-10-10' as though it were '2011-10-10 00:00:00.000'. Therefore, any and all dates after '2011-10-10 00:00:00.000' should be retuirned if the query uses WHERE somedatecolumn > '2011-10-10'.
MeowserM,
I agree with 8080_Diver: your query ought to have worked, so something else is afoot.
1) Please double check the data type used for that column, and make sure it is [small]datetime
2) Are you absolutely positive that you actually have data for that range?
Patrick
I agree with 8080_Diver: your query ought to have worked, so something else is afoot.
1) Please double check the data type used for that column, and make sure it is [small]datetime
2) Are you absolutely positive that you actually have data for that range?
Patrick
Your syntax is correct. The data should be returned, assuming there is data with a value greater than Oct 1, 2011. The fact that data is not being returned seems to indicate that the data in this field is not what you expect. So, let's examine the data.
1) What is the datatype of the field?
2) You note that the Orderdate field has "... the format 'YYYY-MM-DD 00:00:00'." S!QL Server does not have a date format for storage per se, so this is a bit confusing. I am assuming that this is the format returned when you simply run a query, e.g. "SELECT TOP 5 OrderDate FROM MyTable"
3) Try this query: SELECT TOP 5 * FROM <your table name here> WHERE Cast(OrderDate as datetime) > Cast('20111001' as datetime)
There is some extraneous information in this query (i.e. CASTs should not need to be explicit). Nonetheless, does this return any records or error message?
1) What is the datatype of the field?
2) You note that the Orderdate field has "... the format 'YYYY-MM-DD 00:00:00'." S!QL Server does not have a date format for storage per se, so this is a bit confusing. I am assuming that this is the format returned when you simply run a query, e.g. "SELECT TOP 5 OrderDate FROM MyTable"
3) Try this query: SELECT TOP 5 * FROM <your table name here> WHERE Cast(OrderDate as datetime) > Cast('20111001' as datetime)
There is some extraneous information in this query (i.e. CASTs should not need to be explicit). Nonetheless, does this return any records or error message?
If the author will try the query I provided, it should provide the answer to the question regarding whether or not there is data for the date range in question.
ASKER
I apologize for my neglect of this question. I got pulled off what I was doing.
I went back in and used 8080_diver's query. when I did a select on this table with the where clause it worked. So it wasn't my syntax and I did actually have data in that range.
Then I went back to my original query and I realized that I was doing a inner join on several tables and 2 of the tables had the same name for the orderdate field.
Could that be the problem? I am using d.orderdate >= '2011-10-01'
Thank you all for your comments - much appreciated
I went back in and used 8080_diver's query. when I did a select on this table with the where clause it worked. So it wasn't my syntax and I did actually have data in that range.
Then I went back to my original query and I realized that I was doing a inner join on several tables and 2 of the tables had the same name for the orderdate field.
Could that be the problem? I am using d.orderdate >= '2011-10-01'
Thank you all for your comments - much appreciated
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
>>Could that be the problem? <<
Have you considered posting your query here, so that we don't have to guess any more?
Have you considered posting your query here, so that we don't have to guess any more?
ASKER
I have 4 tables joined and I removed the tables 1 by 1 from the query and it worked when I removed the 2nd table. The join from that table did in fact result in 0 records for the record set > 2011-10-01.
Thank you all for helping me find this issue.
Thank you all for helping me find this issue.
ASKER
Thanks again.
Here are some examples for SQL Server
-- Month first
SELECT CONVERT(varchar(12),GETDAT
SELECT CONVERT(varchar(12),GETDAT
SELECT CONVERT(varchar(12),GETDAT
SELECT CONVERT(varchar(12),GETDAT
-- Year first
SELECT CONVERT(varchar(12),GETDAT
SELECT CONVERT(varchar(12),GETDAT
SELECT CONVERT(varchar(12),GETDAT
-- Day first
SELECT CONVERT(varchar(12),GETDAT
SELECT CONVERT(varchar(12),GETDAT
SELECT CONVERT(varchar(12),GETDAT
SELECT CONVERT(varchar(12),GETDAT
-- Time only
SELECT CONVERT(varchar(12),GETDAT
SELECT CONVERT(varchar(12),GETDAT
-- Date Only No Time (SQL 2008)
SELECT Cast(GetDate() AS date); -- 08/12/2011