![](/img/trans.png)
[英]How can I use Computed Column Specification to calculate a variable from other tables?
[英]How can I calculate the next birthday by computed column in ssql
我想計算我的數據庫中人們的下一個生日我有一個名為 dateofbirth 的列和另一個名為生日的列我希望能夠使用 ssql 中的計算列來計算生日。
(datepart(year,getdate())&datepart(day,[DateofBirth]))
我認為我可以通過數據表務實地做到這一點,但我得到了一個錯誤。 下面是我的代碼。
Dimbirthda 作為新的 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)
請為您的命令嘗試此操作:
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"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.