简体   繁体   中英

Convert utc string to date SQL

I've a column sample_date in form of string as 200912301111230000000000 (UTC Time).How can I convert it from string to datetime in form of yyyymmdd using SQL select statement?

As you only want yyyymmdd, which is the first 8 chacters of your string, it is enough to simply use LEFT

I added the ST_TO_DATE so that you can see hw a conversionto a Date column could work

SELECT LEFT('200912301111230000000000',8),STR_TO_DATE(LEFT('200912301111230000000000',8),'%Y%m%d')
 LEFT('200912301111230000000000',8) | STR_TO_DATE(LEFT('200912301111230000000000',8),'%Y%m%d'):--------------------------------- |:------------------------------------------------------- 20091230 |  2009-12-30 

db<>fiddle here

So it would loke like this

SELECT LEFT(Your_Column,8) FROM Your_Table

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