conditional clause in Claudera - sql

I am trying to get an if, then clause working in cloudera - along the lines:
if (appointment_purpose = '') and
appointment_purpose2 = '') and
appointment_type2 = '') and
appointment_type3 = '') and
appointment_type4 = '') and
appointment_type5 = '') and
discuss_other ' ') then
discuss_other = 0
else discuss_other = 1,
How do I get this to work?
If all appointment types and appointment purposes were empty, then discuss_other should also be empty, that is 0 - otherwise discuss_other should be 1

Try this:
SELECT (CASE WHEN appointment_purpose = ''
AND appointment_purpose2 = ''
AND appointment_type2 = ''
AND appointment_type3 = ''
AND appointment_type4 = ''
AND appointment_type5 = ''
AND discuss_other ' '
THEN 0
ELSE 1
) as discuss_other
FROM tableName

In SQL, "empty" typically means NULL. So, I wonder if you really intend one of these constructs:
select (case when appointment_purpose is null and
appointment_purpose2 is null and
appointment_type2 is null and
appointment_type3 is null and
appointment_type4 is null and
appointment_type5 is null and
discuss_other is null
then 0 else 1
end) as discuss_other
Because one of the values has a space, I wonder if you want to handle both spaces and null values:
select (case when (appointment_purpose is null or replace(appointment_purpose, ' ', '') = '') and
(appointment_purpose2 is null or replace(appointment_purpose2, ' ', '') = '') and
(appointment_purpose3 is null or replace(appointment_purpose3, ' ', '') = '') and
(appointment_purpose4 is null or replace(appointment_purpose4, ' ', '') = '') and
(appointment_purpose5 is null or replace(appointment_purpose5, ' ', '') = '') and
(discuss_other is null or replace(discuss_other, ' ', '') = '')
then 0 else 1
end) as discuss_other
Finally, multiple columns in the same table that are acting like an array is usually a bad sign. Normally, this suggests that you want a junction table.

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],

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.

how to show field name if it set to true

I have a table with the 'ContentSubCategory' name.its fields are like below:
Id int
FaName nvarchar(50)
IsSpecial bit
IsPerishable bit
IsDanger bit
now I Want a query that shows list of content names and the attributes that are set to True.
for example:
Row ContentInfo
------------------------------------
1 animal - IsSpecial
2 Human Body - IsSpecial,IsPerishable
3 Danger Goods - IsSpecial,IsDanger
how can I do that?
select cast(FaName as varchar)+' - '+cast(
case when IsSpecial = 1 then 'IsSpecial,' else '' as varchar)+cast(
case when IsPerishable = 1 then 'IsPerishable,' else '' as varchar)+cast(
case when IsDanger = 1 then 'IsDanger' else '' as varchar)
from your_table
Try this and adjust to your own like.
You can also use STUFF():
SELECT
FaName
,ISNULL(
NULLIF(
STUFF(
CASE WHEN IsSpecial = 1 THEN ', IsSpecial' ELSE '' END
+ CASE WHEN IsPerishable =1 THEN ', IsPerishable' ELSE '' END
+ CASE WHEN IsDanger = 1 THEN ', IsDanger' ELSE '' END
, 1, 2, '')
,'')
, 'Unknown')
As Class
FROM TableName

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]

How can I improve this Mailing Address SQL Server SELECT Statement?

