我正在观察一种奇怪的行为。以下与 to 的子字符串转换相关的代码可以nvarchar
正常tinyint
工作:
DECLARE @s nvarchar(50) = N'Line 1'
DECLARE @n tinyint = SUBSTRING(@s, 6, 10)
SELECT
N'Line 1' AS explicit_nvarchar,
@s AS nvarchar_variable,
SUBSTRING(@s, 6, 10) AS nvarchar_substring
, @n AS tinyint_var_converted_earlier
, CAST(SUBSTRING(@s, 6, 10) AS tinyint) AS cast_sub_var
, CAST(SUBSTRING(N'Line 1', 6, 10) AS tinyint) AS cast_nvarchar_substr
我可以在 Microsoft SQL Server Management Studio 中观察到以下输出:
.
SELECT
但是,如果注释行未注释,则尝试处理表中的代码会失败:
Select
[order].[identifier] As [order_identifier],
[plan_data].[starts_on] As [from],
[plan_data].[ends_on] As [to],
[workplace].[identifier] AS line_identifier,
SUBSTRING([workplace].[identifier], 6, 10) AS line_no_str
--, CAST(SUBSTRING([workplace].[identifier], 6, 10) AS int) AS line_no
From
...
带有注释行...
但带有未注释的行(用于转换为tinyint
or int
)...
更新:奇怪。我已将最后一行添加到 WHERE 以检查所有行是否包含'Line...
Where
[plan].[identifier] = @lPlanIdentifier And
(
[plan_data].[starts_on] Between @fromUTC And @toUTC Or
[plan_data].[ends_on] Between @fromUTC And @toUTC)
AND LEFT([workplace].[identifier], 4) = N'Line'
...突然之间,它也适用于未注释的行。我怎样才能发现导致问题的原因?
Update2:我应该完全按照Hamlet Hakobyan 的答案发布。当查看表格内部时,我可以看到: