SQL Datetime Convertion

You often required to convert string to sql datetime.

I found the following list in SQLUSA best practice site. Just crossposting this for reference.

— SQL Server string to date / datetime conversion – datetime string format sql server

— MSSQL string to datetime conversion – convert char to date – convert varchar to date

— Subtract 100 from style number (format) for yy instead yyyy (or ccyy with century)

SELECT convert(datetime, ‘Oct 23 2012 11:01AM’, 100) — mon dd yyyy hh:mmAM (or PM)

SELECT convert(datetime, ‘Oct 23 2012 11:01AM’) — 2012-10-23 11:01:00.000

— Without century (yy) string date conversion – convert string to datetime function

SELECT convert(datetime, ‘Oct 23 12 11:01AM’, 0) — mon dd yy hh:mmAM (or PM)

SELECT convert(datetime, ‘Oct 23 12 11:01AM’) — 2012-10-23 11:01:00.000

Read more of this post

Advertisements