SqlServer数据库函数

ALTER Function fk_qingdao_long(@pcs_no varchar(50)) Returns varchar(50)
    AS
BEGIN
        -- declare @pcs_no varchar(50)
        declare @returnValue varchar(50)
        declare @years varchar(10)
        declare @months varchar(10)
        -- set @pcs_no = 'A01X2AC002-000000-2209150001'
        set @years = SUBSTRING(@pcs_no,19,2)
        set @months = SUBSTRING(@pcs_no,21,2)
        
    set @ReturnValue = (SELECT 
            case 
             -- 青岛长轴年
                when @years = '22' then 'B'
                when @years = '23' then 'C'
                else '--'
            end
            +
            case 
             -- 青岛长轴月
                when @months = '01' then 'A'
                when @months = '02' then 'B'
                when @months = '03' then 'C'
                when @months = '04' then 'D'
                when @months = '05' then 'E'
                when @months = '06' then 'F'
                when @months = '07' then 'G'
                when @months = '08' then 'H'
                when @months = '09' then 'J'
                when @months = '10' then 'K'
                when @months = '11' then 'L'
                when @months = '12' then 'M'
                else '--'
            end
            
            + RIGHT(@pcs_no, 4)

        )
    Return @returnValue
        
        
        
END

使用时别忘了加dbo,否则报错

SELECT  dbo.fk_qingdao_long('A01X2AC002-000000-2210021001')

你可能感兴趣的:(SqlServer数据库函数)