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
SSCertifiable
SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)

Group: General Forum Members
Points: 7947 Visits: 9971
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
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27817 Visits: 13268
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
SSC Eights!
SSC Eights! (941 reputation)SSC Eights! (941 reputation)SSC Eights! (941 reputation)SSC Eights! (941 reputation)SSC Eights! (941 reputation)SSC Eights! (941 reputation)SSC Eights! (941 reputation)SSC Eights! (941 reputation)

Group: General Forum Members
Points: 941 Visits: 2987
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
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16874 Visits: 19122
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
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16874 Visits: 19122
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