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

4 Date-Functions

The document provides SQL commands for handling date and time, including retrieving the current date and time, extracting date parts, performing date arithmetic, and converting strings to dates. It also covers date truncation, formatting, and other useful functions like finding the last day of the month and the next occurrence of a specified weekday. Various examples demonstrate the usage of these functions in SQL queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

4 Date-Functions

The document provides SQL commands for handling date and time, including retrieving the current date and time, extracting date parts, performing date arithmetic, and converting strings to dates. It also covers date truncation, formatting, and other useful functions like finding the last day of the month and the next occurrence of a specified weekday. Various examples demonstrate the usage of these functions in SQL queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/*1.

--Current Date and Time

CURRENT_DATE - Returns the current date. */

select current_time;
select current_date;

--CURRENT_TIMESTAMP - Returns the current timestamp (date and time).

select current_timestamp;

--2. Extract Date Parts

- Extracts a specified part of the date. Example: EXTRACT(YEAR FROM CURRENT_DATE)

DATE_PART(part, date) - Extracts a specified part of a date.

select date_part(day,current_date);
select date_part(month,current_date);
select date_part(year,current_date);
select date_part(day,to_date('2025-06-15'));
select date_part(month,to_date('2025-10-26'));

select to_date('2024-12-12','yyyy-mm-dd'),date_part(day,to_date('2024-12-
12')),date_part(month,to_date('2024-12-12')),date_part(year,to_date('2024-12-12'));

--3. Date Arithmetic

--DATEADD(unit, value, date) - Adds a value to a date for the specified unit.
Example: DATEADD(DAY, 7, CURRENT_DATE)

select dateadd(day,5,'2025-06-15');
select dateadd(day,-133,'2025-10-26');
select dateadd(day,-75,'2025-01-09');
select dateadd(day,10,'2025-06-15 12:24:12');
select dateadd(month,3,'2025-06-25');
select dateadd(month,-5,'2025-02-02');
select dateadd(year,-4,'2025-01-09');
select dateadd(month,-1,current_date);

--DATEDIFF(unit, start_date, end_date) - Returns the difference between two dates


in the specified unit. Example: DATEDIFF(DAY, '2025-01-01', CURRENT_DATE)

select datediff(day,'2021-10-26',current_date);
select datediff(month,'2024-12-04','2025-01-18');

--4.Date Conversion

-- TO_DATE(string, format) - Converts a string to a date.

SELECT to_date('2025-09-26');

-- TO_TIMESTAMP(string, format) - Converts a string to a timestamp.

5. --Date Truncation

-- DATE_TRUNC(unit, date) - Truncates the date to the specified unit.


Example: DATE_TRUNC(MONTH, CURRENT_DATE).
select date_trunc(month,to_date('2025-09-26'));
select date_trunc(year,current_date);
select date_trunc(month,current_date);

--6. Formatting

--TO_CHAR(date, format) - Converts a date to a formatted string.


--Example: TO_CHAR(CURRENT_DATE, 'YYYY-MM-DD').
select TO_CHAR(CURRENT_DATE, 'YYYY-MM-DD');

--7. Other Useful Functions

LAST_DAY(date) - Returns the last day of the month for the specified date.

select last_day(current_date);
select last_day(to_date('2024-09-02'));
select last_day(to_date('2026-01-09'),year);

NEXT_DAY(date, weekday) - Returns the next occurrence of the specified weekday.

-----------------------------------------------------------------------------------
-------------------------
select current_time;
select current_date;
select current_timestamp;

alter session set timezone ='Asia/Kolkata';

select dateadd(day,5,'2025-06-15');
select dateadd(day,1,current_date);
select dateadd(day,-10,'2025-06-15');

select dateadd(month,9,'2025-03-11');
select dateadd(month,-1,current_date); -- Get the previous month current date
select dateadd(month,1,current_date); -- Get the next month current date

select dateadd(year,-4,'2025-01-09');

select dayname('2025-01-09');
select dayname(current_date);

select dayofweek(to_date('2025-01-09'));
select dayofmonth(to_date('2025-01-12'));
select dayofmonth(to_date('12-12-2024','DD-MM-YYYY'));

select dayofyear(to_date('2025-12-04'));

select dayofmonth(current_date);

--date_trunc

select date_trunc(month,to_date('2025-09-26'));

select date_trunc(year,current_date);

select date_trunc(week,current_date);
select date_trunc(day,current_date);

select date_trunc(day,current_timestamp);

select date_trunc(day,to_date('2025-02-12 12:20:54'));

select date_trunc(month,current_date);

select date_part(day,current_date);
select date_part(month,current_date);
select date_part(year,current_date);
select date_part(day,to_date('2025-06-15'));

select date_part(month,to_date('2025-10-26'));

select to_date('2024-12-12','yyyy-mm-dd'),date_part(day,to_date('2024-12-
12')),date_part(month,to_date('2024-12-12')),date_part(year,to_date('2024-12-12'));

-- months_between
select months_between(current_date,to_date('2024-09-16'));

select dateadd(month,-1,current_date); -- Get previous month preseent day

select date_trunc('month',dateadd(month,-1,current_date)); -- Get 1st date of


previous month
select months_between(current_date,date_trunc('month',dateadd(month,-
1,current_date)));
select last_day(current_date,month);

select last_day(to_date('2024-09-02'),month);
select last_day(to_date('2024-09-02'),week);
select last_day(to_date('2026-01-09'),year);

You might also like