Skip to content
Advertisement

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?

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement