junkone junkone - 6 months ago 30
JSON Question

How do I format date in TSql when I use json_value

I am using SQL Server 2016 and I am storing json. When I extract it, the datetime format does not show clean. How do I format the datetime format?

select
JSON_VALUE(trade, '$.entrytime') AS entrytime
from
dbo.Trades


Values shown:

2016-05-23T05:21:30.3068919-04:00
2016-05-24T10:49:16.337257-04:00
2016-05-24T11:05:30.8941267-04:00
2016-05-24T11:37:35.9555731-04:00


How do I format those dates in the
yyyy-mm-dd hh:mm:ss
format?

Answer

You can also use FORMAT:

select FORMAT(CAST(JSON_VALUE(trade, '$.entrytime') as datetimeoffset), 'yyyy-MM-dd hh:mm:ss') AS entrytime from dbo.Trades
Comments