我在mssql2008中尝试这个:
declare @test nvarchar
set @test = '12345'
select 'true' where @test like '%3%' -- no results, condition fails
select 'true' where '12345' like '%3%' -- returns true, condition passes
有人可以向我解释为什么第一个选择语句不会返回任何结果吗?
答案 0 :(得分:6)
更改
declare @test nvarchar
到
declare @test nvarchar(10)
从nchar and nvarchar (Transact-SQL)
回答您的问题在数据定义或变量声明中未指定n时 声明,默认长度为1.当未指定n时 CAST功能,默认长度为30。
答案 1 :(得分:0)
这是因为@test nvarchar初始化为1,即它只包含你的set语句中的'1'。
如果将declare语句更改为@test nvarchar(5),它将起作用。