在T-SQL中何时实际需要Ltrim和Rtrim?

在T-SQL中何时实际需要Ltrim和Rtrim?,sql,sql-server,string,union,Sql,Sql Server,String,Union,帮助我理解SQL和字符串以及空格之类的东西 使用以下数据作为样本: with sampletable as ( select ' 0123456789 ' as num --1 / 23c union all select '0123456789' as num --2 / 10c union all select ' 0123456789' as num --3 / 17 union all select ' 0123456789

帮助我理解SQL和字符串以及空格之类的东西

使用以下数据作为样本:

with sampletable as (
select '       0123456789      ' as num --1 / 23c
union all
select '0123456789' as num  --2  / 10c
union all
select '       0123456789' as num  --3 / 17
union all
select '             0123456789' as num  --4 / 23c
union all
select '0123456789             ' as num  --5 / 23c
union all
select '0123456789 ' as num  --6 / 11c
union all
select ' 0123456789' as num  --7 / 11c
) 
清晰的 这相当于使用
UNION
而不是
UNION ALL

select distinct num,len(num), count(distinct num) from sampletable group by num
结果4行[4,1,5,2]

内部联合对抗自我

select a.num, len(a.num) from sampletable a inner join sampletable b on a.num=b.num 
结果15行(7+7=?)

针对LTRIM(RTRIM(num))的内部连接

select a.num, len(a.num) from sampletable a inner join sampletable b on a.num=ltrim(rtrim(b.num))
结果21行//全部长度10

select num, len(num) from sampletable 

结果4

在SQL Server中被忽略,以便进行比较和
LEN
。所以
'0123456789'
等于
'0123456789'
'0123456789'
,etc@lamak是的,但是不知道什么时候,为什么。那么,你是说规则是1个空格的右边等于n+1个空格??,
为什么
这是因为它是在SQL Server中设计的。我不理解when
questionOff主题中的
,但仍然需要知道:
SELECT
实际上是
SELECT ALL
的缩写形式,
UNION
UNION DISTINCT
的缩写形式。如果您想了解有关尾随空格的更多信息,可以。但答案是,在SQL Server中,在比较和
LEN
时忽略了尾随空格。所以
'0123456789'
等于
'0123456789'
'0123456789'
,etc@lamak是的,但是不知道什么时候,为什么。那么,你是说规则是1个空格的右边等于n+1个空格??,
为什么
这是因为它是在SQL Server中设计的。我不理解when
questionOff主题中的
,但仍然需要知道:
SELECT
实际上是
SELECT ALL
的缩写形式,
UNION
UNION DISTINCT
的缩写形式。如果您想了解有关尾随空格的更多信息,可以。但答案是这是ANSI标准