Friday, 29 November 2019

How to convert julian date to normal date in MySQL

In this article, we will learn how to convert a Julian date to the calendar date with MySQL.
First, let’s see how it looks like a date in Julian format.

Julian calendar and Julian date format

The Julian calendar was used since the time of Julio Cesar, specifically 45 years before Christ. It was used until 1582 when Pope Gregory XIII introduced the Gregorian calendar that we currently use.
The dates in the Julian calendar can have several formats but usually, the first digits indicate the year and the last ones indicate the days that have elapsed in that year. In this article, we will use the following format: YYYYDDD.
Where YYYY indicates the year, and DDD indicates the number of days elapsed in that year.
Let’s see some examples:
Table 1. Julian date conversion examples

MySQL julian date conversion

To illustrate our example, we will use an Orders table with the following structure and data:
CREATE TABLE `orders_table` (
  `id` int(11),
  `client_id` int(11),
  `date_received` integer,
  `date_processed` integer,
  `total_amount` float,
  `total_paid` float
ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `orders_table` (`id``client_id``date_received``date_processed``total_amount``total_paid`VALUES
(112010079201008210075);
INSERT INTO `orders_table` (`id``client_id``date_received``date_processed``total_amount``total_paid`VALUES
(212011110201111250.220.5);
INSERT INTO `orders_table` (`id``client_id``date_received``date_processed``total_amount``total_paid`VALUES
(32201212520121267560);
INSERT INTO `orders_table` (`id``client_id``date_received``date_processed``total_amount``total_paid`VALUES
(422013200201320090.720);
INSERT INTO `orders_table` (`id``client_id``date_received``date_processed``total_amount``total_paid`VALUES
(5320142252014228120100);
INSERT INTO `orders_table` (`id``client_id``date_received``date_processed``total_amount``total_paid`VALUES
(6120150522015054500NULL);
INSERT INTO `orders_table` (`id``client_id``date_received``date_processed``total_amount``total_paid`VALUES
(7120161232016124150150);
INSERT INTO `orders_table` (`id``client_id``date_received``date_processed``total_amount``total_paid`VALUES
(832017312201731220050);
INSERT INTO `orders_table` (`id``client_id``date_received``date_processed``total_amount``total_paid`VALUES
(9220182242018227250250);
INSERT INTO `orders_table` (`id``client_id``date_received``date_processed``total_amount``total_paid`VALUES
(103201936020193633000);


ALTER TABLE `orders_table` ADD PRIMARY KEY (`id`);
Script 1. Orders table creation
We can execute the following command to verify the contents of the Orders table:
SELECT * FROM `orders_table`
Script 2. Query to retrieve Orders table content
Result:
Table 2. Orders table content
As we can see, the received and processed fields have saved the dates in Julian YYYYDDD format.
Let’s build a query to convert the dates of the received field from Julian to normal Calendar:
SELECT `id``client_id``date_received` as `Received Julian`MAKEDATE(LEFT(`date_received`,4) , `date_received` % 1000as `Received Normal Calendar``total_amount``total_paid` FROM `orders_table`
Script 3. Query to convert julian date to calendar date
To build our query, we rely on the LEFT function to obtain the four digits of the year and the % operator to calculate the elapsed days.
Result:
Table 3. Received field with a new format 

0 comments:

Post a Comment