MySQL /* Query 1. (Chapter 8, 10 points) Write a SELECT statement that returns f
ID: 3722763 • Letter: M
Question
MySQL
/* Query 1. (Chapter 8, 10 points)
Write a SELECT statement that returns four colums from the Orders table:
-OrderDate, which is the original OrderDate
-OrderDateConverted, which is a column that uses the CONVERT function to return
the OrderDate column in this format: MM/DD/YYYY. In other words, use 2-digit
months and days and a 4-digit year, and separate each date component with slashes.
-AMPM, which is a column that uses the CONVERT function to return the OrderDate
column with the date, and the hours and minutes on a 12-hour clock with an am/pm
indicator.
-OrderTime, which is a column that uses the CONVERT function to return the OrderDate
column with 2-digit hours, minutes, and seconds on a 24-hour clock. Use leading zeros
for all date/time components.
Hint: https://docs.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql
*/
Explanation / Answer
Below is the SQL Query that returns the four columns from the Order Table.
=======================================================================
select
orderDate,
date_format(orderDate,'%m/%d/%Y') as orderDateConverted,
date_format(orderDate,'%m/%d/%Y %r') as AMPM,
date_format(orderDate,'%m/%d/%Y %k:%i:%s') as orderTime
from Orders;
===================================================================================
DATE_FORMAT function is used to convert the date field in various formats.
Syntax : DATE_FORMAT(DateField, FormatPattern);
Format Pattern can have various format specifiers like :
%m - Display month in numeric format (0-12)
%d - Display day of the month in numeric format(00-31)
%Y - Displays year in numeric four digits
Example : Date_Format('1999-06-18','%m/%d/%Y') - Displayed as 06/18/1999
%k - Displays hours (0-23)
%i - Displays minutes in numeric (00-59)
%s - Displays seconds in numeric (00-59)
%r - Display Time, 12-hour (hh:mm:ss followed by AM or PM)
Example :
Date_Format('1999-06-18','%m/%d/%Y %r') - Displayed as 06/18/1999 12:00:00 AM
Date_Format('1999-06-18','%m/%d/%Y %k:%i:%s') - Displayed as 06/18/1999 0:00:00
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.