zoukankan      html  css  js  c++  java
  • SQL常用自定义函数

    --************************************将数字年月日变换成中文

    CREATE FUNCTION ymdseq
    (@ymd datetime)
    RETURNS varchar(24)
    AS
    BEGIN
    declare @x varchar(2),@y varchar(2),@yy varchar(4),@dd varchar(6),@z varchar(2),@e varchar(2),@r varchar(2),@t varchar(2)

    set @e=case when left(datepart(year,@ymd),1)='1' then ''   when left(datepart(year,@ymd),1)='2' then ''   when left(datepart(year,@ymd),1)='3' then ''   when left(datepart(year,@ymd),1)='4' then ''
                           when left(datepart(year,@ymd),1)='5' then '
    '   when left(datepart(year,@ymd),1)='6' then ''   when left(datepart(year,@ymd),1)='7' then ''   when left(datepart(year,@ymd),1)='8' then  ''
                           when left(datepart(year,@ymd),1)='9'  then '
    ' end

    set @z=case when substring(cast(datepart(year,@ymd) as varchar(4)),2,1)='1' then ''   when substring(cast(datepart(year,@ymd) as varchar(4)),2,1)='2' then ''   when substring(cast(datepart(year,@ymd) as varchar(4)),2,1)='3' then ''   when substring(cast(datepart(year,@ymd) as varchar(4)),2,1)='4' then ''
                           when substring(cast(datepart(year,@ymd) as varchar(4)),2,1)='5' then '
    '   when substring(cast(datepart(year,@ymd) as varchar(4)),2,1)='6' then ''   when substring(cast(datepart(year,@ymd) as varchar(4)),2,1)='7' then ''   when substring(cast(datepart(year,@ymd) as varchar(4)),2,1)='8' then  ''
                           when substring(cast(datepart(year,@ymd) as varchar(4)),2,1)='9'   then '
    ' when substring(cast(datepart(year,@ymd) as varchar(4)),2,1)='0' then ''  end

    set @r=case when substring(cast(datepart(year,@ymd) as varchar(4)),3,1)='1' then ''   when substring(cast(datepart(year,@ymd) as varchar(4)),3,1)='2' then ''   when substring(cast(datepart(year,@ymd) as varchar(4)),3,1)='3' then ''   when substring(cast(datepart(year,@ymd) as varchar(4)),3,1)='4' then ''
                           when substring(cast(datepart(year,@ymd) as varchar(4)),3,1)='5' then '
    '   when substring(cast(datepart(year,@ymd) as varchar(4)),3,1)='6' then ''   when substring(cast(datepart(year,@ymd) as varchar(4)),3,1)='7' then ''   when substring(cast(datepart(year,@ymd) as varchar(4)),3,1)='8' then  ''
                           when substring(cast(datepart(year,@ymd) as varchar(4)),3,1)='9'   then '
    ' when substring(cast(datepart(year,@ymd) as varchar(4)),3,1)='0' then ''   end

    set @t=case when substring(cast(datepart(year,@ymd) as varchar(4)),4,1)='1' then ''   when substring(cast(datepart(year,@ymd) as varchar(4)),4,1)='2' then ''   when substring(cast(datepart(year,@ymd) as varchar(4)),4,1)='3' then ''   when substring(cast(datepart(year,@ymd) as varchar(4)),4,1)='4' then ''
                           when substring(cast(datepart(year,@ymd) as varchar(4)),4,1)='5' then '
    '   when substring(cast(datepart(year,@ymd) as varchar(4)),4,1)='6' then ''   when substring(cast(datepart(year,@ymd) as varchar(4)),4,1)='7' then ''   when substring(cast(datepart(year,@ymd) as varchar(4)),4,1)='8' then  ''
                           when substring(cast(datepart(year,@ymd) as varchar(4)),4,1)='9'   then '
    ' when substring(cast(datepart(year,@ymd) as varchar(4)),4,1)='0' then ''   end

     

    set @x=case when datepart(mm,@ymd) <10 then '0'+cast(datepart(mm,@ymd) as varchar(2))
    else  cast(datepart(mm,@ymd) as varchar(2)) end

    set @yy=case when @x='01' then ''  when @x='02' then ''  when @x='03' then ''  when @x='04' then ''  when @x='05' then ''  when @x='06' then ''  when @x='07' then ''
                            when @x='08' then '
    '  when @x='09' then ''  when @x='10' then ''  when @x='11' then '十一'  when @x='12' then '十二' end

    set @y=case when datepart(day,@ymd) <10 then '0'+cast(datepart(day,@ymd) as varchar(2))
    else  cast(datepart(day,@ymd) as varchar(2)) end

    set @dd=case when @y='01' then ''  when @y='02' then ''  when @y='03' then ''  when @y='04' then ''  when @y='05' then ''  when @y='06' then ''  when @y='07' then ''
                            when @y='08' then '
    '  when @y='09' then ''  when @y='10' then ''  when @y='11' then '十一'  when @y='12' then '十二'   when @y='13' then '十三'   when @y='13' then '十三
                              when @y='14' then '
    十四'   when @y='15' then '十五'   when @y='16' then '十六'   when @y='17' then '十七'   when @y='18' then '十八'   when @y='19' then '十九'   when @y='20' then '二十'
                              when @y='21' then  '
    二十一'  when @y='22' then  '二十二'   when @y='23' then  '二十三'   when @y='24' then '二十四'   when @y='25' then '二十五'   when @y='26' then '二十六'   when @y='27' then '二十七'
                             when @y='28' then '
    二十八'   when @y='29' then '二十九'   when @y='30' then '三十'    when @y='31' then '三十一'
    end


    RETURN
    @e+@z+@r+@t+'
    '+@yy+''+@dd+''
    END


    --**********************************
    将日期转化为'2007-7-1' 的格式
    CREATE FUNCTION dat1
    (@ymd datetime)
    RETURNS varchar(13)
    AS
    BEGIN
    RETURN CAST(YEAR(@ymd) AS varchar) +'-'+case when month(@ymd)<10 then '0'+CAST(month(@ymd) AS varchar)
    else CAST(month(@ymd) AS varchar) end
     + '-' +case when DAY(@ymd)<10 then '0'+CAST(DAY(@ymd) AS varchar)
    else CAST(DAY(@ymd) AS varchar) end
    END


    --**********************************
    取每个没字的拼音第一个字母**********************************
    create function fun_getPY(@str nvarchar(4000))
    returns nvarchar(4000)
    as
    begin
    declare @word nchar(1),@PY nvarchar(4000)
    set @PY=''
    while len(@str)>0
    begin
    set @word=left(@str,1)
    --
    如果非汉字字符,返回原字符
    set @PY=@PY+(case when unicode(@word) between 19968 and 19968+20901
    then (select top 1 PY from (
    select 'A' as PY,N'
    ' as word
    union all select 'B',N'
    簿'
    union all select 'C',N'
    '
    union all select 'D',N'
    '
    union all select 'E',N'
    '
    union all select 'F',N'
    '
    union all select 'G',N'
    '
    union all select 'H',N'
    '
    union all select 'J',N'
    '
    union all select 'K',N'
    '
    union all select 'L',N'
    '
    union all select 'M',N'
    '
    union all select 'N',N'
    '
    union all select 'O',N'
    '
    union all select 'P',N'
    '
    union all select 'Q',N'
    '
    union all select 'R',N'
    '
    union all select 'S',N'
    '
    union all select 'T',N'
    '
    union all select 'W',N'
    '
    union all select 'X',N'
    '
    union all select 'Y',N'
    '
    union all select 'Z',N'
    '
    ) T
    where word>=@word collate Chinese_PRC_CS_AS_KS_WS
    order by PY ASC) else @word end)
    set @str=right(@str,len(@str)-1)
    end
    return @PY
    end


    --**********************************
    格式为'200707'的月份减一**********************************
    CREATE FUNCTION GZ_YM
    (@GZ_YM VARCHAR(6))
    RETURNS varchar(6)
    AS
    BEGIN
    return
    case when RIGHT(@GZ_YM,2)='01' then CAST(LEFT(@GZ_YM,4)-1 AS VARCHAR(4))+'12'
    else  @GZ_YM-1 end

    END


    --**********************************
    格式为'200707'的月份加一**********************************
    CREATE FUNCTION GZ_YMj
    (@GZ_YM VARCHAR(6))
    RETURNS varchar(6)
    AS
    BEGIN
    return
    case when RIGHT(@GZ_YM,2)='12' then CAST(LEFT(@GZ_YM,4)+1 AS VARCHAR(4))+'01'
    else  @GZ_YM+1 end

    END

    --**********************************将时间转换为'08:11'**********************************
    CREATE FUNCTION tim
    (@ymd datetime)
    RETURNS varchar(13)
    AS
    BEGIN
    declare @x varchar(2)
    set @x=case when datepart(mi,@ymd) <10 then '0'+cast(datepart(mi,@ymd) as varchar(2))
    else  cast(datepart(mi,@ymd) as varchar(2)) end
    RETURN
    case when @ymd is null
    then ''
    else
    cast(datepart(hh,@ymd) as varchar(2))+':'+@x
    end
    END

    --**********************************'2007-5-1'的格式转换为'200705'**********************************
    CREATE FUNCTION ym
    (@ymd datetime)
    RETURNS varchar(13)
    AS
    BEGIN
    RETURN CAST(YEAR(@ymd) AS varchar)+case when month(@ymd)<10 then '0'+CAST(month(@ymd) AS varchar)
    else CAST(month(@ymd) AS varchar) end
     

    END


    --**********************************
    将日期为15号之前的转换为上月,15号之后的转换为下月**********************************
    CREATE FUNCTION ym15
    (@ymd datetime)
    RETURNS varchar(13)
    AS
    BEGIN
    return case when datepart(day,@ymd)<=15 then dbo.ym(@ymd) else 
                                                                 case when datepart(month,@ymd)=12  then cast(cast(datepart(year,dateadd(year,1,@ymd)) as varchar(4))+cast('01' as varchar(2))  as varchar(6))
                                                                        else cast(cast(datepart(year,@ymd) as varchar(4))+cast(dateadd(month,1,@ymd) as varchar(2)) as varchar(6)) end end
     

    END


    --**********************************
    '2007-12-10'的格式转换为'20071201'**********************************
    CREATE FUNCTION ymd
    (@ymd datetime)
    RETURNS varchar(8)
    AS
    BEGIN
    declare @x varchar(2),@y varchar(2)

    set @x=case when datepart(mm,@ymd) <10 then '0'+cast(datepart(mm,@ymd) as varchar(2))
    else  cast(datepart(mm,@ymd) as varchar(2)) end

    set @y=case when datepart(day,@ymd) <10 then '0'+cast(datepart(day,@ymd) as varchar(2))
    else  cast(datepart(day,@ymd) as varchar(2)) end


    RETURN
    cast(datepart(yyyy,@ymd) as varchar(4))+@x+@y
    END

    --**********************************'20071201' 的格式转换为'2007-12-01'**********************************
    CREATE FUNCTION ymdate
    (@ym varchar(8))

    RETURNS datetime

    AS

    BEGIN
    declare @x varchar(4),@y varchar(2),@z varchar(2),@d datetime

    set @x=left(@ym,4)
    set @y=substring(@ym,5,2)
    set @z=right(@ym,2)

    set @d=cast(@x+'-'+@y+'-'+@z as datetime)
    return
    @d
    END


    --**********************************
    当月除休息日设置 的天数**********************************
    CREATE FUNCTION ymday
    (@ym varchar(6))
    RETURNS int
    AS
    BEGIN
    declare @x int,@startd datetime,@endd datetime,@y int,@z int
    set @x=case when right(@ym,2)='01' then 31
                when right(@ym,2)='02' then
                   case when (left(@ym,4) % 4 = 0) and ((left(@ym,4) % 100 <> 0) or (left(@ym,4) % 400 = 0))
                        then 29
                   else 28
                end
                when right(@ym,2)='03' then 31
                when right(@ym,2)='04' then 30
                when right(@ym,2)='05' then 31
                when right(@ym,2)='06' then 30
                when right(@ym,2)='07' then 31
                when right(@ym,2)='08' then 31
                when right(@ym,2)='09' then 30
                when right(@ym,2)='10' then 31
                when right(@ym,2)='11' then 30
                when right(@ym,2)='12' then 31
    else 0
    end

    set @startd=cast(cast(left(@ym,4) as varchar(4))+'-'+cast(right(@ym,2) as varchar(2))+'-1' as datetime)
    set @endd=cast(cast(left(@ym,4) as varchar(4))+'-'+cast(right(@ym,2) as varchar(2))+'-'+cast(@x as varchar(2)) as datetime)

    set @y=0

    while @startd<=@endd
    begin
    set @y=@y+case when datepart(weekday,@startd) in(7,1) then 1
                    else 0 end
    set @startd=dateadd(dd,1,@startd)
    end
    set @z= @x-@y
    return
    @z
    END


    --**********************************
    '2007-12-01' 转换为'20071201'**********************************
    CREATE FUNCTION ymdse
    (@ymd datetime)
    RETURNS varchar(20)
    AS
    BEGIN
    declare @x varchar(2),@y varchar(2)

    set @x=case when datepart(mm,@ymd) <10 then '0'+cast(datepart(mm,@ymd) as varchar(2))
    else  cast(datepart(mm,@ymd) as varchar(2)) end

    set @y=case when datepart(day,@ymd) <10 then '0'+cast(datepart(day,@ymd) as varchar(2))
    else  cast(datepart(day,@ymd) as varchar(2)) end


    RETURN
    cast(datepart(yyyy,@ymd) as varchar(4))+'
    '+@x+''+@y+''
    END

    --**********************************将时间'0800' 转换为日期形式'1900-1-1 08:00:00.000'
    CREATE FUNCTION ymtime
    (@ym varchar(4))

    RETURNS datetime

    AS

    BEGIN
    declare @x varchar(2),@y varchar(2),@z varchar(2),@d datetime

    set @x=left(@ym,2)
    set @z=right(@ym,2)

    set @d=cast('1900-1-1 '+@x+':'+@z as datetime)
    return
    @d
    END

  • 相关阅读:
    2019年10月24日打印个人信息清单
    vsftp安装
    网络连接
    mysql多种方法修改密码----5.6的坑
    openstack-L版安装
    openstack是什么
    kvm快照
    文件操作
    函数
    virt-manage图形界面键盘错位问题
  • 原文地址:https://www.cnblogs.com/Hdsome/p/1913194.html
Copyright © 2011-2022 走看看