issue with CASE combined with an OR - sql

SELECT
Siren,
CASE WHEN Code_Juridique LIKE 'M%' AND Enseigne IS NOT NULL AND Enseigne <> '' --ok
THEN 'Enseigne : ' + Enseigne
WHEN (Sigle IS NULL OR Sigle ='')
AND (Enseigne IS NULL OR Enseigne ='')
THEN '' -- ok
WHEN
(Sigle IS NOT NULL OR Sigle <> '' ) THEN 'Sigle : ' + Sigle
ELSE 'Sigle / Enseigne : ' + Sigle + ' / ' + Enseigne
END as SigleEnseigne1,
Sigle,
Enseigne,
Code_Juridique
FROM #JohnJack
The code is straightforward.
Issue lies with the third when as you can see below
I should have have nothing on my 4th and 5th line, yet it is giving me Sigle :
What I want is to have the column SigleEnseigne1 on the 4th and 5th line , to be empty
Thanks for your insights

Try this:
SELECT
Siren,
CASE WHEN ( Code_Juridique LIKE 'M%' ) AND ( IsNull( Enseigne, '' ) <> '' )
THEN 'Enseigne : ' + Enseigne
WHEN ( IsNull( RTrim(LTrim(Sigle)), '') = '') AND ( IsNull( Enseigne, '' ) = '')
THEN '' -- ok
WHEN ( IsNull( RTrim(LTrim(Sigle)), '' ) <> '' )
THEN 'Sigle : ' + RTrim(LTrim(Sigle))
ELSE
'Sigle / Enseigne : ' + IsNull( RTrim(LTrim(Sigle)), '' ) + ' / ' + Enseigne
END as SigleEnseigne1,
Sigle,
Enseigne,
Code_Juridique
FROM #JohnJack

Besides stating the obvious that a (TRUE OR FALSE) = TRUE
I would simplify and bullet proof the code by using ISNULL() and LEN().
SELECT
Siren,
CASE WHEN Code_Juridique LIKE 'M%' AND LEN(ISNULL(Enseigne,'')) > 0 --ok
THEN 'Enseigne : ' + Enseigne
WHEN (LEN(ISNULL(Sigle, '')) = 0)
AND (LEN(ISNULL(Enseigne, '')) = 0)
THEN '' -- ok
WHEN
LEN(ISNULL(Sigle, '')) > 0 THEN 'Sigle : ' + Sigle
ELSE 'Sigle / Enseigne : ' + ISNULL(Sigle, '') + ' / ' + ISNULL(Enseigne, '')
END as SigleEnseigne1,
Sigle,
Enseigne,
Code_Juridique
FROM #JohnJack
How would your code react if those fields contain whitespaces? LEN automatically trims trailing whitespaces.

This line is causing your probelm:
(Sigle IS NOT NULL OR Sigle <> '' ) THEN 'Sigle : ' + Sigle
...but that is only obvious because you state that you dont want this result. Other than that the code acts as would be expected.
The simplest solution would be to take out:
'Sigle : ' + Sigle
but that may or may not be precisely what you are looking for. Based on the given information it is the solution but there is not an abundance of information to work off
If you are trying to get non null values to print then it should be an AND rather than an OR. When you use an OR it will return true if EITHER condition is true.

I'm not sure why you have this when
WHEN (Sigle IS NULL OR Sigle = '')
AND (Enseigne IS NULL OR Enseigne ='')
THEN '' -- ok
As what you want is SigleEnseigne1 to be NULL or '' when Sigle is NULL or '' don't you need this when instead
WHEN (Sigle IS NULL OR Sigle = '') THEN ''
There can also be the problem that Sigle is not the empty string and has whitespaces. You can use the LTRIM() and RTRIM() functions

It took me the weekend but actually, I figured out My mistake.
Sorry for the mess
The answer is below
SELECT
Siren,
CASE WHEN ( Code_Juridique LIKE 'M%' ) AND ( IsNull( Enseigne, '' ) <> '' )
THEN 'Enseigne : ' + Enseigne
WHEN ( IsNull( RTrim(LTrim(Sigle)), '') = '') AND ( IsNull( Enseigne, '' ) = '')
THEN '' -- ok
WHEN (Sigle IS NOT NULL OR Sigle <> '') AND (Enseigne IS NULL OR Enseigne = '')
THEN 'Sigle : ' + RTrim(LTrim(Sigle))
WHEN (Sigle IS NULL OR Sigle = '') AND (Enseigne IS NOT NULL OR Enseigne <> '')
THEN ''
WHEN
(Sigle IS NOT NULL OR Sigle <> '') AND (Enseigne IS NOT NULL OR Enseigne <> '')
THEN 'Sigle / Enseigne : ' + IsNull( RTrim(LTrim(Sigle)), '' ) + ' / ' + Enseigne
END as SigleEnseigne1
FROM John_Jack
Have a great end of the year

Related

How to replace the phrase "1234 Address" by an empty string for the aliased fields

When I run this code, the fields Old_Address and New_Address are containing 1234 Address. I would like the result to change 1234 Address to an empty string each time found. What is an easy way to do this?
INSERT
INTO OPT_INT.mgnNACHG_Results
(
BRANCH_CD,
ACCOUNT_CD,
RR_CD,
ALT_BRANCH_CD,
[OldAddress],
[NewAddress],
[AddressUpdate],
[OldName],
[NewName],
[NameUpdate]
)
SELECT A.BRANCH_CD,
A.ACCOUNT_CD,
A.RR_CD,
A.ALT_BRANCH_CD,
LTRIM(RTRIM(ISNULL(B.LINE4, '')))
+ LTRIM(RTRIM(ISNULL(B.LINE5, '')))
+ LTRIM(RTRIM(ISNULL(B.LINE6, ''))) AS 'Old_Address',
LTRIM(RTRIM(ISNULL(A.LINE4, '')))
+ LTRIM(RTRIM(ISNULL(A.LINE5, '')))
+LTRIM(RTRIM(ISNULL(A.LINE6, ''))) AS 'New_Address',
CASE
WHEN (LTRIM(RTRIM(ISNULL(B.LINE4, ''))) <> LTRIM(RTRIM(ISNULL(A.LINE4, ''))))
OR (LTRIM(RTRIM(ISNULL(B.LINE5,''))) <> LTRIM(RTRIM(ISNULL(A.LINE5, ''))))
OR (LTRIM(RTRIM(ISNULL(B.LINE6, ''))) <> LTRIM(RTRIM(ISNULL(A.LINE6, ''))))
THEN 'Address Change'
ELSE ''
END as 'Address_Update',
LTRIM(RTRIM(ISNULL(B.LINE1, '')))
+ LTRIM(RTRIM(ISNULL(B.LINE2, '')))
+ LTRIM(RTRIM(ISNULL(B.LINE3, ''))) AS 'Client_Old_Name',
LTRIM(RTRIM(ISNULL(A.LINE1, '')))
+ LTRIM(RTRIM(ISNULL(A.LINE2, '')))
+ LTRIM(RTRIM(ISNULL(A.LINE3, ''))) AS 'Client_New_Name',
CASE
WHEN (LTRIM(RTRIM(ISNULL(B.LINE1,''))) <> LTRIM(RTRIM(ISNULL(A.LINE1, ''))))
OR (LTRIM(RTRIM(ISNULL(B.LINE2, ''))) <> LTRIM(RTRIM(ISNULL(A.LINE2, ''))))
OR (LTRIM(RTRIM(ISNULL(B.LINE3, ''))) <> LTRIM(RTRIM(ISNULL(A.LINE3, ''))))
THEN 'Title Change'
ELSE ''
END AS 'Client_Name_Update'
FROM #NEW A
INNER JOIN #OLD B
ON A.BRANCH_CD = B.BRANCH_CD
AND A.ACCOUNT_CD = B.ACCOUNT_CD
WHERE (A.[LINE1] <> B.[LINE1])
OR (A.[LINE2] <> B.[LINE2])
OR (A.[LINE3] <> B.[LINE3])
OR (A.[LINE4] <> B.[LINE4])
OR (A.[LINE5] <> B.[LINE5])
OR (A.[LINE6] <> B.[LINE6])
See whether this helps
(Assumed that the requirement is [Old_Address] = [New_Address])
INSERT INTO OPT_INT.mgnNACHG_Results (BRANCH_CD,ACCOUNT_CD,RR_CD,ALT_BRANCH_CD,[OldAddress],[NewAddress],[AddressUpdate],[OldName],[NewName],[NameUpdate])
Select BRANCH_CD, ACCOUNT_CD, ACCOUNT_CD, ALT_BRANCH_CD,
Case When [Old_Address] = [New_Address] then ' blank or 1234 Address as you need ' Else [Old_Address] End as [Old_Address],
Case When [Old_Address] = [New_Address] then ' blank or 1234 Address as you need ' Else [New_Address] End as [New_Address],
[Address_Update], [Client_Old_Name], [Client_New_Name], [Client_Name_Update]
From
(
SELECT A.BRANCH_CD,A.ACCOUNT_CD,A.RR_CD,A.ALT_BRANCH_CD
,LTRIM(RTRIM(ISNULL(B.LINE4,'')))+LTRIM(RTRIM(ISNULL(B.LINE5,'')))+LTRIM(RTRIM(ISNULL(B.LINE6,''))) AS [Old_Address]
,LTRIM(RTRIM(ISNULL(A.LINE4,'')))+LTRIM(RTRIM(ISNULL(A.LINE5,'')))+LTRIM(RTRIM(ISNULL(A.LINE6,''))) AS [New_Address]
,CASE WHEN (LTRIM(RTRIM(ISNULL(B.LINE4,'')))<>LTRIM(RTRIM(ISNULL(A.LINE4,'')))) OR (LTRIM(RTRIM(ISNULL(B.LINE5,'')))<>LTRIM(RTRIM(ISNULL(A.LINE5,'')))) OR (LTRIM(RTRIM(ISNULL(B.LINE6,'')))<>LTRIM(RTRIM(ISNULL(A.LINE6,'')))) THEN 'Address Change' ELSE '' END as [Address_Update]
,LTRIM(RTRIM(ISNULL(B.LINE1,'')))+LTRIM(RTRIM(ISNULL(B.LINE2,'')))+LTRIM(RTRIM(ISNULL(B.LINE3,''))) AS [Client_Old_Name]
,LTRIM(RTRIM(ISNULL(A.LINE1,'')))+LTRIM(RTRIM(ISNULL(A.LINE2,'')))+LTRIM(RTRIM(ISNULL(A.LINE3,''))) AS [Client_New_Name]
,CASE WHEN (LTRIM(RTRIM(ISNULL(B.LINE1,'')))<>LTRIM(RTRIM(ISNULL(A.LINE1,'')))) OR (LTRIM(RTRIM(ISNULL(B.LINE2,'')))<>LTRIM(RTRIM(ISNULL(A.LINE2,'')))) OR (LTRIM(RTRIM(ISNULL(B.LINE3,'')))<>LTRIM(RTRIM(ISNULL(A.LINE3,'')))) THEN 'Title Change' ELSE '' END AS [Client_Name_Update]
FROM #NEW A
INNER JOIN #OLD B ON A.BRANCH_CD=B.BRANCH_CD AND A.ACCOUNT_CD=B.ACCOUNT_CD
WHERE (A.[LINE1]<>B.[LINE1]) OR (A.[LINE2]<>B.[LINE2]) OR (A.[LINE3]<>B.[LINE3]) OR (A.[LINE4]<>B.[LINE4]) OR (A.[LINE5]<>B.[LINE5]) OR (A.[LINE6]<>B.[LINE6])
) AS Q
If in case you need to check whether the 2 data fields containing a particular text, you may use
Case When [Old_Address] = [New_Address] and [Old_Address] = '1234 Address' then ' blank or 1234 Address as you need ' Else [Old_Address] End as [Old_Address],

Filter data by keywords and note where they were found

so i have a query at the moment.
SELECT *
FROM TABLE1
It yields besides other stuff 4 colums which contain text "Textbox1" "Textbox2" "Textbox3" "Textbox4".
I now want to filter this query by keywords in those text boxes. Basically the user can enters 0 to 5 keywords and the rows from TABLE1 should only show if at least one keyword was found in one of the textboxes. Also a new column should be added that for every keyword that was found in a textbox should say which keyword and in which textboxes it was found.
The query itself will later run in SSRS which is where the user puts in the keywords. So the keywords are accasable as varchars. For the query you can use this:
DECLARE #Keyword1 AS VARCHAR(100) = '';
DECLARE #Keyword2 AS VARCHAR(100) = '';
DECLARE #Keyword3 AS VARCHAR(100) = '';
DECLARE #Keyword4 AS VARCHAR(100) = '';
DECLARE #Keyword5 AS VARCHAR(100) = '';
Thanks for any help.
Edit:
This is a very unelegant solution and it's not exactly what you wanted but hopefully close enough.
To improve it, especially if you are goning to extend this to more column/keywords I would consider unpivoting the data, testing each row and then aggregating the result of that back up again.
DECLARE #t TABLE (T1 varchar(100), T2 varchar(100), T3 varchar(100), T4 varchar(100))
INSERT INTO #t VALUES
(1, 11, 203, 30),
(8898, 54452, 1222, 12122)
DECLARE #k1 varchar(100) = '1'
DECLARE #k2 varchar(100) = '22'
SET #k1 = '%' + #k1 + '%'
SET #k2 = '%' + #k2 + '%'
SELECT
*
, CASE WHEN T1 like #k1 THEN '1:T1 ' ELSE '' END +
CASE WHEN T2 like #k1 THEN '1:T2 ' ELSE '' END +
CASE WHEN T3 like #k1 THEN '1:T3 ' ELSE '' END +
CASE WHEN T4 like #k1 THEN '1:T4 ' ELSE '' END +
CASE WHEN T1 like #k2 THEN '2:T1 ' ELSE '' END +
CASE WHEN T2 like #k2 THEN '2:T2 ' ELSE '' END +
CASE WHEN T3 like #k2 THEN '2:T3 ' ELSE '' END +
CASE WHEN T4 like #k2 THEN '2:T4 ' ELSE '' END
As Found
FROM #t
This give the following results.
With the help of Alan Schofield i found the solution that i need.
SELECT * from( Select*,
CASE WHEN Textbox1 like '%'+#Keyword1+'%' AND #Keyword1 <> '' THEN #Keyword1 +', ' ELSE '' END +
CASE WHEN Textbox1 like '%'+#Keyword2+'%' AND #Keyword2 <> '' THEN #Keyword2 +', ' ELSE '' END +
CASE WHEN Textbox1 like '%'+#Keyword3+'%' AND #Keyword3 <> '' THEN #Keyword3 +', ' ELSE '' END +
CASE WHEN Textbox1 like '%'+#Keyword4+'%' AND #Keyword4 <> '' THEN #Keyword4 +', ' ELSE '' END +
CASE WHEN Textbox1 like '%'+#Keyword5+'%' AND #Keyword5 <> '' THEN #Keyword5 +', ' ELSE '' END
As Textbox1found,
CASE WHEN Textbox2 like '%'+#Keyword1+'%' AND #Keyword1 <> '' THEN #Keyword1 +', ' ELSE '' END +
CASE WHEN Textbox2 like '%'+#Keyword2+'%' AND #Keyword2 <> '' THEN #Keyword2 +', ' ELSE '' END +
CASE WHEN Textbox2 like '%'+#Keyword3+'%' AND #Keyword3 <> '' THEN #Keyword3 +', ' ELSE '' END +
CASE WHEN Textbox2 like '%'+#Keyword4+'%' AND #Keyword4 <> '' THEN #Keyword4 +', ' ELSE '' END +
CASE WHEN Textbox2 like '%'+#Keyword5+'%' AND #Keyword5 <> '' THEN #Keyword5 +', ' ELSE '' END
As Textbox2found,
CASE WHEN Textbox3 like '%'+#Keyword1+'%' AND #Keyword1 <> '' THEN #Keyword1 +', ' ELSE '' END +
CASE WHEN Textbox3 like '%'+#Keyword2+'%' AND #Keyword2 <> '' THEN #Keyword2 +', ' ELSE '' END +
CASE WHEN Textbox3 like '%'+#Keyword3+'%' AND #Keyword3 <> '' THEN #Keyword3 +', ' ELSE '' END +
CASE WHEN Textbox3 like '%'+#Keyword4+'%' AND #Keyword4 <> '' THEN #Keyword4 +', ' ELSE '' END +
CASE WHEN Textbox3 like '%'+#Keyword5+'%' AND #Keyword5 <> '' THEN #Keyword5 +', ' ELSE '' END
As Textbox3found,
CASE WHEN Textbox4 like '%'+#Keyword1+'%' AND #Keyword1 <> '' THEN #Keyword1 +', ' ELSE '' END +
CASE WHEN Textbox4 like '%'+#Keyword2+'%' AND #Keyword2 <> '' THEN #Keyword2 +', ' ELSE '' END +
CASE WHEN Textbox4 like '%'+#Keyword3+'%' AND #Keyword3 <> '' THEN #Keyword3 +', ' ELSE '' END +
CASE WHEN Textbox4 like '%'+#Keyword4+'%' AND #Keyword4 <> '' THEN #Keyword4 +', ' ELSE '' END +
CASE WHEN Textbox4 like '%'+#Keyword5+'%' AND #Keyword5 <> '' THEN #Keyword5 +', ' ELSE '' END
AS Textbox4found
FROM Table1)as Result
WHERE Result.Textbox1found <> '' OR Result.Textbox2found <> '' OR Result.Textbox3found <> '' OR Result.Textbox4found <> '' or (#Keyword1='' AND #Keyword2='' AND #Keyword3='' AND #Keyword4='' AND #Keyword5='')
I switched to a Textbox based query which will generat for every textbox a column with the keywords found in that textbox.

RTRIM & LTRIM Function with Case Statement

How do i use the LTRIM & RTRIM with the following SQL? I need to LTRIM and RTRIM all these fields for leading spaces
UPDATE CORE.WeccoPartyAddress
SET AddressElements = CONTROL.TrimChar(
CASE when COALESCE(Address1,'') != '' THEN Address1 + ', ' ELSE '' END +
CASE when COALESCE(Address2,'') != '' THEN Address2 + ', ' ELSE '' END +
CASE when COALESCE(Address3,'') != '' THEN Address3 + ', ' ELSE '' END +
CASE when COALESCE(Town,'') != '' THEN Town + ', ' ELSE '' END +
CASE when COALESCE(County,'') != '' THEN County + ', ' ELSE '' END +
CASE when COALESCE(Postcode,'') != '' THEN Postcode ELSE '' END, ', '
)
Use like below nested:
UPDATE CORE.WeccoPartyAddress SET AddressElements = rtrim(ltrim(CASE when COALESCE(Address1,'') != '' THEN Address1 + ', ' ELSE '' END + CASE when COALESCE(Address2,'') != '' THEN Address2 + ', ' ELSE '' END + CASE when COALESCE(Address3,'') != '' THEN Address3 + ', ' ELSE '' END + CASE when COALESCE(Town,'') != '' THEN Town + ', ' ELSE '' END + CASE when COALESCE(County,'') != '' THEN County + ', ' ELSE '' END + CASE when COALESCE(Postcode,'') != '' THEN Postcode ELSE '' END))
You don't have to use CASE in your statement
UPDATE CORE.WeccoPartyAddress
SET AddressElements = ISNULL( STUFF (
COALESCE( ', ' + LTRIM( RTRIM(Address1) ) , '') +
COALESCE( ', ' + LTRIM( RTRIM(Address1Address2) ) , '') +
COALESCE( ', ' + LTRIM( RTRIM(Address1Address3) ) , '') +
COALESCE( ', ' + LTRIM( RTRIM(Address1Town) ) , '') +
COALESCE( ', ' + LTRIM( RTRIM(Address1County) ) , '') +
COALESCE( ', ' + LTRIM( RTRIM(Address1Postcode) ) , '')
,1
,2
,''
), '')
If any of Address values is not null you will get string like this: ', Address', then using the function STUFF you replace ', ' at the beginning of the string to get 'Address' as the result.
If all values are null the STUFF function will return NULL which will be replaced with '' by ISNULL function.

SQL Server stored proc substitute an empty string if column is empty or null

I need to check to see if a certain coloumn in my stored proc is either empty or null.
This is a snippet of what I have right now:
SELECT * ,
CASE WHEN a.USER IS NULL
THEN b.ROLE
ELSE ISNULL(a.FirstName,'') + ' ' + (ISNULL(a.MiddleName+' ','') + ISNULL(a.LastName,'')
END AS 'CustomerName'
I am checking to see if a.MiddleName is NULL but how do I also check to see if its empty and if its empty to just insert a empty string (no space)?
Thanks
Change to
SELECT
* ,
CASE
WHEN a.USER IS NULL
THEN b.ROLE
ELSE CASE
WHEN ISNULL(a.MiddleName, '') = ''
THEN ISNULL(a.FirstName,'') + ' ' + ISNULL(a.LastName,'')
ELSE ISNULL(a.FirstName,'') + ' ' + a.MiddleName + ' ' + ISNULL(a.LastName,'')
END
END AS 'CustomerName'
Another sollution is:
SELECT * ,
CASE WHEN a.USER IS NULL
THEN b.ROLE
ELSE ISNULL(a.FirstName,'') + replace( ( ' ' + ISNULL(a.MiddleName+' ',' '),' ',' ') + ISNULL(a.LastName,'')
END AS 'CustomerName'

T SQL Conditional String Concatenation

Have a 5 columns of address data. I need to concatenate these fields into a single address with spaces in between the values if they exist. If the column has a null value I should skip it and not enter any space.
select
case
when street_number != '' THEN (cast(street_number as int))
end as street_number,
case
when street_ext != '' then
case
when street_ext = 50 then '1/2'
end
end as street_ext,
case
when street_direct ! = '' then street_direct
end as street_direct,
case
when site_street ! = '' then site_street
end as site_street,
case
when site_address ! = '' then site_address
end as site_address
from parcel
what I'd like to do is have a variable and assign it to the value of the first column street_number, then when I move on to the next column, street_ext, if it isn't null I'd like to check to see if the variable is null and if not, append a space and the value...and so on down the road.
I'm rusty as hell and could use a push in the right direction.
Thanks everyone.
Use the "+" to concatenate strings in TSQL:
SELECT CASE
WHEN LEN(p.street_number) > 0 THEN p.street_number + ' '
ELSE ''
END +
CASE
WHEN p.street_ext = 50 THEN '1/2'
WHEN LEN(p.street_ext) > 0 THEN ''
ELSE p.street_ext
END + ' ' +
CASE
WHEN LEN(p.street_direct) > 0 THEN p.street_direct + ' '
ELSE ''
END +
CASE
WHEN LEN(p.site_street) > 0 THEN p.site_street + ' '
ELSE ''
END +
CASE
WHEN LEN(p.site_address) > 0 THEN p.site_address + ' '
ELSE ''
END AS full_address
FROM PARCEL p
The LEN function returns zero if the string value is NULL, or a zero length string.
Nested isnulls could do what you need. Something like:
SELECT
ISNULL(streetnumber + ' ', '')
+ ISNULL(streetext + ' ', '')
etc
relying on the fact that NULL + ' ' = NULL.
Something along the lines of:
select coalesce(street_number+' ','')+
coalesce(case when street_ext=50 then '1/2' else null end+' ','')+
coalesce(street_direct+' ','')+
coalesce(site_street+' ','')+
coalesce(site_address,'')
from parcel
I have assumed your data types are all varchar or similar for simplicity. If you are OK with removing any double spaces, how about:
rtrim(ltrim(replace(isnull(street_number) + ' '
+ isnull(street_ext) + ' '
+ isnull(street_direct) + ' '
+ isnull(site_street) + ' '
+ isnull(site_address), ' ', ' ')))
First I would declare the seperator as a variable, because customers are notorious for changing these.
I would do this as follows:
DECLARE #AddressSeperator NVARCHAR(5) = ' '
...and then for the column declation, I'd use the following:
, CONCAT
(
(CASE WHEN LEN(p.street_number) > 0 THEN p.street_number + #AddressSeperator ELSE '' END)
, (CASE WHEN p.street_ext = 50 THEN '1/2' + #AddressSeperator WHEN LEN(p.street_ext) > 0 THEN p.street_ext + #AddressSeperator ELSE '' END)
, (CASE WHEN LEN(p.street_direct) > 0 THEN p.street_direct + #AddressSeperator ELSE '' END)
, (CASE WHEN LEN(p.site_street) > 0 THEN p.site_street + #AddressSeperator ELSE '' END)
, ISNULL(p.site_address, '')
) AS [full_address]