27,580
社区成员
发帖
与我相关
我的任务
分享
/*
-- Author:Flystone
-- Date:2008-05-15
-- Version:V1.001
*/
-- Test Data: Ta
If object_id('Ta') is not null
Drop table Ta
Go
Create table Ta(a varchar(20))
Go
Insert into Ta
select 'ASDFD,34.342' union all
select 'AED,453.2' union all
select 'SFSFSD,234,45' union all
select 'sdff,' union all
select 'sdfsdf,' union all
select 'sdf,'
Go
--Start
Select left(A,case when charindex(',',A)= 0 then len(a) else charindex(',',A)-1 end ) as A,
stuff(A,1,charindex(',',A),'') as B
from ta
--Result:
/*
A B
-------------------- -----------------------
ASDFD 34.342
AED 453.2
SFSFSD 234,45
sdff
sdfsdf
sdf
(所影响的行数为 6 行)
*/
--End
-->--> (Roy_88)测试数据#A表
if object_id('Tempdb..#A') is not null
drop table #A
Go
Create table #A(A nvarchar(100))
Go
insert #A select 'ASDFD,34.342 ' union all
select 'AED,453.2 ' union all
select 'SFSFSD,234,45 ' union all
select 'sdff, ' union all
select 'sdfsdf, ' union all
select 'sdf,'
select
case when stuff(A,1,patindex('%,%',A),'')>''
then left(A,patindex('%,%',A)-1)
else left(A,patindex('%,%',A)) end as A,
stuff(A,1,patindex('%,%',A),'') as B
from
#A
/*
A B
---------------------------------------------------------------------------------------------------- -------
ASDFD 34.342
AED 453.2
SFSFSD 234,45
sdff,
sdfsdf,
sdf,
(所影响的行数为 6 行)
*/
/*
-- Author:Flystone
-- Date:2008-05-15
-- Version:V1.001
*/
-- Test Data: Ta
If object_id('Ta') is not null
Drop table Ta
Go
Create table Ta(a varchar(20))
Go
Insert into Ta
select 'ASDFD,34.342' union all
select 'AED,453.2' union all
select 'SFSFSD,234,45' union all
select 'sdff,' union all
select 'sdfsdf,' union all
select 'sdf,'
Go
--Start
Select left(A,case when charindex(',',A)= 0 then len(a) else charindex(',',A)-1 end ) as A,
rtrim(stuff(A,1,case when charindex(',',A)= 0 then len(a) else charindex(',',A) end,'')) as B
from ta
--Result:
/*
A B
-------------------- ---------------
ASDFD 34.342
AED 453.2
SFSFSD 234,45
sdff
sdfsdf
sdf
(所影响的行数为 6 行)
*/
--End
/*
-- Author:Flystone
-- Date:2008-05-15
-- Version:V1.001
*/
-- Test Data: Ta
If object_id('Ta') is not null
Drop table Ta
Go
Create table Ta(a varchar(20))
Go
Insert into Ta
select 'ASDFD,34.342' union all
select 'AED,453.2' union all
select 'SFSFSD,234,45' union all
select 'sdff' union all
select 'sdfsdf' union all
select 'sdf'
Go
--Start
Select left(A,case when charindex(',',A)= 0 then len(a) else charindex(',',A)-1 end ) as A,
stuff(A,1,case when charindex(',',A)= 0 then len(a) else charindex(',',A) end,'') as B
from ta
--Result:
/*
A B
-------------------- ---------
ASDFD 34.342
AED 453.2
SFSFSD 234,45
sdff
sdfsdf
sdf
(所影响的行数为 6 行)
*/
--End
select
left(A,patindex(',',A)-1) as A,
stuff(A,1,patindex(',',A),'') as B
from
T
select
left(A,patindex(',',A)-1) as A,
stuff(A,1,patindex(',',A) as B
from
T
--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (A varchar(13))
insert into #T
select 'ASDFD,34.342' union all
select 'AED,453.2' union all
select 'SFSFSD,234,45'
select A=left(A,charindex(',',A)-1), B=right(A,len(A)-charindex(',',A)) from #T
/*
A B
------------- -------------
ASDFD 34.342
AED 453.2
SFSFSD 234,45
*/