Building Date Formats


As previously mentioned, the week commencing field, week number field and week day field are not stored in the MySQL database as they are built from the transaction date field. By default, dates in MySQL are stored in the format 'yyyy-mm-dd'. The date functions to alter the format and produce the new date-related data are:

MySQL Code

SELECT trans_id, trans_staff_id, trans_cust_id,
DATE_FORMAT(trans_date, '%d/%m/%Y') AS Date,
DATE_FORMAT(DATE_ADD(trans_date, INTERVAL(-WEEKDAY(trans_date)) DAY),'%d/%m/%Y' ) AS trans_wc,
DAYNAME(trans_date) AS trans_day,
WEEK(trans_date) As trans_wknum,
MONTH(trans_date) As trans_month
FROM transactions

If the database being used was an MS-SQL database, this would be achieved as follows:

T-SQL Code (MS SQL)

SET DATEFIRST 1 -- Changes first day of week from Sunday to Monday
SELECT trans_id, trans_staff_id, trans_cust_id,
CONVERT (varchar(10), trans_date, 103) AS trans_date,
DATEADD(DAY, 1 - DATEPART(WEEKDAY, trans_date),
CAST(trans_date AS DATE)) AS trans_wc,
DATENAME(dw, trans_date) AS trans_day,
DATEPART(ISO_WEEK, trans_date) AS trans_wknum,
DATEPART(M, trans_date) AS trans_month
FROM transactions

Here are the live results of a MySQL query demonstrating the above functions that will build this data from the 'trans_date' in the Transactions table:


trans_idtrans_staff_idtrans_cust_idDatetrans_wctrans_daytrans_wknumtrans_month
5250458903/01/201702/01/2017Tuesday11
5351458303/01/201702/01/2017Tuesday11
5452456804/01/201702/01/2017Wednesday11
5553457004/01/201702/01/2017Wednesday11
5655458205/01/201702/01/2017Thursday11
5756458205/01/201702/01/2017Thursday11
5850458709/01/201709/01/2017Monday21
5951458909/01/201709/01/2017Monday21
6052458210/01/201709/01/2017Tuesday21
6153456811/01/201709/01/2017Wednesday21
6255456812/01/201709/01/2017Thursday21
6351461812/01/201709/01/2017Thursday21
6456456713/01/201709/01/2017Friday21
6550458316/01/201716/01/2017Monday31
6651458416/01/201716/01/2017Monday31
6752458318/01/201716/01/2017Wednesday31
6853458520/01/201716/01/2017Friday31
6955461620/01/201716/01/2017Friday31
7056461720/01/201716/01/2017Friday31
7150461825/01/201723/01/2017Wednesday41
7251458725/01/201723/01/2017Wednesday41
7352458925/01/201723/01/2017Wednesday41
7451456725/01/201723/01/2017Wednesday41
7553457025/01/201723/01/2017Wednesday41
7652457130/01/201730/01/2017Monday51
7753457230/01/201730/01/2017Monday51
7855457330/01/201730/01/2017Monday51
7950458506/02/201706/02/2017Monday62
8051458207/02/201706/02/2017Tuesday62
8151461807/02/201706/02/2017Tuesday62
8252460107/02/201706/02/2017Tuesday62
8353460207/02/201706/02/2017Tuesday62
8454460307/02/201706/02/2017Tuesday62
8555461607/02/201706/02/2017Tuesday62
8656458207/02/201706/02/2017Tuesday62
8757458308/02/201706/02/2017Wednesday62
8850461613/02/201713/02/2017Monday72
8951457213/02/201713/02/2017Monday72
9051457013/02/201713/02/2017Monday72
9152458513/02/201713/02/2017Monday72
9253461713/02/201713/02/2017Monday72
9354456715/02/201713/02/2017Wednesday72
9455457115/02/201713/02/2017Wednesday72
9556457215/02/201713/02/2017Wednesday72
9657459115/02/201713/02/2017Wednesday72
9757459216/02/201713/02/2017Thursday72
9850461820/02/201720/02/2017Monday82
9951460120/02/201720/02/2017Monday82
10052460220/02/201720/02/2017Monday82
10152460320/02/201720/02/2017Monday82
10253458320/02/201720/02/2017Monday82
10354458523/02/201720/02/2017Thursday82
10455461723/02/201720/02/2017Thursday82
10556461623/02/201720/02/2017Thursday82
10657457124/02/201720/02/2017Friday82
10750461128/02/201727/02/2017Tuesday92
10851461228/02/201727/02/2017Tuesday92
10952461328/02/201727/02/2017Tuesday92
11052460328/02/201727/02/2017Tuesday92
11153461602/03/201727/02/2017Thursday93
11254461403/03/201727/02/2017Friday93
11355457203/03/201727/02/2017Friday93
11456460303/03/201727/02/2017Friday93
11557457103/03/201727/02/2017Friday93
11656460107/03/201706/03/2017Tuesday103
11757460208/03/201706/03/2017Wednesday103
11855460309/03/201706/03/2017Thursday103
11954458309/03/201706/03/2017Thursday103
12054458509/03/201706/03/2017Thursday103
12153461210/03/201706/03/2017Friday103
12252461610/03/201706/03/2017Friday103
12350461710/03/201706/03/2017Friday103


Click the button below to advance to the next stage


Next: >> Inner Join on Two Tables


Or use the above 'SQL Queries' dropdown menu to jump to any stage.