标签:
开发中经常遇到把多个id拼接在一起符号隔开传入数据库,那拆分呢就是个大事
SELECT nPushID INTO #temp FROM Table1 WHERE CHARINDEX( ‘,‘ + CONVERT(VARCHAR(10), nCoulmn) + ‘,‘, ‘,‘ + ‘3305,3311,3314‘ + ‘,‘) > 0 SELECT * FROM #temp
这里的列针对的是int 型,所以要转换。
针对订单那种订单主从表,从表多个的名称拼接在一起的
--实例1 分割符在前 select isnull ((select ‘@‘+ CommodityName from OrderInfo where OrderInfo.orderid =OrderMain.OrderID for xml path(‘‘)),‘‘) from OrderMain where orderid =‘20120302121548‘ --针对分隔符在前,可以用stuff很方便 select isnull (stuff((select ‘@‘+ CommodityName from OrderInfo where OrderInfo.orderid =OrderMain.OrderID for xml path(‘‘)),1,1,‘‘),‘‘) from OrderMain where orderid =‘20120302121548‘ --实例2 同表,分隔符在后 SELECT distinct vcName,dtCreateTime, ISNULL ((select CONVERT(VARCHAR(5),nCode)+‘,‘ from Table1 a where a.vcName =b.vcName AND a.dtCreateTime=b.dtCreateTime for xml path(‘‘)),‘‘)AS vcCode FROM Table1 b --分隔符在后,对于拼接名称的,个人觉得用substring、left还麻烦点,不如在后台或者前台去处理
标签:
原文地址:http://www.cnblogs.com/ghelement/p/5630874.html