JpCrow JpCrow - 1 year ago 100
SQL Question

How to cast varchar to datetime in sql

I need to cast a string to a datetime so i can compare it later.

The varchar that i have is like this format:

29/11/2013 12:00:00 a.m.

And i need it to cast to:

2013-11-29 00:00:00.000

Im using MSSQL Server 2012

Thx for all

Answer Source

Please, have a look a CAST and CONVERT for more information.

Here are some examples:

-- converting from DD-MM-YYYY
select CONVERT(datetime, '29/11/2013 12:00:00 AM', 103)

-- converting from MM-DD-YYYY
select CONVERT(datetime, '11/29/2013 12:00:00 AM', 101)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download