Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL DATE

Status
Not open for further replies.

mobius1983

Technical User
Jul 7, 2002
45
GB
Hi everyone,

I am hoping someone can help me, I have a date/time field in my database (e.g 10/06/2004 13:45). I need to create a SQL statement so that I can access the information from an asp page. But I want to use a WHERE statement to only grab fields that match today's date, so I need to only get the date part of the field, and ignore the time. Does anyone know how to format this field in a SQL statement.

e.g "select * from Date_table where FORMAT(date_field, only date) = date()"

I hope that makes sence.

Thanks to anyone that can help or point me in the right direction.

Nick.
 
Simple exercise ...
In Q-Analyser

declare @date datetime
declare @date1 datetime
set @date = getdate()
set @date1 = '2004/05/26'
print @date
print @date1

print datediff("y",@date,@date1)
print datediff("M",@date,@date1)
print datediff("d",@date,@date1)

So ... the query would be something like this...

SELECT * FROM Date_table
WHERE datediff("y",date_field,getdate()) = 0 AND
datediff("m",date_field,getdate()) = 0 AND
datediff("d",date_field,getdate()) = 0

Hope this helps
[flowerface]

I was standing in the park, wondering why frisbees got bigger as they came closer... then it hit me!
 
The check for year and month isn't necessary
Code:
SELECT * FROM Date_table 
WHERE datediff("d",date_field,getdate()) = 0
-Karl
 
Learn something everyday
:)

I was standing in the park, wondering why frisbees got bigger as they came closer... then it hit me!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top