SQL PRACTICE: SQL SERVER - DATEADD Function

 Q12. Find the join date of employees  if they  join after 2 weeks of the hire date?


N.B: I am using the following:

Database: MSSQL

SQL Dialect: T-SQL 


Consider below table (Sql_data) as a source data.

select * from Sql_data;

Table 1. Source Data


-- Find the join date of employees
select      empid, 
FirstName, 
LastName,
DOB,
Hire_date,
DATEADD(WK, 2, Hire_date) as Join_date,
Salary
from Sql_data;

Table 2. Join Date

ExplanationDATEADD function adds a number to a datepart and returns a modified  value based on a   specified datepart (Year, Month, Week, Day …..etc...). 

Syntax:
DATEADD (datepart, NumberToAdd, Date)

Parameters:
datepart - It is part of the date to which the dateadd () function will add a number. It can be Year(YY, YYYY), Quarter(QQ, Q), month(MM,M), day(DD, D), …..etc....  In our case, we are going to use Week (WK)
NumberToAddIt is an integer  that is added to the specified datepart. In this particular example, we will add 2 weeks to the hire_date.
Date -  It is a date value (or an expression) that will resolve a datetime value. In this case, we are going to use the Hire_date column.

DATEADD(WK, 2, Hire_date) as Join_date


Comments

Popular posts from this blog

SQL PRACTICE: SQL SERVER - Unique Records

SQL PRACTICE: SQL SERVER: DATEDIFF Function