Using CONVERT()
The CONVERT() function can help you to convert dates into the desired format.
You need to get a report of the airports, carriers, canceled flights, and registration dates, registered in the first six months of the year 2014. You realize that the format of the registration_date column is yyyy-mm-dd, and you want to show the results in the format of mm/dd/yyyy, which is hardcoded as 101, using the CONVERT() function.
Notice that the type of the registration_date column is VARCHAR(10) and not a date.
Este exercício faz parte do curso
Cleaning Data in SQL Server Databases
Instruções do exercício
- Convert the type of the
registration_datecolumn toDATEand print it inmm/dd/yyyyformat. - Convert the
registration_datecolumn tomm/dd/yyyyformat to filter the results. - Filter the results for the first six months of 2014 in
mm/dd/yyyyformat.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
SELECT
airport_code,
carrier_code,
canceled,
-- Convert the registration_date to a DATE and print it in mm/dd/yyyy format
___(VARCHAR(10), ___(registration_date AS DATE), 101) AS registration_date
FROM flight_statistics
-- Convert the registration_date to mm/dd/yyyy format
WHERE ___(VARCHAR(10), ___(registration_date AS DATE), 101)
-- Filter the first six months of 2014 in mm/dd/yyyy format
BETWEEN '___' AND '___'