While manipulating with the date and time data of the SQL columns we have requirements where we want to alter the date or time based on some condition or for checking validation in our application.
For doing such kind of operation in your database you need to know the DATEADD function provided by the SQL Server.
Examples:
If you want to add or subtract only hours from the date time column of your table:
SELECT DATEADD(HOUR, 1, getdate()) -- For adding one hour
SELECT DATEADD(HOUR, -1, getdate()) -- For subtrating one hour
If you want to add or subtract days from the datetime column
SELECT DATEADD(DAY, 1, IsExpired) -- For adding one day
SELECT DATEADD(DAY, -1, IsExpired) -- For subtrating one day
For adding or removing Year you can also use the same function for it.
SELECT DATEADD(YEAR, 1, IsExpired) -- For adding one year
SELECT DATEADD(YEAR, -1, IsExpired) -- For subtrating one year
You can also use this function for setting/updating the column value of date time type.
-- For updating one year
UPDATE EMPLOYEE SET ISEXPIRED=DATEADD(YEAR, 1, IsExpired) WHERE EMPID=12
0 Comment(s)