Sunday, March 11, 2012

Date from datetime

Hello!

This is probably easy, but i'm a noob and couldn't find the answer by searching...

I'm trying to run a search query on my database that will get all entrys for a specified date.

this is the statement i have so far

SELECT *
FROM dbo.CLM
WHERE Date = '7/10/2007'

(I will be using ASP to make the date dynamic)

Even though all of my entries are from that date, it pulls up nothing. Where am I going wrong here?HI
i am also a beginne so i may not help u so much but what i know is that:
when you say date ='7/10'2007' it i sjust givinig you the entries for that date not before or after that date.
have alook to see what is the name of that column for the date. you have to put the name of that column and if you want all the entries after that you have to put date=>''7/10/2007'
and also check the format of your date. is it 7-10-2007 or 7 oct 2007 or 10 jul 07.
i hope i be right and can help u.
voroojak|||Thanks for the advise but that's not quite what i'm looking for.

I do only want it to pull up dates from that one specified day. The column name is Date and the date entry looks like this

7/10/2007 3:51:01 PM it is a datetime value

when i run the query posted above it doesn't return anything.

not sure what i'm doing wrong here..|||

Quote:

Originally Posted by Shisou

Hello!

This is probably easy, but i'm a noob and couldn't find the answer by searching...

I'm trying to run a search query on my database that will get all entrys for a specified date.

this is the statement i have so far

SELECT *
FROM dbo.CLM
WHERE Date = '7/10/2007'

(I will be using ASP to make the date dynamic)

Even though all of my entries are from that date, it pulls up nothing. Where am I going wrong here?


In SQL when you compare a datetime field to '7/10/2007' it converts it to a datetime of 2007-10-07 00:00:00.000. Therefore your query is looking for records at exactly that date. What you need to do is:

WHERE Date => '7/10/2007' AND < DATEADD(DAY, 1, '7/10/2007')

i.e. look for records between 2007-10-07 00:00:00.000 and 2007-10-08 00:00:00.000.|||I think you can use one of these methods.

1)where date>= '7/10/2007' and date <'8/10/2007'
and also you can use
2) where date like '7/10/2007'

3) where
datepart(year, date)='2007' AND
datepart(month, date)= '10' AND
datepart(day, date)='7'

i think it should work.

No comments:

Post a Comment