Sunday, March 11, 2012

Date from a Datetime field

Hi,

a colleague need to get the date from a datetime field with a select query.

Is there an easy way to get this date? I need an excample.

Thanks

Thomas

select convert(varchar,datepart(mm,DateTimeCol)) + '/' +convert(varchar,datepart(dd,DateTimeCol)) + '/' + convert(varchar,datepart(yy,DateTimeCol)) from DataTable|||

select CONVERT(CHAR(10),GETDATE(),110)

select CONVERT(CHAR(10),mydatetimefield,110) from mytable

|||

Thomas:

Try SELECT CONVERT (VARCHAR(10), urDateTime, 101)

Check in BOL under CONVERT > CAST and CONVERT if there is an alternate format that you would rather use.

Dave

|||

Use the ISO unseparated date format style and CONVERT:

select convert(varchar, dtcol, 112)

All other formats are region or language specific so you will have issues interpreting it correctly depending on how you consume or use it.

|||

The suggestions so far would return your column as a string. If you want the data type of the returned column to be DateTime (with the time component removed) you can do this instead.

Select DateAdd(Day, DateDiff(Day, 0, dtcol), 0) As dtcol

No comments:

Post a Comment