34,593
社区成员
发帖
与我相关
我的任务
分享
if object_id('temdb..#t') is not null drop table #t
create table #t(s nvarchar(100))
insert into #t
select replicate(' ',sv.number)
from master.dbo.spt_values as sv where sv.type='P' and sv.number <20
select datalength(s) as len,len(s) TrimSpaceLen,'A'+s+'B' as s from #t
len TrimSpaceLen s
1 0 0 AB
2 2 0 A B
3 4 0 A B
4 6 0 A B
5 8 0 A B
6 10 0 A B
7 12 0 A B
8 14 0 A B
9 16 0 A B
10 18 0 A B
11 20 0 A B
12 22 0 A B
13 24 0 A B
14 26 0 A B
15 28 0 A B
16 30 0 A B
17 32 0 A B
18 34 0 A B
19 36 0 A B
20 38 0 A B
if OBJECT_ID ('Tempdb..#tmp') is not null drop table #tmp
create table #tmp(
tId int identity(1,1) primary key not null,
tCol nvarchar(100) null
)
insert into #tmp (tCol)
select 'a'
union all select 'b '
union all select 'c '
union all select 'c '
select *, datalength(tCol) tCol_Len from #tmp
tId tCol tCol_Len
1 a 2
2 b 4
3 c 6
4 c 40
--测试数据
if not object_id(N'Tempdb..#A') is null
drop table #A
Go
Create table #A(col nvarchar(100))
Insert #A
select N'A ' union all
select N'A ' union all
select N'A'
GO
--测试数据结束
SELECT *
FROM #A;
if OBJECT_ID ('Tempdb..#tmp') is not null drop table #tmp
create table #tmp(
tId int identity(1,1) primary key not null,
tCol nvarchar(100) null
)
insert into #tmp (tCol)
select 'a'
union all select 'b '
union all select 'c '
union all select 'c '
select *, '['+tCol+']' as N't' from #tmp
Id tCol t
----------- ---------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------
1 a [a]
2 b [b ]
3 c [c ]
4 c [c ]
(4 行受影响)