PDA

View Full Version : SQL date formating


be0
11-16-07, 11:48
Hello everyone,

I am very upset with this datetime field. I would like to to the following:

where
ship_date = getdate()

I cant do this because getdate returns 2007-11-16 11:47:09.690
I only want 2007-11-16 part not the time. So no rows get returned. How do I edit this so that I only get the date in there?

Thanks for the help!!!!

timduk
11-16-07, 14:12
If ship_date is a datetime, then :

where year( ship_date ) = year( getdate() )
and month( ship_date ) = month( getdate() )
and day( ship_date ) = day( getdate() )

might do it.

It has been a while since I did any SQL, and never on SQL-Server, so maybe wrong!

Wookie
11-16-07, 15:28
Datepart should work nicely, you can use some string and concat functions to get whatever formatting you want

http://msdn2.microsoft.com/en-us/library/ms174420.aspx

Publius
11-20-07, 10:41
I would use date_format()

where
ship_date = date_format(getdate(), '%Y-%m-%d')
This should return 2007-11-16 only.

You can use date_format() to parse out any part of a datetime field in any format that you want.

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format