zoukankan      html  css  js  c++  java
  • SqlSerVer 列与逗号分隔字符串 互相转换

    在项目中,使用SQLServer数据库,有一个需求,需要将数据库的某一列,转换成逗号分隔的字符串。同时,需要将处理完的字符串,转换成为一列。

    经过查阅资料与学习,通过以下方式可以实现如上所述需求:

    1、编写一个表值函数,传入一个字符串,实现转换成列,条件以逗号分隔(任何符号都可以自定义)

    //空格分隔的字符串    
    CREATE FUNCTION [dbo].[GetInStr]     
                 (@SourceStr varchar(2000))--源字符串    
        
    RETURNS  @table table(list  varchar(50) )      
      AS      
    BEGIN    
        
    --  select @sourcestr =  replace(@sourcestr,';',',')        
    --  select @sourcestr = replace(@sourcestr,' ',',')    
     --declare @OutStr varchar(200)      
       if charindex(',',@sourcestr)>0      
        begin    
          declare @i int    
          declare @n int    
          set @i=1    
          while charindex(',',@sourcestr,@i)>0    
            begin    
               set @n=charindex(',',@sourcestr,@i)    
               insert into @table values(substring(@sourcestr,@i, @n-@i) )    
               set @i=@n+1    
            end    
            insert into @table values(substring(@sourcestr,@i,len(@sourcestr)-@i+1))    
        end  else insert into @table values(@sourcestr)    
        
      delete from @table where isnull(list,'') = ''    
    return    
    END

    2、通过for XML Path实现将列转换成逗号连接的字符串

    SELECT STUFF((SELECT ','+字段名 FROM 表名 for xml path('')),1,1,'') 
    

      STUFF函数的意义是去掉组成字符串的尾数逗号。

  • 相关阅读:
    Sparc 10 with Simics
    Perl log 0906
    FPGA flow
    SV program与module的区别
    [基础]Verilog的$readmemx介绍
    [转载] 我对验证的一些理解
    我的书单
    boost 1_45_0 boost\cstdint.hpp 在 VS8下的 bug stdint.h 无法找到头文件
    Inside DllMain
    NoSQL数据库笔谈(转载)
  • 原文地址:https://www.cnblogs.com/yangdunqin/p/GetInStr.html
Copyright © 2011-2022 走看看