From 49d2b56ed2d85421c0635c62f56965e8ae87ff69 Mon Sep 17 00:00:00 2001 From: Imran Imtiaz Date: Fri, 5 Dec 2025 09:11:20 +0400 Subject: [PATCH] Refactor date.sql for clarity and consistency --- date.sql | 79 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 30 deletions(-) diff --git a/date.sql b/date.sql index 5b049fb..ee5ffa8 100644 --- a/date.sql +++ b/date.sql @@ -1,30 +1,49 @@ - - --- SHOW timezone; -SELECT now(); - -create table timeZ (ts TIMESTAMP without time zone , tsz TIMESTAMP with time zone ); - -INSERT into timez VALUES('2024-01-12 10:45:00', '2024-01-12 10:45:00'); - -SELECT * from timez; - - -SELECT now(); - -SELECT CURRENT_DATE; - -SELECT now()::date; -SELECT now()::time; - -SELECT to_char(now(), 'DDD'); - -SELECT CURRENT_DATE - INTERVAL '1 year 2 month'; - -SELECT age(CURRENT_DATE, '1995-07-29'); - -SELECT *, age(CURRENT_DATE, dob) from students; - -SELECT extract(month from '2024-01-25'::date); - -SELECT 'n'::BOOLEAN; \ No newline at end of file +-- =============================== +-- SHOW CURRENT TIME & DATE +-- =============================== +SELECT now() AS current_timestamp; +SELECT CURRENT_DATE AS today_date; +SELECT now()::date AS current_date_only; +SELECT now()::time AS current_time_only; + +-- =============================== +-- CREATE TABLE WITH TIMESTAMP TYPES +-- =============================== +DROP TABLE IF EXISTS timeZ; + +CREATE TABLE timeZ ( + ts TIMESTAMP WITHOUT TIME ZONE, + tsz TIMESTAMP WITH TIME ZONE +); + +-- =============================== +-- INSERT SAMPLE DATA +-- =============================== +INSERT INTO timeZ (ts, tsz) VALUES +('2024-01-12 10:45:00', '2024-01-12 10:45:00+00'); + +-- =============================== +-- SELECT FROM TIME TABLE +-- =============================== +SELECT * FROM timeZ; + +-- =============================== +-- DATE AND TIME FUNCTIONS +-- =============================== +-- Day of year +SELECT to_char(now(), 'DDD') AS day_of_year; + +-- Subtracting interval from current date +SELECT CURRENT_DATE - INTERVAL '1 year 2 months' AS past_date; + +-- Age calculation +SELECT age(CURRENT_DATE, '1995-07-29') AS age_years; + +-- Age for students table (example) +SELECT *, age(CURRENT_DATE, dob) AS age_of_student FROM students; + +-- Extract month from date +SELECT EXTRACT(MONTH FROM '2024-01-25'::DATE) AS month_number; + +-- Boolean casting +SELECT 'n'::BOOLEAN AS boolean_value; -- 'n' will be false