MSSQL Edit date (YEAR, MONTH) in one command
Question
I need manipulate with time in one command. I have this date "15.02.2013" and now i need this time change on this "15.01.2012". Date is dynamic stats for me. i need change time with this functions (GATEDATE, YEAR, MONTH) or others.
INPUT:
"15.02.2013"
I need this output:
"15.01.2012"
Can you help me please?
Solution
You can do a double DATEADD
on the same value.
I.e.
SELECT DATEADD(year, -1, DATEADD(month, -1, @date_from)) FROM Table1
The inner DATEADD
is your original, which subtracts the month. This is then wrapped in a second DATEADD
which subtracts the year. Alternatively, if it's always 1 year and one month, you could easily subtract 13 months:
SELECT DATEADD(month, -13, @date_from))
You could even choose to have a computed column in your table using the function to calculate the date.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow