请写一个触发器,当登入身份证号码之后,自动计算出生日期,规则15位身份证是从第6位之后的6位,18位身份证是从第6位到第8位。定义一个函数能计算出指定月份有多少天,如2011002有28,201003有31天,200907月31天

解决方案 »

  1.   


    go
    create trigger tri_tracy on tablename
    for insert
    as
    declare @id varchar(18)
    select @id=cardid from inserted
    if len(@id)=15
    begin
    print substring(@id,6,8)
    end
    else
    begin
    print substring(@id,7,13)
    end
      

  2.   


    go
    if OBJECT_ID('fun_tracy')is not null
    drop function fun_tracy
    go
    create function fun_tracy(@date date)
    returns varchar
    as
    begin
    declare @str varchar(10)
    declare @year int,@month int
    select @year=year(@date),@month=month(@date)
    if @year%4=0
    begin
    if (select charindex(ltrim(@month)+',','1,3,5,7,8.10.12'))>0
    begin
    set @str='31天'
    end
    if @month=2
    begin
    set @str='29天'
    end
    else
    begin 
    set @str='30天'
    end
    end
    else
    if (select charindex(ltrim(@month)+',','1,3,5,7,8.10.12'))>0
    begin
    set @str='31天'
    end
    if @month=2
    begin
    set @str='28天'
    end
    else
    begin 
    set @str='30天'
    end
    return @str
    end
      

  3.   

    Public Function tianshu(ByVal a As String) As Integer
            Dim iyear, imonth, iday As Integer
            iyear = Year(a)
            imonth = Month(a)
            If (iyear Mod 4 = 0) And (iyear Mod 100 = 0) Then            iday = 1
            ElseIf (iyear Mod 4 = 0) And (iyear Mod 100 <> 0) Then
                iday = 1
            Else
                iday = 0
            End If
            Select Case imonth
                Case 1, 3, 5, 7, 8, 10, 12
                    Return 31
                Case 4, 6, 9, 11
                    Return 30
                Case 2
                    If iday = 1 Then
                        Return 29
                    Else
                        Return 28
                    End If
            End Select
        End Function