如何通过ssql中的计算列计算下一个生日

时间:2019-10-02 07:42:49

标签: sql vb.net

我想计算人们的下一个生日 在我的数据库中,我有一列称为dateofbirth,另一列称为Birthday 我希望能够使用ssql中的计算列来计算生日。

(datepart(year,getdate())&datepart(day,[DateofBirth]))

我想我可以通过datatable实用地做到这一点,但是我得到了一个错误。 下面是我的代码。

将birthdim设置为新的SqlDataAdapter(birthcmd)

    Dim birthdt As New DataTable
    birthda.Fill(birthdt)

    For Each rw As DataRow In birthdt.Rows
        Dim dob As String = rw.Item(3)



        Dim mdat As Date = FormatDateTime(dob, DateFormat.ShortDate)

        Dim bday As Date = (Date.Today.Year & mdat.Month & mdat.Day)


        Dim yers As Integer = DateDiff(DateInterval.Year, mdat.Date, Today.Date)
        Dim moths As Integer = DateDiff(DateInterval.Month, mdat.Date, Today.Date)
        Dim dys As Integer = DateDiff(DateInterval.Day, mdat.Date, Today.Date)

1 个答案:

答案 0 :(得分:0)

请为您的命令尝试以下操作:

    birthcmd.CommandText = "SELECT        DateofBirth, " & _
                      "DATEADD(year, DATEDIFF(year, DateofBirth, { fn NOW() }) + (CASE WHEN month(dateofbirth) * 100 + day(dateofbirth) < month({ fn NOW() }) * 100 + day({ fn NOW() }) THEN 1 ELSE 0 END), DateofBirth) AS Nextbday, " & _
                      "DATEDIFF(year, { fn NOW() }, DATEADD(year, DATEDIFF(year, DateofBirth, { fn NOW() }) + (CASE WHEN month(dateofbirth) * 100 + day(dateofbirth) < month({ fn NOW() }) * 100 + day({ fn NOW() }) THEN 1 ELSE 0 END), DateofBirth)) AS Diffyers, " & _
                      "DATEDIFF(month, { fn NOW() }, DATEADD(year, DATEDIFF(year, DateofBirth, { fn NOW() }) + (CASE WHEN month(dateofbirth) * 100 + day(dateofbirth) < month({ fn NOW() }) * 100 + day({ fn NOW() }) THEN 1 ELSE 0 END), DateofBirth)) AS Diffmoths, " & _
                      "DATEDIFF(day, { fn NOW() }, DATEADD(year, DATEDIFF(year, DateofBirth, { fn NOW() }) + (CASE WHEN month(dateofbirth) * 100 + day(dateofbirth) < month({ fn NOW() }) * 100 + day({ fn NOW() }) THEN 1 ELSE 0 END), DateofBirth)) AS Diffdys " & _
                      "FROM myBirthdayTable"