简体   繁体   中英

I have date stored in varchar in SQL Server as '19-09-2020' and I want to convert it to '2020-09-19'

I have date stored in a varchar column in SQL Server as '19-09-2020'.

I want to convert it to '2020-09-19'.

I have tried this but it's not working:

select convert(varchar, '19-09-2020', 23)

You must specify the source format for convert:

 convert(date, '19-09-2020',105)

This results in a DATE, if you actually want a VarChar again (of course, you shouldn't):

convert(varchar(10), convert(date, '19-09-2020',105), 23)

Do you want string functions?

select concat_ws('-', right(mycol, 4), substring(mycol, 4, 2), left(mycol, 2))
from mytable

On the other hand, if you want to generate a date from that string, then:

select convert(date, mycol, 105) 
from mytable

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