zoukankan      html  css  js  c++  java
  • SQL 同一列列字符串相加。

    Create table Tab([Col1] int,[Col2] nvarchar(1))
    Insert Tab
    select 1,N'a' union all
    select 1,N'b' union all
    select 1,N'c' union all
    select 2,N'd' union all
    select 2,N'e' union all
    select 3,N'f'
    Go

    create function F_Str(@Col1 int)
    returns nvarchar(100)
    as
    begin   
    declare
    @S nvarchar(100)   
    select @S=isnull(@S,'')+Col2 from Tab where Col1=@Col1   
    return @S
    end
    go
    Select distinct Col1,Col2=dbo.F_Str(Col1) from Tab
    go

    ================================
    说明:
    目标:表的一列为字符串,要求把该列字符串连起来。
    如:
    1,aa,
    1,bb
    2,cc
    2,dd
    结果:
    1,aabb
    2,ccdd
    如果要出现aa,bb  cc,dd的结果
    那isnull(@S,'')改成isnull(@S+',','')

    isnull(@S,''): 如果@S是空,则用''代替,因为null+字符串 = null

  • 相关阅读:
    vue父子组件传值的方式
    定时任务写法
    仅仅为笔记
    consul剔除某个服务
    mybatis批量查询
    一次eureka的事故
    feign的工作原理
    JVM优化
    threadlocal应用
    秋招总结
  • 原文地址:https://www.cnblogs.com/aoyihuashao/p/1623740.html
Copyright © 2011-2022 走看看