Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • DATEADD function in SQL

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 196
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: