简体   繁体   中英

calculation of expiration dates with sql

my first time doing a date calculation on my system

使用此逻辑帮助我

as you can see here on my first query how would i say that my expdate table is equal to this current date? then if so msgbox me "your item has expired"

on my second query i wanted to set a msgbox where msgbox me three months before my expdate ?

heres what i tried to do

cn.Open()
Dim query As String
query = "Select * from tblmeds where TIMESTAMPDIFF(MONTH,`expdate`,CURRENT_TIMESTAMP())< 1"
command = New MySqlCommand(query, cn)
readers = command.ExecuteReader

Dim count As Integer
count = 0
While readers.Read
  count = count + 1
End While

cn.Close()

If count = 1 Then
  msgbox "you have a expired items"
else
  "no items are at risk"

PS:i am currently using PHPMYADMIN as my database

Assuming SQL SErver...

Subtract 3 months from the expiration date and compare that to the utc date (if multiple timezones are involved) otherwise you could just use getDate()

SELECT EXPDATE
FROM tblMeds
WHERE Dateadd(Month, -3,expDate) < = getutcdate()

If you change you query to

query = "Select count(*) as cnt from tblmeds where TIMESTAMPDIFF(MONTH,`expdate`,CURRENT_TIMESTAMP())< 1"

Then you won't need a look to count the rows, the server can do that which is faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM