SQL SERVER常用函数

验证身份证函数

/*
---------------------------
校验身份证号是否有效 成功返回1 失败返回0
---------------------------
*/
CREATE FUNCTION [dbo].[fn_IDCardChk] ( @IDCard VARCHAR(18) )
RETURNS BIT
AS
    BEGIN
        IF LEN(@IDCard) <> 15
            AND LEN(@IDCard) <> 18--身份证号只有15或18位
            RETURN(0);
            --如果是15位身份证 则只验证日期和是否数字格式
        IF LEN(@IDCard) = 15
            IF ISDATE('19' + SUBSTRING(@IDCard, 7, 6)) = 0
                OR ISNUMERIC(@IDCard) = 0
                RETURN(0);
            ELSE
                RETURN(1);
 
        --18位身份证 验证日期 校验位
        IF ISDATE(SUBSTRING(@IDCard, 7, 8)) = 0
            OR ISNUMERIC(SUBSTRING(@IDCard, 1, 17)) = 0--验证日期和前17位是否数字格式
            RETURN(0);
        --验证校验位开始
        DECLARE @validFactors VARCHAR(17) ,
            @validCodes VARCHAR(11) ,
            @i TINYINT ,
            @iTemp INT;
        SELECT  @validFactors = '79A584216379A5842',
                @validCodes = '10X98765432', @i = 1, @iTemp = 0;
        WHILE @i < 18
            BEGIN
                SELECT  @iTemp = @iTemp + CAST(SUBSTRING(@IDCard, @i, 1) AS INT)* ( CASE SUBSTRING(@validFactors, @i, 1) WHEN 'A' THEN 10  ELSE SUBSTRING(@validFactors, @i, 1) END )
                , @i = @i + 1;
            END;
            
        IF SUBSTRING(@validCodes, @iTemp % 11 + 1, 1) = RIGHT(@IDCard, 1)
            RETURN 1;
        ELSE
            RETURN 0;
        RETURN NULL;
    END;
调用方式:SELECT [dbo].[fn_IDCardChk]('210702198412106191')

字符串转日期函数

--格式化时间
CREATE function [dbo].[fn_GetFormatTime]
(
@date varchar(100),
@format varchar(50)
)
Returns varchar(100) 
AS
BEGIN
    declare @returns varchar(100)
    if(isnull(@date,'')='')
    begin
        set @returns=''
    end
    else
    begin
        --去除字符
        set @date=replace(replace(replace(@date,'.',''),'-',''),'/','')
        if(@format='yyyy.mm')--年月
        begin
            select @returns=(case when len(@date)>=6 then substring(@date,1,4)+'.'+substring(@date,5,2) else '' end)
        end        
        else if(@format='yyyy.mm.dd')--年月日
        begin
            select @returns=(case len(@date) when 6 then substring(@date,1,4)+'.'+substring(@date,5,2)+'.01' when 8 then substring(@date,1,4)+'.'+substring(@date,5,2)+'.'+substring(@date,7,2) else '' end)
        end
        else if(@format='yyyymmdd')--年月日
        begin
            select @returns=(case len(@date) when 6 then @date+'01' when 8 then @date else '' end)
        end
        else if(@format='yyyy-mm-dd')--转换为时间格式
        begin
            select @returns=convert(varchar(100),(case len(@date) when 6 then substring(@date,1,4)+'-'+substring(@date,5,2)+'-01' when 8 then substring(@date,1,4)+'-'+substring(@date,5,2)+'-'+substring(@date,7,2) else '' end),121)
        end
        else
        begin
            select @returns=@date
        end
    end
      
    return @returns
END
调用方式:SELECT [dbo].[fn_GetFormatTime]('201202','yyyy-MM-dd')

根据日期计算年龄函数

CREATE Function [dbo].[GetAge]
(
@birth varchar(50),
@now datetime
)
Returns int
As
Begin
    Declare @birthday datetime,@Age int, @year int, @month int, @Day int
    select @birth=replace(replace(replace(@birth,'.',''),'-',''),'/','')
    if(len(@birth)=4)
        set @birth=@birth+'0101'
    else if(len(@birth)=6)
        set @birth=@birth+'01'
    else if(len(@birth)=8)
        set @birth=@birth

    Set @age = 0
    Set @year = 0
    Set @month = 0
    Set @day = 0
    if(isdate(@birth)=1)
    begin
        set @birthday=@birth    
        Set @year = DatePart(Year,@Now) - DatePart(Year, @BirthDay)
        Set @month = DatePart(Month,@Now) - DatePart(Month, @BirthDay)
        Set @Day = DatePart(Day,@Now) - DatePart(Day, @BirthDay)
        if( @month > 0)
            Set @Age = @Year
        if( @month < 0)
            Set @Age = @Year - 1
        if(@month = 0)
        Begin
            if( @Day >= 0)
                Set @Age = @Year
            Else
                Set @Age = @Year -1
        End
    end
    Return(@Age)
End
调用方式:SELECT [dbo].[GetAge]('19940201',getdate())
原文地址:https://www.cnblogs.com/studydp/p/10108266.html