字符串比较因 varchar 变量失败 [英] String comparison fails with varchar variable
本文介绍了字符串比较因 varchar 变量失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法理解为什么会得到以下结果:
I'm having trouble with understanding why I get the results below:
declare @myVar1 varchar = 'Friday'
declare @myVar2 varchar(10) = 'Friday'
select
case when @myVar1 = 'Friday' then 'yes' else 'no' end as test1,
case when @myVar2 = 'Friday' then 'yes' else 'no' end as test2,
case when @myVar1 = @myVar2 then 'yes' else 'no' end as test3
我得到的是:
test1: no
test2: yes
test3: no
如果声明的 varchar 没有(可选)大小,为什么字符串比较会失败?
Why does the string comparison fail if the varchar is declared without the (optional) size?
推荐答案
答案在这里:http://sqlfiddle.com/#!6/d41d8/4737
declare @myVar1 varchar = 'Friday'
declare @myVar2 varchar(10) = 'Friday'
select len(@myVar1)as len1,
len(@myVar2)as len2
结果是:
LEN1 LEN2
1 6
因此,如果您没有为 varchar
指定大小,SQL Server 将为您指定.在这种情况下 1. 您应该始终明确指定大小.
So if you don't specify a size for the varchar
, SQL Server will do it for you. In this case 1. You should always specify the size explicitly.
这篇关于字符串比较因 varchar 变量失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文