Case statement for a date field with Mysql db - mysql

Iam trying to do a CASE statement. The requirement is if Date field is NULL then show it as 'Pending' or else show the date from the db.
I have tried the below case statement but there is some problem.
CASE po_amend_received_date IS NULL WHEN 1 THEN 'Pending'
ELSE po_amend_received_date END AS `po_amend_received_date`
Am i making any mistake here?

CASE WHEN po_amend_received_date IS NULL THEN 'Pending'
else po_amend_received_date
end as `po_amend_received_date`

Related

MySQL compare different values in case when function

In this query I use the CASE WHEN function to compare variables values in two different conditions. The problem is the number, because return a value = 1, but the correct result is NULL (20 is > of 8).
SELECT DISTINCT
case
when '20'>= '8' then null
when '20'< '8' then '1'
ELSE '0'
end checkValue
FROM DUAL;
How to resolve this problem?
The query run correctly if change 8 with 08. This solution is not applicable to because from variable arrive numers: 10 100 1000 units.
Thanks
I have change the field type from VARCHAR to DECIMAL and in this case work fine.
if you want compare string remember that string '20' < '8' so 'AB' is < 'C'
SELECT DISTINCT
case
when '20'>= '08' then null
when '20'< '08' then '1'
ELSE '0'
end checkValue
FROM DUAL;
otherwise if you want a number compare then you shoudl convert tne string as number
You need just cast strings to some numeric data type and then compare, for example
SELECT DISTINCT
case
when cast('20' as signed)>= cast('8' as signed) then null
when cast('20' as signed)< cast('8' as signed) then '1'
ELSE '0'
end checkValue
FROM DUAL;
Side note: DISTINCT no makes sense in this particular case

sql case when else do nothing

I have an ssis package that improts a csv file and then loads the data into a sql server db.
my table within sql server db has 5 fields that get populated with dates from another table. if there is a value in the 1st field then it will populate the second and so on. this works fine. i want to clear all the fields one all 5 fields are populated to start the process again.
UPDATE report
SET
date1 = CASE WHEN date1 IS NOT NULL and date5 IS NOT NULL Then '' else date1 end,
date2 = CASE WHEN date2 IS NOT NULL and date5 IS NOT NULL Then '' else date2 end
from report
my issue is with the 'else' part. i want it to be case when... then..else do nothing. at the moment its populating the else bit with the systemdate '1900-01-01' rather than leaving original value.
I share the Update Table Case when if syntax for all
UPDATE XTABLE
SET FieldX = (CASE WHEN FieldZ in (1,2,4,5,7,9,13) THEN '18;20'
WHEN FieldZ in (15,18) THEN 730 END),
FieldY = (CASE WHEN FieldZ in (1,2,4,5,7,9,13) THEN 2555
WHEN FieldZ in (15,18) THEN 730 END)
WHERE ConditionField = 1

SQL - Time Range (CASE WHEN)

I am working on a Time Query with the following logic. But I am not familiar with time query. Can anyone help?
CASE WHEN (07:00:00) TO (07:10:00) is between [STARTTIME] AND [STOPTIME] THEN 'YES'
Start Time and Stop Time are both DateTime Field
Why not
CASE WHEN '07:00:00' >= [STARTTIME] AND '07:10:00' <= [STOPTIME] THEN 'YES'
if I understand clearly what you want to do.
HERE is the query :- I used #start_Time(for STARTIME) and #end_Time (for STOPTIME) as local variable you can replace them. (As you said between i am taking in between those values if you need to include those time too then instead of > you need to put >= and same with < replace with <=).
select CASE WHEN ((time_to_sec(timediff('07:00:00', time(#Start_Time))))>0
AND (time_to_sec(timediff('07:00:00', time(#end_Time))))<0)
AND
((time_to_sec(timediff('07:10:00', time(#Start_Time))))>0
AND (time_to_sec(timediff('07:10:00', time(#end_Time))))<0) =1
THEN 'YES' else 'NO' END
Hope it helps you !!

Using DATE_SUB() based off dynamic field

How does one go abouts using a dynamically created field to be used within a DATE_SUB calculation?
I have this SQL below:
SELECT *,
CASE
WHEN `currentDate` IS NULL
THEN
`lastDate`
ELSE
`currentDate`
END AS `useDate`,
CASE
WHEN `type` = 'weekly'
THEN DATE_SUB(`useDate`, INTERVAL 1 WEEK)
END AS `nextDate`
FROM `aTable`
And this does not work, due to error '#1054 - Unknown column 'useDate' in 'field list''
The query works fine if I am using an actual field from the Select *, however won't accept the dynamically created field.
What would the proper way to work with this without having to do a 'case in case' query?
Did you try to use the CASE clause inside WHEN?
SELECT *,
CASE
WHEN `currentDate` IS NULL
THEN `lastDate`
ELSE `currentDate`
END AS `useDate`,
CASE
WHEN `type` = 'weekly'
THEN DATE_SUB(
CASE
WHEN `currentDate` IS NULL
THEN `lastDate`
ELSE `currentDate`
END AS `useDate`
, INTERVAL 1 WEEK)
END AS `nextDate`
FROM `aTable`

Using Sum inside a Case statement in MySQL

I am having problem in MySQL adding three values, this should be simple right?
I have code that selects values from a column based upon the value of a second column, I use a case statement like this:
Select
Max(Case
When Table1.costcode Like '%Costcode1%'
Then Table1.costs
Else Null End) As 'Costcode1',
Max(Case
When Table1.costcode Like '%Costcode2%'
Then Table1.costs
Else Null End) As 'Costcode2',
Max(Case
When Table1.costcode Like '%Costcode3%'
Then Table1.costs
Else Null End) As 'Costcode3',
(Case
When Table1.costcode In ('%Costcode1%','%Costcode2%','%Costcode3%')
Then Sum(Table1.costs)
Else Null End) As 'Total Cost',
From Table1
the first three Case statements work fine and all return values (these are held in the database as negative numbers e.g. -13624.00), however the Total Cost Case just returns Null...
The column Table1.costcode includes many other codes as well so I can't just sum all of the values without picking them out first.
It must be simple to sum these values, but obviously I'm missing something… Help, please :-)
Thanks
Try this -
SUM(Case Table1.costcode
When LIKE ('%Costcode1%') Then Table1.costs
When LIKE ('%Costcode2%') Then Table1.costs
When LIKE ('%Costcode3%') Then Table1.costs
Then Table1.costs
Else 0.00 End) As 'Total Cost'