json - 来自 OPENJONS() 的 TSQL 具有显式架构:十进制 (38,30) 数据类型中的空字符串
问题描述
当它是空字符串时,我无法from openjson()
转换、转换或识别数据类型。decimal(38,30)
出于某种原因,我必须将其明确声明为varchar(39)
. 为什么? 否则,我会收到错误消息,Error converting data type varchar to numeric
我将进一步解释:
我最终构建了一个 UPDATE 语句,但我已经简化了这个 SO question 中显示的示例。这个临时表对应于我真正的目标表;数据类型是准确的。
declare @tmpSchedules table (
[EffectDate] [smalldatetime] null,
[Description] [varchar](30) null,
[Calculated] [bit] not null,
[BaseSchedule] [varchar](9) null,
[Factor] [decimal](38, 30) null
)
这是我的传入数据。这是一个 JSON 字符串。请注意,因子可能是:1) 结构中完全缺失,2) 空字符串,或 3)decimal(38,30)
值。
declare @jsonRequest nvarchar(max) =
'{
"EffectDate": "2021-04-01",
"Description": "",
"Calculated": "",
"Factor": "",
"BaseSchedule": null
}'
/*
==FACTORS==
NULL (missing from structure) --->>> from openjson() can handle this
"Factor": "", --->>> from openjson() CAN NOT handle this!!!
"Factor": "0.411334674542662787386271222800", --->>> from openjson() can handle this
*/
当我将它声明为 adecimal
时,它不能是空字符串,否则我会得到这个: Error converting data type varchar to numeric
。由于它是decimal
,我想将其声明为小数。我不想将其声明为varchar
!
另外,我需要选择NULL
它是空字符串还是实际上为空:nullif(Factor, '') as Factor
无论如何,我对json_value()
and的运气更好json_query()
,所以也许我应该改变我的策略?
问题是我对 的错误期望from openjson()
,还是这是转换/转换/数据类型问题?
select
EffectDate
,Description
,Calculated
,BaseSchedule
,Factor
--,nullif(Factor, '') as Factor
from openjson(@jsonRequest, '$')
with
(
EffectDate [smalldatetime] '$.EffectDate'
,Description [varchar](30) '$.Description'
,Calculated [bit] '$.Calculated'
,BaseSchedule [varchar](9) '$.BaseSchedule'
,Factor [decimal](38, 30) '$.Factor' --Error converting data type varchar to numeric.
--,Factor [varchar](39) '$.Factor'
)
解决方案
在这里,我们使用字符串FACTOR
,然后try_convert
将其转换为小数。这样空值不会失败
例子
declare @jsonRequest nvarchar(max) =
'{
"EffectDate": "2021-04-01",
"Description": "",
"Calculated": "",
"Factor": "0.411334674542662787386271222800",
"BaseSchedule": null
}'
select
EffectDate
,Description
,Calculated
,BaseSchedule
,Factor = try_convert(decimal(38,30),Factor) --<<< Notice the try_convert()
from openjson(@jsonRequest, '$')
with
(
EffectDate [smalldatetime] '$.EffectDate'
,Description [varchar](30) '$.Description'
,Calculated [bit] '$.Calculated'
,BaseSchedule [varchar](9) '$.BaseSchedule'
,Factor [varchar](39) '$.Factor'
)
退货
EffectDate Description Calculated BaseSchedule Factor
2021-04-01 00:00:00 0 NULL 0.411334674542662787386271222800
推荐阅读
- java - 如何将字符串数组列表存储到 Firebase 中?
- android - 如何在每次运行应用程序时显示一次免责声明
- python - 如果请求包的特定附加功能,请安装依赖项的附加功能
- javascript - 如何将数据表参数传递给我的水晶报表控制器,以便在 ASP.Net MVC 中使用表的参数保存文件名?
- c# - 在 C# 中比较两个数据表的列名
- elasticsearch - centos 7无法启动elasticsearch服务
- powershell - Why is PowerShell [TYPE]::new() lower case
- java - 注入通用 ViewModel Dagger Hilt Android
- javascript - 如何使用 url-parse 包在 react js 中解析 url 中的查询?
- php - 如何使用 array_filter() 按类别对帖子进行分组?