SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Are there alternatives to this SQL logic


Are there alternatives to this SQL logic

Author
Message
Sush Ram
Sush Ram
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 8
Hi!

what ways you could simplify using an explicit “=” statement for each Source value? Are there alternatives, especially when you have 3 Source values that equate to one Eg: 'CREDITNEW' and 'CREDITOLD' equate to one 'CREDIT'.
Is there a more maintainable way we could alter this logic. Can I avoid using the case statement.

SELECT CASE
WHEN RTRIM(Customer.Source) = 'CREDITNEW' THEN 'CREDIT'
WHEN RTRIM(Customer.Source) = 'CREDITOLD' THEN 'CREDIT'
WHEN RTRIM(Customer.Source) = 'TOC' THEN 'CREDIT'
WHEN RTRIM(Customer.Source) = 'DEBIT' THEN 'UC DEBIT'
WHEN RTRIM(Customer.Source) = 'DEBITOLD' THEN 'DEBIT' -- Defined orginally with space
WHEN RTRIM(Customer.Source) = 'DEBITNEW' THEN 'DEBIT' -- Crystal uses the space version for logo
WHEN RTRIM(Customer.Source) = 'TLC' THEN 'DEBIT'
ELSE 'UNDEFINED' -- Default logo
END AS CompanyName, CompanyId, TermsofUse
FROM Customer
Jeffrey Williams 3188
Jeffrey Williams 3188
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19297 Visits: 10042
The only way to get rid of the case expression would be to build a table that contains a cross-map of the values.

Then in your query you would join (outer join) to this table and pull the value you want.

Jeffrey Williams
Problems are opportunities brilliantly disguised as insurmountable obstacles.

How to post questions to get better answers faster
Managing Transaction Logs

Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62802 Visits: 13298
You could use a "simple case" expression (in contrast with the searched case expression you wrote).


SELECT CASE RTRIM(Customer.Source)
WHEN 'CREDITNEW' THEN 'CREDIT'
WHEN 'CREDITOLD' THEN 'CREDIT'
WHEN 'TOC' THEN 'CREDIT'
WHEN 'DEBIT' THEN 'UC DEBIT'
WHEN 'DEBITOLD' THEN 'DEBIT' -- Defined orginally with space
WHEN 'DEBITNEW' THEN 'DEBIT' -- Crystal uses the space version for logo
WHEN 'TLC' THEN 'DEBIT'
ELSE 'UNDEFINED' -- Default logo
END AS CompanyName, CompanyId, TermsofUse
FROM Customer




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
LightVader
LightVader
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1245 Visits: 3038
How about using IN?

SELECT CASE
WHEN RTRIM(Customer.Source) IN ('CREDITNEW','CREDITOLD','TOC') THEN 'CREDIT'
WHEN RTRIM(Customer.Source) IN ('DEBIT') THEN 'UC DEBIT'
WHEN RTRIM(Customer.Source) IN ('DEBITOLD','DEBITNEW','TLC') THEN 'DEBIT' -- Defined orginally with space, Crystal uses the space version for logo
ELSE 'UNDEFINED' -- Default logo
END AS CompanyName, CompanyId, TermsofUse
FROM Customer



Edit: Formatting



The opinions expressed herein are strictly personal and do not necessarily reflect the views or policies of my employer.
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42143 Visits: 19829
You don't need the RTRIM at all. Here's an example.
WITH Customer(Source) AS(
SELECT 'CREDITNEW' UNION ALL
SELECT 'CREDITNEW ' UNION ALL --Spaces added
SELECT 'CREDITNEW ' UNION ALL --Tab added
SELECT 'CREDITOLD' UNION ALL
SELECT 'TOC')
SELECT * ,
CASE
WHEN RTRIM(Customer.Source) IN ('CREDITNEW','CREDITOLD','TOC') THEN 'CREDIT'
WHEN RTRIM(Customer.Source) IN ('DEBIT') THEN 'UC DEBIT'
WHEN RTRIM(Customer.Source) IN ('DEBITOLD','DEBITNEW','TLC') THEN 'DEBIT' -- Defined orginally with space, Crystal uses the space version for logo
ELSE 'UNDEFINED' -- Default logo
END AS CompanyName,
CASE
WHEN Customer.Source IN ('CREDITNEW','CREDITOLD','TOC') THEN 'CREDIT'
WHEN Customer.Source IN ('DEBIT') THEN 'UC DEBIT'
WHEN Customer.Source IN ('DEBITOLD','DEBITNEW','TLC') THEN 'DEBIT' -- Defined orginally with space, Crystal uses the space version for logo
ELSE 'UNDEFINED' -- Default logo
END AS CompanyName
FROM Customer




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42143 Visits: 19829
Jeffrey Williams 3188 (10/22/2013)
The only way to get rid of the case expression would be to build a table that contains a cross-map of the values.

Then in your query you would join (outer join) to this table and pull the value you want.


An example without the need of a physical table. Performance should be basically the same as the number of rows is very small. However, test should be done.
WITH CompanyNames(Source, Name) AS(
SELECT 'CREDITNEW', 'CREDIT' UNION ALL
SELECT 'CREDITOLD' , 'CREDIT' UNION ALL
SELECT 'TOC' , 'CREDIT' UNION ALL
SELECT 'DEBIT' , 'UC DEBIT' UNION ALL
SELECT 'DEBITOLD' , 'DEBIT' UNION ALL -- Defined orginally with space
SELECT 'DEBITNEW' , 'DEBIT' UNION ALL -- Crystal uses the space version for logo
SELECT 'TLC' , 'DEBIT'),
Customer AS(
SELECT 'CREDITNEW' AS Source,
1 AS CompanyID,
'Something' AS TermsofUse
UNION ALL
SELECT 'DOESITMATTER?' AS Source,
2 AS CompanyID,
'Something Else' AS TermsofUse
)
SELECT ISNULL(CAST( n.Name AS varchar(10)), 'UNDEFINED') AS CompanyName,
CompanyId,
TermsofUse
FROM Customer c
LEFT
JOIN CompanyNames n ON c.Source = n.Source




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Sush Ram
Sush Ram
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 8
Thanks for the very good suggestions.

really appreciate it.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search