Skip to content

The codes for the article SQL 455: Convert Date format into DD/MM/YYYY format in SQL #363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- A sample table creation
CREATE TABLE Payment ( id INT AUTO_INCREMENT PRIMARY KEY, customer_name VARCHAR(100), date_of_payment DATE );
-- Insertion of sample data
INSERT INTO Payment (customer_name, date_of_payment) VALUES ('Alice Johnson', '2025-06-01'), ('Bob Smith', '2025-06-15'), ('Charlie Lee', '2025-06-20'), ('Diana Patel', '2025-06-25');
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- A sample table creation
CREATE TABLE Payment ( id INT IDENTITY(1,1) PRIMARY KEY, customer_name VARCHAR(100), date_of_payment DATE );
-- Insertion of sample data
INSERT INTO Payment (customer_name, date_of_payment) VALUES ('Alice Johnson', '2025-06-01'), ('Bob Smith', '2025-06-15'), ('Charlie Lee', '2025-06-20'), ('Diana Patel', '2025-06-25');
-- Using CONVERT Function
SELECT id, customer_name, CONVERT(VARCHAR(100), date_of_payment, 103) AS formatted_date FROM Payment;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- A sample table creation
CREATE TABLE Payment ( id INT AUTO_INCREMENT PRIMARY KEY, customer_name VARCHAR(100), date_of_payment DATE );
-- Insertion of sample data
INSERT INTO Payment (customer_name, date_of_payment) VALUES ('Alice Johnson', '2025-06-01'), ('Bob Smith', '2025-06-15'), ('Charlie Lee', '2025-06-20'), ('Diana Patel', '2025-06-25');
-- Using DATE Function
SELECT id, customer_name, DATE_FORMAT(date_of_payment, '%d/%m/%Y') AS converted_date FROM Payment;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- A sample table creation
CREATE TABLE Payment ( id INT IDENTITY(1,1) PRIMARY KEY, customer_name VARCHAR(100), date_of_payment DATE );
-- Insertion of sample data
INSERT INTO Payment (customer_name, date_of_payment) VALUES ('Alice Johnson', '2025-06-01'), ('Bob Smith', '2025-06-15'), ('Charlie Lee', '2025-06-20'), ('Diana Patel', '2025-06-25');
-- Using FORMAT Function
SELECT id, customer_name, FORMAT(date_of_payment, 'dd/MM/yyyy') AS converted_date FROM Payment;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- A sample table creation
CREATE TABLE Payment ( id SERIAL PRIMARY KEY, customer_name VARCHAR(100), date_of_payment DATE );
-- Insertion of sample data
INSERT INTO Payment (customer_name, date_of_payment) VALUES ('Alice Johnson', '2025-06-01'), ('Bob Smith', '2025-06-15'), ('Charlie Lee', '2025-06-20'), ('Diana Patel', '2025-06-25');
-- Using LPAD and EXTRACT Function
SELECT id, customer_name, LPAD(EXTRACT(DAY FROM date_of_payment)::TEXT, 2, '0') || '/' || LPAD(EXTRACT(MONTH FROM date_of_payment)::TEXT, 2, '0') || '/' || EXTRACT(YEAR FROM date_of_payment)::TEXT AS converted_date FROM Payment
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- A sample table creation
CREATE TABLE Payment ( id INT AUTO_INCREMENT PRIMARY KEY, customer_name VARCHAR(100), date_of_payment DATE );
-- Insertion of sample data
INSERT INTO Payment (customer_name, date_of_payment) VALUES ('Alice Johnson', '2025-06-01'), ('Bob Smith', '2025-06-15'), ('Charlie Lee', '2025-06-20'), ('Diana Patel', '2025-06-25');
-- Using LPAD Function
SELECT id, customer_name, CONCAT( LPAD(DAY(date_of_payment), 2, '0'), '/', LPAD(MONTH(date_of_payment), 2, '0'), '/', YEAR(date_of_payment) ) AS converted_date FROM Payment;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- A sample table creation
CREATE TABLE Payment ( id SERIAL PRIMARY KEY, customer_name VARCHAR(100), date_of_payment DATE );
-- Insertion of sample data
INSERT INTO Payment (customer_name, date_of_payment) VALUES ('Alice Johnson', '2025-06-01'), ('Bob Smith', '2025-06-15'), ('Charlie Lee', '2025-06-20'), ('Diana Patel', '2025-06-25');
-- Using TO_CHAR Function
SELECT id, customer_name, TO_CHAR(date_of_payment, 'DD/MM/YYYY') AS converted_date FROM Payment;