`
free_bird816
  • 浏览: 197258 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

itpub笔记-字符串分割

SQL 
阅读更多
有一个字符串: 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省资源,因为占用的日志少。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics