Skip to content
Advertisement

Use replace with interval in sql query

I want to show if 0 it will put a word, perm for example and if not 0 it will get the normal date using interval.

Tried to include it in replace.

REPLACE(REPLACE(usetime,0,'PERM'), add_time + interval usetime minute)

If usetime = 0

START DATE            USETIME     END DATE
--------------------------------------------
2019-04-11 23:01:11     0           PERM

If usetime is not 0 and is something else(not a specific value)

START DATE            USETIME     END DATE
--------------------------------------------
2019-04-11 23:01:11     5         2019-04-11 23:06:11

Advertisement

Answer

Don’t use REPLACE(), use CASE:

(CASE WHEN usetime = 0 THEN 'PERM'
      ELSE add_time + interval usetime minute
 END)

You might want to use date_format() to format the date to a string with exactly the format you want.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement