我不确定为什么以下SQL无效。请建议。当我尝试设置为@ year2时查询失败。
declare @year char(2)
set @year = '14'
declare @year2 as char(2)
set @year2 = SELECT CAST(CAST('14' as int ) as int) + 1
select @year2
答案 0 :(得分:3)
@year2
定义为CHAR(2)
,您正在尝试选择一个integer
价值。您需要CAST/CONVERT
到CHAR(2)
set @year2 = (SELECT (CONVERT(CHAR(2), CAST('14' AS INT) + 1)))
答案 1 :(得分:1)
试试这个......
DECLARE @year CHAR(2)
SET @year = '14'
DECLARE @year2 as CHAR(2)
SET @year2 = CAST(CAST('14' AS INT ) AS CHAR(2)) + 1
SELECT @year2
我删除了SELECT
并将上一个AS INT
更改为AS CHAR(2)