T-SQL查询问题语法不正确

时间:2015-10-08 22:00:28

标签: sql-server tsql

我不确定为什么以下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

2 个答案:

答案 0 :(得分:3)

  

@year2定义为CHAR(2),您正在尝试选择一个   integer价值。您需要CAST/CONVERTCHAR(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)