Using DATE_ADD with a Column Name as the Interval Value

China☆狼群 提交于 2019-12-01 04:02:08

It won't be easy o get your result - since you're storing it as plain 1 Year or similar. It is not allowed to use it dynamically in INTERVAL construct - MySQL syntax demands that you'll point both interval quantity and type.

However, there's kind of trick to resolve the matter:

SELECT 
    product_name, 
    start_date,
    expiry_period,
    @num:=CAST(expiry_period AS UNSIGNED),
    @p  :=SUBSTR(expiry_period, CHAR_LENGTH(@num)+2),
    CASE
      WHEN @p='Year' THEN DATE_ADD(start_date, INTERVAL @num YEAR)
      WHEN @p='Month' THEN DATE_ADD(start_date, INTERVAL @num MONTH)
      WHEN @p='Day' THEN DATE_ADD(start_date, INTERVAL @num DAY)
      WHEN @p='Week' THEN DATE_ADD(start_date, INTERVAL @num WEEK)
    END AS end_date
FROM
    tbl_products

-as you can see, this query relies on fact, that quantity always goes first (so CAST will extract exactly it, therefore, it can be used to get interval length after this). But in any case, you'll have to recount all possible interval types in CASE clause

Another good idea would be - to store your period in unified form (for example, always in days) - so you'll store only one number for each row (thus, 1 week=7days, e t.c.)

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!