Using DATE_ADD with a Column Name as the Interval Value

后端 未结 2 1142
离开以前
离开以前 2021-01-11 17:32

I have a table which contains products, a start date and an interval value :

product_name                    start_date              expiry_period
Domain Reg         


        
2条回答
  •  清酒与你
    2021-01-11 18:26

    You need to test for each one individually:

    select product_name, start_date, expiry_period,
           (case when expiry_period like '%day'
                 then DATE_ADD(start_date, INTERVAL expiry_period + 0 DAY) as end_date
                 when expiry_period like '%week'
                 then DATE_ADD(start_date, INTERVAL expiry_period + 0 WEEK) as end_date
                 when expiry_period like '%month'
                 then DATE_ADD(start_date, INTERVAL expiry_period + 0 MONTH) as end_date
                 when expiry_period like '%year'
                 then DATE_ADD(start_date, INTERVAL expiry_period + 0 YEAR) as end_date
           end)
    from tbl_products;
    

    The arithmetic (+ 0 and * 7) converts the string to a number.

提交回复
热议问题