w3resource

AdventureWorks Database: Return a truncated date with 4 months added to the orderdate

SQL Query - AdventureWorks: Exercise-182 with Solution

182. From the following table write a query in SQL to return a truncated date with 4 months added to the orderdate.

Sample table: sales.salesorderheader


Click to view Full table

Sample Solution:

SELECT orderdate,DATE_TRUNC('month', (select orderdate + interval '4 month'))
FROM Sales.salesorderheader;

Sample Output:

orderdate              |date_trunc             |
-----------------------+-----------------------+
2011-05-31 00:00:00.000|2011-09-01 00:00:00.000|
2011-05-31 00:00:00.000|2011-09-01 00:00:00.000|
2011-05-31 00:00:00.000|2011-09-01 00:00:00.000|
2011-05-31 00:00:00.000|2011-09-01 00:00:00.000|
2011-05-31 00:00:00.000|2011-09-01 00:00:00.000|
2011-05-31 00:00:00.000|2011-09-01 00:00:00.000|
2011-05-31 00:00:00.000|2011-09-01 00:00:00.000|
2011-05-31 00:00:00.000|2011-09-01 00:00:00.000|
2011-05-31 00:00:00.000|2011-09-01 00:00:00.000|
2011-05-31 00:00:00.000|2011-09-01 00:00:00.000|
2011-05-31 00:00:00.000|2011-09-01 00:00:00.000|
2011-05-31 00:00:00.000|2011-09-01 00:00:00.000|
...

SQL AdventureWorks Editor:

Practice Online


Contribute your code and comments through Disqus.

Previous: Return the difference in sales quotas for a specific employee.
Next: Return the orders that have sales on or after December 2011.


What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

SQL: Tips of the Day

ROW_NUMBER() in MySQL

SELECT t0.col3
FROM table AS t0
LEFT JOIN table AS t1 ON t0.col1=t1.col1 AND t0.col2=t1.col2 AND t1.col3>t0.col3
WHERE t1.col1 IS NULL;

Ref : https://bit.ly/3VX3Jzv