Member-only story

SQL Mastery: 50+ Functions to Manipulate and Operate on Time and Date Data Type

btd
6 min readDec 27, 2023

--

In SQL, time and date data types are used to represent and manipulate temporal values. These data types allow you to store and perform operations on time and date information.

1. CURRENT_DATE:

SELECT CURRENT_DATE AS Result;
-- Result: 2023-12-27

2. CURRENT_TIME:

SELECT CURRENT_TIME AS Result;
-- Result: 12:34:56

3. CURRENT_TIMESTAMP:

SELECT CURRENT_TIMESTAMP AS Result;
-- Result: 2023-12-27 12:34:56

4. TIMESTAMP WITH TIME ZONE:

Converts a timestamp to a specific time zone.

SELECT CURRENT_TIMESTAMP AT TIME ZONE 'UTC' AS Result;
-- Result: Current timestamp with time zone set to UTC

5. EXTRACT:

SELECT EXTRACT(YEAR FROM CURRENT_DATE) AS Result;
-- Result: 2023

6. DATE_PART:

SELECT DATE_PART('hour', CURRENT_TIMESTAMP) AS Result;
-- Result: 12

7. DATE_TRUNC:

Truncates a date or timestamp to a specified precision.

SELECT DATE_TRUNC('month', CURRENT_DATE) AS…

--

--

btd
btd

No responses yet