如何从SQL Server中的字符串获取准确的数字?

时间:2019-05-08 09:34:41

标签: sql json sql-server tsql sql-server-2014

如何从以下SQL Server 2014数据库的TrigParams字段中获取订单号(189,190)。

TrigParams

{"OWLSObjKey":{"key":"OWLSObjKey","value":"189","type":null},"OWLSObjType":{"key":"OWLSObjType","value":"17","type":null},"ObjKey":{"key":"ObjKey","value":"189","type":null},"ObjType":{"key":"ObjType","value":"17","type":null}}
{"OWLSObjKey":{"key":"OWLSObjKey","value":"190","type":null},"OWLSObjType":{"key":"OWLSObjType","value":"17","type":null},"ObjKey":{"key":"ObjKey","value":"190","type":null},"ObjType":{"key":"ObjType","value":"17","type":null}}

3 个答案:

答案 0 :(得分:1)

从其他答案中您会看到,具有广泛JSON支持的SQL-Server 2016+将会有很大的帮助。但是没有你就不会迷路。您可以使用字符串方法:

为MCVE授予Panagiotis Kanavos

declare @table table (trigparams nvarchar(2000))

insert into @table 
values
('{"OWLSObjKey":{"key":"OWLSObjKey","value":"189","type":null},"OWLSObjType":{"key":"OWLSObjType","value":"17","type":null},"ObjKey":{"key":"ObjKey","value":"189","type":null},"ObjType":{"key":"ObjType","value":"17","type":null}}'),
('{"OWLSObjKey":{"key":"OWLSObjKey","value":"190","type":null},"OWLSObjType":{"key":"OWLSObjType","value":"17","type":null},"ObjKey":{"key":"ObjKey","value":"190","type":null},"ObjType":{"key":"ObjType","value":"17","type":null}}')

-这是查询

select LEFT(CutOff,CHARINDEX('"',CutOff)-1)
from @table t
CROSS APPLY(SELECT STUFF(t.trigparams,1,CHARINDEX('"value":"',t.trigparams)+8,'')) A(CutOff);

简而言之:

APPLY内,我们将使用STUFF()在要查找的数字前的第一个字符上不写 (在{{1}第一次出现之后) }。这将返回为 CutOff 列。我们现在可以使用"value":"仅选择数字。

答案 1 :(得分:0)

该问题未指定服务器版本,表架构,也未指定字符串表示单个值还是两个单独行的内容。

我只是假设 这是支持JSON的SQL Server 2016,文本来自两行。我还将假设查询仅需要将数据作为单个值返回。

在这种情况下,对JSON_VALUE('$.OWLSObjKey.value')的简单调用将返回数据。 JSON_VALUE从格式良好的JSON字符串返回单个值:

declare @table table (trigparams nvarchar(2000))

insert into @table 
values
('{"OWLSObjKey":{"key":"OWLSObjKey","value":"189","type":null},"OWLSObjType":{"key":"OWLSObjType","value":"17","type":null},"ObjKey":{"key":"ObjKey","value":"189","type":null},"ObjType":{"key":"ObjType","value":"17","type":null}}'),
('{"OWLSObjKey":{"key":"OWLSObjKey","value":"190","type":null},"OWLSObjType":{"key":"OWLSObjType","value":"17","type":null},"ObjKey":{"key":"ObjKey","value":"190","type":null},"ObjType":{"key":"ObjType","value":"17","type":null}}')

select JSON_VALUE(trigparams,'$.OWLSObjKey.value') As SomeKey
from @table

这将返回:

SomeKey 
189 
190 

答案 2 :(得分:-1)

假设 ,您正在使用SQL Server 2016+,请使用OPENJSON

SELECT O.OrderNumber
FROM (VALUES('{"OWLSObjKey":{"key":"OWLSObjKey","value":"189","type":null},"OWLSObjType":{"key":"OWLSObjType","value":"17","type":null},"ObjKey":{"key":"ObjKey","value":"189","type":null},"ObjType":{"key":"ObjType","value":"17","type":null}}'),
            ('{"OWLSObjKey":{"key":"OWLSObjKey","value":"190","type":null},"OWLSObjType":{"key":"OWLSObjType","value":"17","type":null},"ObjKey":{"key":"ObjKey","value":"190","type":null},"ObjType":{"key":"ObjType","value":"17","type":null}}')) V(TrigParams)
     CROSS APPLY OPENJSON(V.TrigParams) WITH (OWLSObjKey nvarchar(MAX) AS JSON) OK
     CROSS APPLY OPENJSON(OK.OWLSObjKey) WITH (OrderNumber int '$.value') O;

如果没有,那么在不使用CLR函数的情况下,SQL Server不是您的朋友。如果您不能使用CLR,那么我建议您使用其他工具来读取JSON值。