简体   繁体   English

使用 json_value 时如何在 TSql 中格式化日期

[英]How do I format date in TSql when I use json_value

I am using SQL Server 2016 and I am storing json.我正在使用 SQL Server 2016 并且我正在存储 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?如何以yyyy-mm-dd hh:mm:ss格式格式化这些日期?

您还可以使用FORMAT

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

You want to format the following timestamp to look like the one following it:您希望格式化以下时间戳,使其看起来像紧随其后的时间戳:

2016-05-24T11:37:35.9555731-04:00
2016-05-24 11:37:35

The following query replaces the T with a space, and also substrings off everything which comes after (and including) the period.以下查询将T替换为一个空格,并从句点之后(包括)中删除所有内容。

SELECT REPLACE(SUBSTRING(JSON_VALUE(trade, '$.entrytime'), 1,
                         CHARINDEX('.', JSON_VALUE(trade, '$.entrytime')) - 1),
               'T', ' ') AS entrytime
FROM dbo.Trades

SELECT CONVERT(VARCHAR(33), GetDate(), 126)选择转换(VARCHAR(33),GetDate(),126)

SELECT LEFT(CONVERT(VARCHAR(33), GetDate(), 126),19)+'Z' SELECT LEFT(CONVERT(VARCHAR(33), GetDate(), 126),19)+'Z'

could be worth a look值得一看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM