Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • DATEDIFF() function: find difference in date and time

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 58
    Comment on it

    In SQL server if you have columns which are of Date/Time type then sometimes you need to find their difference for passing into the application when needed.

    So for doing that SQL Server provides a function called DATEDIFF, it is mainly used for finding the Date and Time difference in SQL.

    -- Daywise Difference
    
    SELECT DATEDIFF(day, GETDATE(), ExpiryDate)  FROM EMPLOYEE

     

    We can get the difference in the terms of days years minutes hours that depends on the query  arguments passed.

     

    -- Hours Difference
    
    SELECT DATEDIFF( hour, GETDATE(), ExpiryDate) FROM EMPLOYEE

     

    We can also use this function for setting the values in the SQL statement.

     

    UPDATE NUDGE
    		SET DaysTillExpire = CASE  WHEN DATEDIFF(day, GETDATE(), ExpiryDate) < 0 THEN 0
    									ELSE DATEDIFF(day, GETDATE(), ExpiryDate)
    							END				
    		,
    		HoursTillExpire = CASE  WHEN DATEDIFF( hour, GETDATE(), ExpiryDate) < 0 THEN 0
    									ELSE DATEDIFF(day, GETDATE(), ExpiryDate)
    							END				
    
    		,
    		IsExpired = CASE WHEN DATEDIFF(day, GETDATE(), ExpiryDate) < 0 AND DATEDIFF( hour, GETDATE(), ExpiryDate) < 0 THEN 1
    							ELSE 0
    							END

     

     

    SQL Server

 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: