Home > OS >  SQL Query to get Expiry date
SQL Query to get Expiry date

Time:06-17

I have columns Issue Date and Valid for:

  • Issue Date is the issue date of certification
  • Valid For (type INT) is the duration (number of years) of how long the certificate is valid for

I'm looking to get the exact expiry date.

Thanks

Screenshot of the table with sample data:

pic

CodePudding user response:

SQL Server has a simple function to add a number of time units to a date: DATEADD (datepart , number , date )

e.g. IF [Valid For] represents a number of days

select
     [Issue Date]
   , [Valid For]
   , dateadd(day,[Valid For],[Issue Date]) as [Valid To Date]
from yourtable

The first parameter to that function is the "datepart" i.e. the appropriate time unit (such as: second, hour, day, week, month) that the column [Valid For] represents. The second parameter is treated as an integer (any decimal value is ignored).

So, choose the appropriate "datepart" that satisfies the intended meaning of the number held in [Valid For] in the dateadd() function. Refer to this page for details of the function.

CodePudding user response:

dateadd(minute,convert(int,1440*[Valid For]),[Issue Date])

  • Related