How do I form an SQL query to show entire column of dates with modified values?
Question
Assume that I have a table Purchases with column named PurchaseDate and with data format of 'YYYY-MM-DD HH:MI:SS'.
Suppose that I want to see the entire column of dates with the days increasing by 5.
Suppose I form the query in the following way:
SELECT PurchaseDate+5 FROM Purchases;
The result will be given in a form of single numbers, and the increased part will be seconds instead of days.
I.e.: This 1982-09-07 00:00 will turn into this 198209070005.
How do I form the right standard SQL query?
UPDATE: I've got answer. Thanks.
-- this will work for standard SQL:
SELECT PurchaseDate, PurchaseDate + INTERVAL 5 DAY FROM Purchases;
-- this will work for MySQL:
SELECT PurchaseDate, ADDDATE(PurchaseDate, INTERVAL 10 DAY) FROM Purchases;
Solution
Asuming the column has a datetime type, simply:
SELECT PurchaseDate + INTERVAL 5 DAY FROM
If it was a string type, you would have to parse it first.
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange