Case when sim.PickPackUom='IP' then sim.InnerPackQuantity
else
im.CaseQuantity
end
as divqty
,concat (cast (i.QuantityOnHand as float)/divqty ,' ', sim.PickPackUom )qty
concat is giving error. How can I make divqty value available for concat or division?
You have to use the entire case condition instead of divqty:
Case when sim.PickPackUom='IP' then sim.InnerPackQuantity
else im.CaseQuantity
end as divqty
,concat (
cast (i.QuantityOnHand as float)/
(Case when sim.PickPackUom='IP' then sim.InnerPackQuantity
else im.CaseQuantity end),
' ', sim.PickPackUom) qty
Related
I am trying to wrap the inner CASE statements below with an outer CASE statement to output '00-00-00-00-00' if there is not a matching row found on the left-joined OtherTable C , instead what is happening is when there is not a row found in OtherTable C then it is outputting ---- (just dashes).
SELECT A.INV_ITEM_ID,
CASE WHEN C.INV_ITEM_ID '' THEN '00-00-00-00-00'
ELSE ( CONCAT(CASE WHEN C.STORAGE_AREA like '[0-9]'
THEN '0'+ C.STORAGE_AREA
WHEN C.STORAGE_AREA = '' THEN '00'
ELSE C.STORAGE_AREA END ,'-', CASE WHEN C.STOR_LEVEL_1 like '[0-9]'
THEN '0' + C.STOR_LEVEL_1
WHEN C.STOR_LEVEL_1 = '' THEN '00'
ELSE C.STOR_LEVEL_1 END , '-',
CASE WHEN C.STOR_LEVEL_2 like '[0-9]'
THEN '0' + C.STOR_LEVEL_2
WHEN C.STOR_LEVEL_2 = '' THEN '00'
ELSE C.STOR_LEVEL_2 END, '-',
CASE WHEN C.STOR_LEVEL_3 like '[0-9]'
THEN '0' + C.STOR_LEVEL_3
WHEN C.STOR_LEVEL_3 = '' THEN '00'
ELSE C.STOR_LEVEL_3 END, '-',
CASE WHEN C.STOR_LEVEL_4 like '[0-9]'
THEN '0' + C.STOR_LEVEL_4
WHEN C.STOR_LEVEL_4 = '' THEN '00'
ELSE C.STOR_LEVEL_4 END ) ) END
FROM MyTable A
LEFT OUTER JOIN OtherTable C ON C.INV_ITEM_ID = A.INV_ITEM_ID
Is there a way to achieve this without having to use a Sub-query here?
If there is no match in a left join the OtherTable value will be null not ''
So when you say this:
CASE WHEN C.INV_ITEM_ID '' THEN '00-00-00-00-00'
You are checking if there is a zero length string or '' in the field c.INV_ITEM_ID
Instead you should use
CASE WHEN C.INV_ITEM_ID is null THEN '00-00-00-00-00'
You likely do not have any c.INV_ITEM_ID with string value ''. So when there is no matching data in the left join (ie the value is null) the case expression moves on and performs a concatenation. Each sub-case expression checks for c.STOR_LEVEL_1 and doesn't have a match so goes with the "else" or C.STOR_LEVEL_1. So what you actually have is
concat(null,'-',null,'-',null,'-',null,'-')
Which evaluates to ----
Might be able to cut your SQL down a bit:
SELECT
A.INV_ITEM_ID,
CONCAT(
RIGHT(CONCAT('00', C.STOR_LEVEL_1), 2), '-',
RIGHT(CONCAT('00', C.STOR_LEVEL_2), 2), '-',
RIGHT(CONCAT('00', C.STOR_LEVEL_3), 2), '-',
RIGHT(CONCAT('00', C.STOR_LEVEL_4), 2)
) as x
CONCAT behaves slightly differently to + with NULL. CONCAT treats null as emptystring, but + will nullify the whole expression. This, whether your column is null, a single digit or double digit, if you CONCAT it onto '00' then take the rightmost 2 you end up with a 2 digits number (00 if null, 0x if one digit, xx if 2).
I have the following select query. I want to convert it from string into a number. If I wrap a TO_NUMBER around the case expression, I get
expression must have same datatype as corresponding expression
error.
SELECT CASE SUBSTR(GRADE, INSTR(GRADE, ' ') + 1)
WHEN 'Unspecified' THEN ' '
ELSE SUBSTR(GRADE, INSTR(GRADE, ' ') + 1)
END as Final_Grade,
How can I get Final_Grade to be numeric?
Thank you!
Well, ' ' is not a number, so better figure out what to do. I would suggest NULL:
SELECT (CASE SUBSTR(GRADE, INSTR(GRADE, ' ') + 1)
WHEN 'Unspecified' THEN NULL
ELSE TO_NUMBER(SUBSTR(GRADE, INSTR(GRADE, ' ') + 1))
END) as Final_Grade,
Actually, I prefer:
(CASE WHEN GRADE NOT LIKE 'Unspecified%'
THEN TO_NUMBER(SUBSTR(GRADE, INSTR(GRADE, ' ') + 1))
END) as Final_Grade
Or perhaps even more safely:
(CASE WHEN REGEXP_LIKE(GRADE, '^[0-9]+ ')
THEN TO_NUMBER(SUBSTR(GRADE, INSTR(GRADE, ' ') + 1))
END) as Final_Grade
I am working in SQL and I have 3 columns Current Name, Given Full Name and Whether the names match (Y or No)
The problem with that is that when I am comparing the strings in the first 2 columns, it is not showing me the current result. For example, I am not finding a way to prove that 'Tushar Sharma' is same as 'Tushar-Sharma' considering that Tushar Sharma is the current full name and Tushar-Sharma is the name that has been extracted from a report.
I am stuck at the LIKE statement as to what to do if I want to have hyphen(-) included in the comparison so that I get a Y in the 3rd column.
Thank you
One option is to remove the hyphen for the comparison:
select (case when replace(given_name, '-', '') = replace(full_name, '-', '') then 'Y' else 'N' end) as names_match
You can use replace() with like as well:
select (case when replace(given_name, '-', '') like '%' + replace(full_name, '-', '') '%' then 'Y' else 'N' end) as names_match
Replace - with whitespace and compare, you can also use regex or fuzzy matching to improve the match for other conditions.
AND REPLACE(CurrentName, '-', ' ') = REPLACE(GivenName, '-', ' ');
Ex:
AND REPLACE('Tushar Sharma', '-', ' ') = REPLACE('Tushar-Sharma', '-', ' ')
will eval to
AND 'Tushar Sharma' = 'Tushar Sharma'
this will work:
select currentname,givenfullname,case when regexp_replace(currentname,' ','') like
regexp_replace(givenfullname,' ','') the 'Y' else 'N' end as matchstatus from
table_name;
I want my view to select the most recent 'Manual Handling' renewal date but cannot find how to incorporate the MAX function in the query
ALTER VIEW [dbo].[cb_TrainingFar] AS
SELECT
ISNULL(cs_facilities.guid, '00000000-0000-0000-0000-000000000000') AS [FacilityGuid],
'' as DART_ALL,
ISNULL(cs_facilities.name,'') as [Facility name],
ISNULL (ct_workers.forenames,'') + ',' + ISNULL (ct_workers.surname,'') AS 'Name of worker / volunteer',
ISNULL (ct_workers.startDate, 0) As 'Start Date',
CASE when ct_qualificationTypes.type =
'Manual Handling'
THEN CONVERT (varchar, ct_qualifications.renewalDate)
ELSE 'Not completed'
END 'Manual Handling',
Here is the general idea. You should be able to work out the specifics:
select case when maxdate > getdate() then 'Fred' else 'Wilma' end Flinstone
from
(select id, max(datefield) maxdate
from sometable
group by id) temp
getdate() is a sql server function. You didn't specify your database.
Based on your inputs
ALTER VIEW [dbo].[cb_TrainingFar] AS
SELECT
ISNULL(cs_facilities.guid, '00000000-0000-0000-0000-000000000000') AS [FacilityGuid],
'' as DART_ALL,
ISNULL(cs_facilities.name,'') as [Facility name],
ISNULL (ct_workers.forenames,'') + ',' + ISNULL (ct_workers.surname,'') AS 'Name of worker / volunteer',
ISNULL (ct_workers.startDate, 0) As 'Start Date',
CASE
ct_qualificationTypes.type
WHEN 'Manual Handling'
THEN CONVERT (varchar, MAX(ct_qualifications.renewalDate))
ELSE 'Not completed' END AS 'Manual Handling'
FROM ct_workers
...
GROUP BY ct_qualificationstype
Here is one approach using a window function:
ALTER VIEW [dbo].[cb_TrainingFar] AS
SELECT
ISNULL(cs_facilities.guid, '00000000-0000-0000-0000-000000000000') AS [FacilityGuid],
'' as DART_ALL,
ISNULL(cs_facilities.name,'') as [Facility name],
ISNULL (ct_workers.forenames,'') + ',' + ISNULL (ct_workers.surname,'') AS 'Name of worker / volunteer',
ISNULL (ct_workers.startDate, 0) As 'Start Date',
CASE when ct_qualificationTypes.type =
'Manual Handling'
THEN CONVERT(varchar(255), max(ct_qualifications.renewalDate) over ())
ELSE 'Not completed'
END 'Manual Handling',
However, I suspect that your problem requires better understanding the query. What does the rest of the query look like? By the way, never use varchar without length. The default length varies depending on the context and it is quite dangerous to depend on the default length being sufficiently long.
I am trying to combine multiple columns (varchar, but used to store boolean 'Y' or '') into a single column (list) with human readable text.
The Table layout is like this:
MEMBER_ID (int) | PROC (varchar) | 50K_12_MTHS (varchar) | 100K_12_MTHS (varchar)
1||||
2|Y|Y||
3|Y|Y|Y|
4|Y|||
For the output of the able sample I am trying to get:
1|
2|Proc, 50
3|Proc, 50, 100
4|Proc
I think the way to do this is with a Case (see below) but can't get it to work.
SELECT
MEMBER_ID,
Gorup =
Select(
CASE PROC
WHEN 'Y'
THEN 'Proc'
END + ', ' +
CASE 50K_12_MTHS
WHEN 'Y'
THEN '50K'
END-- + ', ' +
CASE 100K_12_MTHS
WHEN 'Y'
THEN '100K'
END + ', ' +)
from Members
Nearly...!
SELECT
MEMBER_ID,
CASE [PROC]
WHEN 'Y' THEN 'Proc, '
ELSE ''
END +
CASE [50K_12_MTHS]
WHEN 'Y' THEN '50K,'
ELSE ''
END +
CASE [100K_12_MTH]
WHEN 'Y' THEN '100K, '
ELSE ''
END as [group]
from Members
Try this
SELECT
MEMBER_ID,
(CASE [PROC] WHEN 'Y'
THEN 'Proc' ELSE ''
END +
CASE [50K_12_MTHS] WHEN 'Y'
THEN ', 50K' ELSE '' END
+ CASE [100K_12_MTHS] WHEN 'Y'
THEN ', 100K' ELSE ''
END) as [GROUP]
from Members