Convert varchar dd / mm / yyyy to dd / mm / yyyy datetime

I am trying to convert a date in a varchar column in dd/mm/yyyy format to datetime dd/mm/yyyy format, so I can run date range queries in data.

So far I have the following which does not work

 CONVERT(varchar, CAST(date_started AS datetime), 103) 

I also tried

 convert(date_started as datetime, 103) 

I need to make sure the result is dd/mm/yyyy , since we are in the UK, not the mm/dd/yyyy format

+7
date datetime sql-server tsql sql-server-2008
source share
4 answers

I think you are after this:

 CONVERT(datetime, date_as_string, 103) 

Note that datetime has no format. You think about his presentation. To get datetime data in the appropriate format, you can use

 CONVERT(varchar, date_as_datetime, 103) 
+18
source share

Try this code:

 CONVERT(varchar(15), date_started, 103) 
0
source share

I think this syntax is more accurate:

 SELECT CONVERT(CHAR(10), GETDATE(), 103) 

I add SELECT and GETDATE () for instant testing :)

0
source share

If you want to return the format mm / dd / yyyy, use 101 instead of 103: CONVERT (VARCHAR (10), [MyDate], 101)

0
source share

All Articles