MySQL Forums
Forum List  »  Stored Procedures

Re: DAYNAME problem using CASE statement
Posted by: Peter Brawley
Date: May 07, 2020 08:34AM

That form of Case clause compares When subclause values with the value following the Case keyword, as you can see ...

set @x=5;
select @x, pow(@x,2),case @x when pow(@x,2)=25 then @x+2 else @x+1 end as xnew; 
+------+-----------+------+
| @x   | pow(@x,2) | xnew |
+------+-----------+------+
|    5 |        25 |    6 |
+------+-----------+------+

...ie pow(@x,2)=25 evaluates to 1 which<>@x. You need ...

set @d = '2020-5-9'; -- sat
SELECT
@d, DAYNAME(@d),
CASE @d WHEN DAYNAME(@d) = "Saturday" THEN DATE_ADD(@d, INTERVAL 2 DAY)
ELSE DATE_ADD(@d, INTERVAL 1 DAY) END as despatchDate;
+----------+-------------+--------------+
| @d | DAYNAME(@d) | despatchDate |
+----------+-------------+--------------+
| 2020-5-9 | Saturday | 2020-05-11 |
+----------+-------------+--------------+
[/code]

Options: ReplyQuote


Subject
Views
Written By
Posted
Re: DAYNAME problem using CASE statement
442
May 07, 2020 08:34AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.