1. Write a SELECT statement that returns these columns from the Products table:
ID: 642805 • Letter: 1
Question
1. Write a SELECT statement that returns these columns from the Products table:
The ListPrice column
A column that uses the CAST function to return the ListPrice column with 1 digit to the
right of the decimal point
A column that uses the CONVERT function to return the ListPrice column as an integer
A column that uses the CAST function to return the ListPrice column as an integer
MY CODE:
SELECT ListPrice,
CAST(ListPrice AS decimal)
CONVERT(decimal, ListPrice)
CAST(ListPrice AS Int)
FROM Products;
2. Write a SELECT statement that returns these columns from the Products table:
The DateAdded column
A column that uses the CAST function to return the DateAdded column with its date only
(year, month, and day)
A column that uses the CAST function to return the DateAdded column with its full time
only (hour, minutes, seconds, and milliseconds)
A column that uses the CAST function to return the OrderDate column with just the
month and day
MY CODE:
SELECT DateAdded,
CAST(DateAdded AS date) AS CastDate,
CAST(DateAdded AS time ) AS Time,
CAST(DateAdded AS date (mm:dd)) AS OrderDate,
FROM Products;
3. Write a SELECT statement that returns these columns from the Orders table:
A column that uses the CONVERT function to return the OrderDate column in this
format: MM/DD/YYYY. In other words, use two-digit months, days, and years and
separate each date component with slashes
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
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.
Explanation / Answer
1.
The SQL query to return the ListPrice column from Products table is as follows:
SELECT ListPrice, CAST(ListPrice AS DECIMAL(18, 1)) AS DECIMALPRICE, CONVERT(INT, ListPrice) AS INTPRICE, CAST(ListPrice AS INT) AS PRICEINT FROM Products;
2.
The SQL query to return the DateAdded column from Products table is as follows:
SELECT DateAdded, CAST(DateAdded AS date) AS DateOnly, CAST(DateAdded AS time) AS TimeOnly, CAST(DateAdded AS varchar(6)) MonthDay FROM Products;
3.
The SQL query to return the OrderDate column from Orders table is as follows:
SELECT CONVERT(varchar, OrderDate, 1) AS DateOfOrder, CONVERT(varchar, OrderDate, 100) AS , CONVERT(varchar, OrderDate, 8) FROM Orders;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.