我的列中包含使用JSON_QUERY
查询的数组值。
结果集如下所示:
My_Column
["2017-06-13T07:00:00","2017-06-13T12:00:00","2017-06-13T17:00:00"]
["2017-05-04T06:00:00","2017-05-04T16:00:00","2017-05-04T23:00:00"]
["2017-05-04T04:00:00","2017-05-04T11:00:00","2017-05-04T16:00:00"]
["2017-05-04T07:00:00","2017-05-04T12:00:00","2017-05-04T17:00:00"]
["2017-05-26T08:00:00","2017-05-26T12:00:00","2017-05-26T16:00:00","2017-05-26T20:00:00"]
如何格式化列以仅检索用逗号分隔的时间?
My_Column
00:00, 12:00, 17:00
06:00, 16:00, 23:00
04:00, 11:00, 16:00
等等
答案 0 :(得分:0)
您可以创建执行转换的用户定义函数(UDF):
if object_id('FxJsonDateConverter') is not null
drop function FxJsonDateConverter
go
create function FxJsonDateConverter(@serializedDateTime varchar(max))
RETURNS varchar(max) AS
BEGIN
declare @result varchar(max) = ''
declare @dates table(json_date time)
insert into @dates
select CONVERT(time,[value])
from string_split(replace(replace(replace(@serializedDateTime, '"',''), ']',''), '[',''),',')
select @result = @result + ', '+ left(cast(json_date as varchar(max)),5)
from @dates
return RIGHT(@result, len(@result)-2)
end
go
declare @your_table table (your_column varchar(max))
insert into @your_table
values
('["2017-06-13T07:00:00","2017-06-13T12:00:00","2017-06-13T17:00:00"]' ),
('["2017-05-04T06:00:00","2017-05-04T16:00:00","2017-05-04T23:00:00"]' ),
('["2017-05-04T04:00:00","2017-05-04T11:00:00","2017-05-04T16:00:00"]' ),
('["2017-05-04T07:00:00","2017-05-04T12:00:00","2017-05-04T17:00:00"]' ),
('["2017-05-26T08:00:00","2017-05-26T12:00:00","2017-05-26T16:00:00","2017-05-26T20:00:00"]' )
select your_column, dbo.FxJsonDateConverter(your_column) as only_time
from @your_table
结果: