I was trying to do the following:
SELECT CASE WHEN CHARINDEX ('(',sohCusref ) > 0 THEN SUBSTRING(sohCusref ,1,CHARINDEX ('(',sohCusref ) - 1) WHEN CHARINDEX (' ',sohCusref ) > 0 THEN SUBSTRING(sohCusref ,1,CHARINDEX (' ',sohCusref ) - 1) WHEN CHARINDEX ('/',sohCusref ) > 0 THEN SUBSTRING(sohCusref ,1,CHARINDEX ('/',sohCusref ) - 1) ELSE sohCusref END as sohCusrefFormatted FROM dbo.BaseData
But for some reason it would apply the first condition... after scratching my head for a while I tried changing the operators to '<' ,'<>', '=' and things started working... not applying the correct conditions but was picking up all conditions.
Colleague suggested i try it with a simple example so tried following:
SELECT CASE WHEN CHARINDEX ('/',sohCusref ) > 0 THEN SUBSTRING(sohCusref,1,CHARINDEX ('/',sohCusref ) - 1) WHEN CHARINDEX (' ',sohCusref ) > 0 THEN SUBSTRING(sohCusref,1,CHARINDEX (' ',sohCusref ) - 1) WHEN CHARINDEX ('(',sohCusref ) > 0 THEN SUBSTRING(sohCusref,1,CHARINDEX ('(',sohCusref ) - 1) ELSE sohCusref END as sohCusref_formatted FROM ( SELECT 'AAA/BBB)' AS sohCusref UNION ALL SELECT 'CCC DDD' AS sohCusref UNION ALL SELECT 'EEE(FFF)' AS sohCusref UNION ALL SELECT 'GGG/HHH III(JJJ)' AS sohCusref ) test
Which worked fine... and hence led me to check whether it was something to do with the type which was char(25)... i changed it to varchar and everything started working fine.
Can anybody highlight the reason why it behaved in this way with a char column?
Thanks!
SQL Case Statement CHAR
Hi Yunus Simjee, as the behavior of char() datatype it pad blank space if your value size is not as enough.
SELECT 'CHAR(25)' AS [DataType] , CHARINDEX(' ', CAST('EEE(FFF)' AS CHAR(25))) AS [CharIndex] , DATALENGTH(CAST('EEE(FFF)' AS CHAR(25))) AS [DataLength] GO SELECT 'VARCHAR(25)' AS [DataType] , CHARINDEX(' ', CAST('EEE(FFF)' AS VARCHAR(25))) AS [CharIndex] , DATALENGTH(CAST('EEE(FFF)' AS VARCHAR(25))) AS [DataLength][DataLength]
so if you want the same result using char(25) then you have to change your when clause condition.Put ' ' condition comparision at the end of case statement like below example
--Using VarChar SELECT CASE WHEN CHARINDEX ('/',sohCusref ) > 0 THEN SUBSTRING(sohCusref,1,CHARINDEX ('/',sohCusref ) - 1) WHEN CHARINDEX (' ',sohCusref ) > 0 THEN SUBSTRING(sohCusref,1,CHARINDEX (' ',sohCusref ) - 1) WHEN CHARINDEX ('(',sohCusref ) > 0 THEN SUBSTRING(sohCusref,1,CHARINDEX ('(',sohCusref ) - 1) ELSE sohCusref END as sohCusref_formatted FROM ( SELECT 'AAA/BBB)' AS sohCusref UNION ALL SELECT 'CCC DDD' AS sohCusref UNION ALL SELECT 'EEE(FFF)' AS sohCusref UNION ALL SELECT 'GGG/HHH III(JJJ)' AS sohCusref ) test GO --Using Char(25) SELECT CASE WHEN CHARINDEX ('/',sohCusref ) > 0 THEN SUBSTRING(sohCusref,1,CHARINDEX ('/',sohCusref ) - 1) WHEN CHARINDEX ('(',sohCusref ) > 0 THEN SUBSTRING(sohCusref,1,CHARINDEX ('(',sohCusref ) - 1) WHEN CHARINDEX (' ',sohCusref ) > 0 THEN SUBSTRING(sohCusref,1,CHARINDEX (' ',sohCusref ) - 1) ELSE sohCusref END as sohCusref_formatted FROM ( SELECT CAST('AAA/BBB)' AS CHAR(25)) AS sohCusref UNION ALL SELECT CAST('CCC DDD' AS CHAR(25)) AS sohCusref UNION ALL SELECT CAST('EEE(FFF)' AS CHAR(25)) AS sohCusref UNION ALL SELECT CAST('GGG/HHH III(JJJ)' AS CHAR(25)) AS sohCusref ) test
Thanks! Appreciate the help!
Thanks for your sharing
Managed Windows Shared Hosting by OrcsWeb