什么时候在T-SQL中实际需要Ltrim和Rtrim?

时间:2017-02-08 15:11:31

标签: 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' 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
) 

DISTINCT 这相当于使用UNION而不是UNION ALL

select distinct num,len(num), count(distinct num) from sampletable group by num

结果 4 行[4,1,5,2]

INNER加入自我

select a.num, len(a.num) from sampletable a inner join sampletable b on a.num=b.num 

结果 15 行(7 + 7 =?)

INNER加入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

0 个答案:

没有答案