Question

I have the following strings in a table

SEP 21 2009 and OCT 7 2009

Is there any way to convert them to a date format in SQL ? I want to check them as date.

Was it helpful?

Solution

You can use CONVERT() or CAST()

Using CONVERT():

CONVERT ( data_type [ ( length ) ] , expression [ , style ] )

Eg:

SELECT CONVERT(DATETIME,'2014/10/15 11:27:06 PM',20)

See example of CONVERT() in SQL Fiddle

Using CAST():

CAST ( expression AS data_type [ (length)]) 

Eg:

SELECT CAST('MAR 2 1991' as date) as DateValue
SELECT CAST('2013-04-16' AS datetime) AS DateValue

See example of CAST() in SQL Fiddle.

Read more here.

OTHER TIPS

yes, you could use CONVERT(DATETIME,inputdate) AS inputdate

for more datetime formats you could visit 1

You can use CONVERT or CAST. Please check the documentation online.

declare @a varchar(20)
set @a='SEP 21 2009'

select cast(@a as date)

SEE DEMO

You can use CONVERT or CAST.
More information here: http://msdn.microsoft.com/en-us/library/ms174450.aspx

select date_col, cast(date_col as date) as dt
from date_tab

SQL Fiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top