How can I format a Mailing Address so that I always push all non-null rows to the top? That is, I want to convert an address from the structure below to a mailing address.
Here is the structure:
[Line1] [varchar](50) NULL,
[Line2] [varchar](50) NULL,
[Line3] [varchar](50) NULL,
[City] [varchar](50) NULL,
[State] [varchar] (2) NULL,
[PostalCode] [varchar](50) NULL,
Here is some sample data:
Line1=
Line2=123 Some Address
Line3=
City=Royal Oak
State=MI
ZIP=45673-2312
Here is what the result should look like (4 distinct or separate fields should be returned):
MailAddress1=123 Some Address
MailAddress2=ROYAL OAK MI 45673-2312
MailAddress3=
MailAddress4=
I am using SQL Server 2005.
Someone wrote this logic in our company and it just seemed to complex (Note: this is not the whole SELECT statement):
,CASE
WHEN eai.Line1 IS NULL OR eai.Line1 = '' THEN
CASE
WHEN eai.Line2 IS NULL OR eai.Line2 = '' THEN
CASE
WHEN eai.Line3 IS NULL OR eai.Line3 = '' THEN ISNULL(LTRIM(RTRIM(eai.City)),'') + ' ' + ISNULL(LTRIM(RTRIM(eai.RegionCode)),'') + ' ' + ISNULL(LTRIM(RTRIM(eai.PostalCode)),'')
ELSE eai.Line3
END
ELSE eai.Line2
END
ELSE eai.Line1
END
,CASE
WHEN eai.Line1 IS NULL OR eai.Line1 = '' THEN
CASE
WHEN eai.Line3 IS NULL OR eai.Line3 = '' THEN ISNULL(LTRIM(RTRIM(eai.City)),'') + ' ' + ISNULL(LTRIM(RTRIM(eai.RegionCode)),'') + ' ' + ISNULL(LTRIM(RTRIM(eai.PostalCode)),'')
ELSE eai.Line3
END
ELSE
CASE
WHEN eai.Line2 IS NULL OR eai.Line2 = '' THEN
CASE
WHEN eai.Line3 IS NULL OR eai.Line3 = '' THEN ISNULL(LTRIM(RTRIM(eai.City)),'') + ' ' + ISNULL(LTRIM(RTRIM(eai.RegionCode)),'') + ' ' + ISNULL(LTRIM(RTRIM(eai.PostalCode)),'')
ELSE eai.Line3
END
ELSE eai.Line2
END
END
,CASE
WHEN eai.Line1 IS NULL OR eai.Line1 = '' THEN
CASE
WHEN eai.Line2 IS NULL OR eai.Line2 = '' THEN NULL
ELSE
CASE
WHEN eai.Line3 IS NULL OR eai.Line3 = '' THEN NULL
ELSE ISNULL(LTRIM(RTRIM(eai.City)),'') + ' ' + ISNULL(LTRIM(RTRIM(eai.RegionCode)),'') + ' ' + ISNULL(LTRIM(RTRIM(eai.PostalCode)),'')
END
END
ELSE
CASE
WHEN eai.Line2 IS NULL OR eai.Line2 = '' THEN
CASE
WHEN eai.Line3 IS NULL OR eai.Line3 = '' THEN NULL
ELSE ISNULL(LTRIM(RTRIM(eai.City)),'') + ' ' + ISNULL(LTRIM(RTRIM(eai.RegionCode)),'') + ' ' + ISNULL(LTRIM(RTRIM(eai.PostalCode)),'')
END
ELSE
CASE
WHEN eai.Line3 IS NULL OR eai.Line3 = '' THEN ISNULL(LTRIM(RTRIM(eai.City)),'') + ' ' + ISNULL(LTRIM(RTRIM(eai.RegionCode)),'') + ' ' + ISNULL(LTRIM(RTRIM(eai.PostalCode)),'')
ELSE eai.Line3
END
END
END
,CASE WHEN eai.Line2 IS NOT NULL AND eai.Line2 <> '' AND eai.Line3 IS NOT NULL AND eai.Line3 <> '' THEN eai.City + ' ' + eai.RegionCode + ' ' + eai.PostalCode ELSE NULL END
The way to do this is with an UNPIVOT. Here is the solution:
With AddrTable as (
Select AddrFld, MailAddr From (
Select Cast(ISNULL([Line1], '') as Varchar(102)) as [A1],
Cast(ISNULL([Line2], '') as Varchar(102)) as [A2],
Cast(ISNULL([Line3], '') as Varchar(102)) as [A3],
Cast(ISNULL(LTRIM(RTRIM(City)),'') + ' ' + ISNULL(LTRIM(RTRIM(RegionCode)),'') + ' ' + ISNULL(LTRIM(RTRIM(PostalCode)),'') as Varchar(102)) as A4
From TableName Where UniqueID=#UniqueID) p
Unpivot (MailAddr For AddrFld in ([A1], [A2], [A3], [A4])) as unpvt)
Select Row_Number() over (Order by (Case Len(MailAddr) When 0 then 1 else 0 end), AddrFld) as RN,
MailAddr From AddrTable
Order By RN
Here's the output:
Address1
Westby WI 55555
-empty line-
-empty line-
Note that I had to use "Varchar(102)" as the field length (unpivot requires that all fields be the same) because your City/Region/Postal can have up to 102 chars in total. Also, note that "#UniqueID" is the identifier for the record whose address you need. This returns four and always four rows containing the data you need for your address.
UPDATE: If you need to return this as a set of four columns rather than four rows, then just plop it into a view and then query the view with a Pivot. I've included the view here for completeness as I had to change the above just a bit when creating the view so the uniqueID field was included and no sort was done (the sort is now done in the query):
Create View AddressRows AS
With AddrTable as (
Select UniqueID, AddrFld, MailAddr From (
Select UniqueID,
Cast(ISNULL([Line1], '') as Varchar(102)) as [A1],
Cast(ISNULL([Line2], '') as Varchar(102)) as [A2],
Cast(ISNULL([Line3], '') as Varchar(102)) as [A3],
Cast(ISNULL(LTRIM(RTRIM(City)),'') + ' ' + ISNULL(LTRIM(RTRIM(RegionCode)),'') + ' ' + ISNULL(LTRIM(RTRIM(PostalCode)),'') as Varchar(102)) as A4
From TableName Where UniqueID=#UniqueID) p
Unpivot (MailAddr For AddrFld in ([A1], [A2], [A3], [A4])) as unpvt)
Select UniqueID,
Row_Number() over (Order by (Case Len(MailAddr) When 0 then 1 else 0 end), AddrFld) as RN,
MailAddr From AddrTable
And then, when you want to pull your matching "row" out, Pivot it back using this SQL (notice that I am querying again using UniqueID):
Select [Addr1], [Addr2], [Addr3], [Addr4] From (
Select Top 4 'Addr' + Cast(Row_Number() over (Order by RN) as Varchar(12)) as AddrCol, -- "Top 4" needed so we can sneak the "Order By" in
MailAddr
From AddressRows Where UniqueID=#UniqueID
) p PIVOT (Max([MailAddr]) for AddrCol in ([Addr1], [Addr2], [Addr3], [Addr4])
) as pvt
This returns:
Addr1 Addr2 Addr3 Addr4
-------------- ------------------ ------------- ------------------
Address1 Westby WI 54667
Here's a three-minutes-invested solution:
DECLARE #address TABLE (
[Line1] [varchar](50) NULL,
[Line2] [varchar](50) NULL,
[Line3] [varchar](50) NULL,
[City] [varchar](50) NULL,
[State] [varchar] (2) NULL,
[PostalCode] [varchar](50) NULL
)
INSERT INTO #address (
[Line1],
[Line2],
[Line3],
[City],
[State],
[PostalCode]
)
VALUES (
NULL,
'123 Some Address',
NULL,
'Royal Oak',
'MI',
'45673-2312'
)
SELECT * FROM #address
SELECT
ISNULL(Line1 + CHAR(13), '')
+ ISNULL(Line2 + CHAR(13), '')
+ ISNULL(Line3 + CHAR(13), '')
+ ISNULL(City + ' ', '')
+ ISNULL([State] + ' ', '')
+ ISNULL(PostalCode, '')
FROM #address
Result:
123 Some Address
Royal Oak MI 45673-2312
Fiddle with the control characters until you get the result you need.
SELECT
LTRIM(RTRIM(LINE1)) + LTRIM(RTRIM(LINE2)) + LTRIM(RTRIM(LINE3)) AS MailAddress1,
LTRIM(RTRIM(CITY)) + ' ' + LTRIM(RTRIM(STATE)) + ' ' + LTRIM(RTRIM(POSTALCODE)) AS MailAddress2
FROM MyTable