简体   繁体   中英

How to convert string format “yyyyMMdd HHmmss_fff” to date T-SQL?

I'm getting this string "20150930 092622_647" and have to save it to a datetime column but I can convert it.

I know about the CONVERT but the closest expression style I found was 112 the problem is the time not the date here.

 CONVERT(DATETIME, REPLACE( '20150930 092622_647', '_', ' '), 112)

You don't need the style unless you are converting the other way... from a datetime to a character based type.

This expression will probably get you what you need:

select convert(datetime,stuff(stuff(stuff('20150930 092622_647',16,1,'.'),14,0,':'),12,0,':'))

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