0% found this document useful (0 votes)
23 views3 pages

SQL - Date - Functions

Uploaded by

Aparna Panya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
23 views3 pages

SQL - Date - Functions

Uploaded by

Aparna Panya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

/*

DATE
FUNCTIONS
*****************************************************************************************
***********
Date and Time functions are used to handle Date and Time data.
1. GETDATE(): It is used to get current date and time
2. YEAR(<date>): It is used to get four digits of the year
3. MONTH(<date>): It is used to get month in the form of numbers from 1 to 12
4. DAY(<date>) : It is used to get day nmber from 1 to 31
5. DATEADD(<interval>, <Number>, <date>): It is used to add/subtract interval to a date
and return date
interval
---------
year, yyyy, yy = Year
quarter, qq, q = Quarter
month, mm, m = month
dayofyear, dy, y = Day of the year
day, dd, d = Day
week, ww, wk = Week
weekday, dw, w = Weekday
hour, hh = hour
minute, mi, n = Minute
second, ss, s = Second
millisecond, ms = Millisecond

6. DATEDIFF(<interval>, <startdate>, <enddate>): It is used to difference between two


dates in specified interval
7. DATEPART(<interval>, <date>): It is used to get part of the date in the form numbers
8. DATENAME(<interval>, <date>): It is used to get part of the date in the form text
*/

--Write a SQL Code to get current date and time


SELECT GETDATE() AS CurrentDateAndTime

--Write a SQL Code to get privious day


SELECT GETDATE()-1 AS CurrentDateMinusOne

--Write a SQL Code to get next day


SELECT GETDATE()+1 AS CurrentDatePlueOne

--2. YEAR(<date>): It is used to get four digits of the year


--Write a SQL Statement to get current year
SELECT YEAR(GETDATE()) AS CurrentYear

--3. MONTH(<date>): It is used to get month in the form of numbers from 1 to 12


--Write a SQL Statement to get current month
SELECT MONTH(GETDATE()) AS CurrentMonthNbr

--4. DAY(<date>) : It is used to get day nmber from 1 to 31


--Write a SQL Statement to get current day
SELECT DAY(GETDATE()) AS DayNbr

SELECT GETDATE() AS CurrenDate, GETDATE()+1 AS CurrentDatePlusOne

--5. DATEADD(<interval>, <Number>, <date>): It is used to add/subtract interval to a date


and return date
--Write a SQL Statement to add one month to the current date
SELECT
GETDATE() AS CurrentDate,
DATEADD(MONTH, 1, GETDATE()) AS CurrentDatePlusOneMonth

--Write a SQL statement to subtract one month to the current date


SELECT
GETDATE() AS CurrentDate,
DATEADD(MONTH, -1, GETDATE()) AS CurrentDateMinusOneMonth

--6. DATEDIFF(<interval>, <startdate>, <enddate>): It is used to difference between two


dates in specified interval
--Write a SQL Statement to get employee experience in the form of years
SELECT
EmployeeNo,
Ename,
Hiredate,
DATEDIFF(YEAR, Hiredate, GETDATE()) AS Exp1,
DATEDIFF(MONTH, Hiredate, GETDATE()) AS Exp2,
ROUND(DATEDIFF(MONTH, Hiredate, GETDATE())/12.0,1) AS Exp2
FROM employee

--7. DATEPART(<interval>, <date>): It is used to get part of the date in the form numbers
--Write a SQL code to get year from the current date
SELECT
YEAR(GETDATE()) AS CurrentYear1,
DATEPART(YEAR, GETDATE()) AS CurrentYear2

--Write a SQL Statement to get current year, current month and current day
SELECT
YEAR(GETDATE()) AS CurrentYear1,
DATEPART(YEAR, GETDATE()) AS CurrentYear2,
MONTH(GETDATE()) AS CurrentMonth1,
DATEPART(MONTH, GETDATE()) AS CurrentMonth2,
DAY(GETDATE()) AS CurrentDay1,
DATEPART(DAY, GETDATE()) AS CurrentDay2,
DATEPART(WEEK, GETDATE()) AS CurrentWeek

--8. DATENAME(<interval>, <date>): It is used to get part of the date in the form text
--Write a SQL Statement to get current year, current month and current day, Current day
name
SELECT
DATENAME(YEAR, GETDATE()) AS CurrentYear,
DATENAME(MONTH, GETDATE()) AS CurrentMonth,
DATENAME(DAY, GETDATE()) AS CurrentDay,
DATENAME(WEEKDAY, GETDATE()) AS WeekdayName
--
SELECT * FROM employee

--Write a SQL statement to get year, quarter, month, weeknumber from Hiredate
SELECT
YEAR(Hiredate) AS Year,
DATEPART(QUARTER, Hiredate) AS Quarter,
DATENAME(MONTH, Hiredate) AS Month,
DATEPART(WEEK, Hiredate) AS WeekNbr
FROM employee

--Write a SQL statent to dispay employeeno, ename, hiredate and short month name (Jan,
Feb...)
SELECT
EmployeeNo,
Ename,
Hiredate,
LEFT(DATENAME(MONTH, Hiredate),3) AS ShortMonth
FROM employee

--Write a SQL Statement to get How many number of days are completed by every employee
SELECT
EmployeeNo,
Ename,
Hiredate,
DATEDIFF(DAY, Hiredate, GETDATE()) AS DayCompleted
FROM employee

--Write a SQL Statement to substract 10 days from Hiredate


SELECT
EmployeeNo,
Ename,
Hiredate,
DATEADD(DAY, -10, Hiredate) AS HiredateMinus10
FROM employee

--Write a SQL statement to get Weekday names in the uppercase characters.


SELECT
EmployeeNo,
Ename,
Hiredate,
UPPER(DATENAME(WEEKDAY, Hiredate)) AS WeekdayNames
FROM employee

SELECT
DATEPART(DY, GETDATE()) AS DayOfYear

SELECT
Salary,
Commission,
ISNULL(Commission,0) + Salary AS Gross_Salary1,
COALESCE(Commission,0) + Salary AS Gross_Salary2,
CASE
WHEN Commission IS NULL THEN Salary
ELSE Commission+Salary
END AS Gross_Salary3
FROM employee

You might also like