-3

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?

  • 1
    So, what do you need exactly? Do you need to set date back of 1 year and 1 month? Or what? – Marco Nov 18 '13 at 11:57
  • For date i using declaration. Now i selecting data from SQL by date. But now i know how can i change year, month or day). But if i want select data from SQL where i need change date while by year and month – user2967933 Nov 18 '13 at 12:01
  • Still I don't understand your needs... – Marco Nov 18 '13 at 12:03
  • My input time is example: 15.02.2013 - > @date_from ok if i want change date i using this dateadd(month, -1, @date_from) Output is this: 15.01.2013 But i need earn this in one command 15.01.2012 – user2967933 Nov 18 '13 at 12:05
  • 1
    So use `DATEADD(year, -1, dateadd(month, -1, @date_from))`. – SchmitzIT Nov 18 '13 at 12:10

1 Answers1

1

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.

SchmitzIT
  • 9,227
  • 9
  • 65
  • 92