简体   繁体   中英

convert varchar to datetime dd/mm/yy to yyyy-mm-dd

this is the below format in the table with varchar as datatype need a query to convert it into datetime, cannot change the data types as there are dependencies , please provide a working query

10/19/2020 00:00:00.000000
08/17/2011 00:00:00.000000
02/18/2018 00:00:00.000000
02/27/2015 00:00:00.000000
11/22/2020 00:00:00.000000
11/14/2017 00:00:00.000000

the correct query should result in below format.

2008-07-15 00:00:00.000
2015-12-23 00:00:00.000
2008-07-15 00:00:00.000
2016-01-02 00:00:00.000

You don't seem to have a time component, so just convert to a date:

select convert(date, left(col, 10), 101)

If there is really a time component, you can use:

select convert(datetime , left(col, 10), 101) + convert(datetime, convert(time, right(col, 15)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM