ITeye博客

itpub笔记-字符串分割

free_bird816 · 2006-08-08 08:40 · 数据库 · SQL · Markdown

有一个字符串: a,bc,d,abce,ddz
现在用T-SQL语句把这一字符串变成这种样子:
item
----------
a
bc
d
abce
ddz

(5 row(s) affected)

正解:

declare   @str   varchar ( 20 ), @strSql   varchar ( 8000 )
set   @str   =   ' a,bc,d,abce,ddz '   --  此处的字符串可以随心所欲的更改
if   object_id ( ' tempdb.dbo.#temp1 ' )  is   null
create   table  #temp1(item  varchar ( 20 ))
else
truncate   table  #temp1
SELECT   @strSql = ' insert into #temp1 values( ''' + REPLACE ( @str , ' , ' , ''' ) 
insert into #temp1 values( ''' ) + ''' ) '
print   @strSql
exec  ( @strSql )
select   *   from  #temp1

知识:
1.)  truncate table tableName   一次删除表中的所有数据,同于delete没有where,但是比delete省资源,因为占用的日志少。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。