adding months to a date SQL

后端 未结 3 1075
离开以前
离开以前 2021-01-25 18:21

I am trying to add months to an existing date in SQL. The new column displayed will have a followup column instead of a days column. Im getting an error in the select statement.

相关标签:
3条回答
  • 2021-01-25 18:40

    Your usage of the add_months() function is incorrect. It's not two words, it's just one (with an underscore)

    add_months(datesold, 1)
    

    note the underscore _ between ADD and MONTHS. It's function call not an operator.

    Alternatively you could use:

    datesold + INTERVAL '1' month
    

    Although it's worth noting that the arithmetics with intervals is limited (if not broken) because it simply "increments" the month value of the date value. That can lead to invalid dates (e.g. from January to February). Although this is documented behaviour (see below links) I consider this a bug (the SQL standard requires those operations to "Arithmetic obey the natural rules associated with dates and times and yield valid datetime or interval results according to the Gregorian calendar")

    See the manual for details:
    http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions011.htm#i76717
    http://docs.oracle.com/cd/E11882_01/server.112/e26088/sql_elements001.htm#i48042

    Another thing:

    I am trying to add months to an existing date in SQL.

    Then why are you using an INSERT statement? To change the data of existing rows you should use UPDATE. So it seems what you are really after is something like this:

    update auctions
       set datesold = add_months(datesold, 1)
    where item = 'Radio';
    
    0 讨论(0)
  • 2021-01-25 18:42

    This can be used to add months to a date in SQL:

    select DATEADD(mm,1,getdate())
    

    This might be a useful link.

    0 讨论(0)
  • 2021-01-25 18:48

    Your SQL has typographical quotation marks, not standard ones. E.g. is not the same as '. Instead of delimiting a string value, those quotes become part of the value, at least for the particular SQL I have here to test with.

    If this doesn't fix your problem, try posting the error you're getting in your question. Magical debugging isn't possible.

    0 讨论(0)
提交回复
热议问题