SQL QUERY with CASE

Hello there,

I have a SQL query where I am trying to replace the code with meaningful name for a column and my SELECT looks like this

SELECT
    Receipt_alias1.transactionNumber as Number,
    (CASE 
     WHEN (Receipt_alias1.receiptType = 14)  THEN 'Received'
     WHEN (Receipt_alias1.receiptType = 18)  THEN 'Deposited'
     WHEN (Receipt_alias1.receiptType = 19)  THEN 'Cleared'
     WHEN (Receipt_alias1.receiptType = 20)  THEN 'Balance'
     WHEN (Receipt_alias1.receiptType = 22)  THEN 'Pending'
     ELSE (Receipt_alias1.receiptType )
 END) as receiptType,

Open in new window


If i remove the ELSE part then my query runs and it shows those meangingful names for the column But the problem is it is skipping the records which are not included in the CASE. I want to show the code in the column if the CASE has not mentioned it. How can I achieve this.

cheers
Zolf
zolfAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anthony CarterCommented:
Not sure which database you are using, but this looks right except that Receipt_alias1.receiptType is numeric and you are using varchar for the results except in the case of the ELSE.

Try casting the value of the column to a string:

SELECT
    Receipt_alias1.transactionNumber as Number,
    (CASE
     WHEN (Receipt_alias1.receiptType = 14)  THEN 'Received'
     WHEN (Receipt_alias1.receiptType = 18)  THEN 'Deposited'
     WHEN (Receipt_alias1.receiptType = 19)  THEN 'Cleared'
     WHEN (Receipt_alias1.receiptType = 20)  THEN 'Balance'
     WHEN (Receipt_alias1.receiptType = 22)  THEN 'Pending'
     ELSE (cast (Receipt_alias1.receiptType as varchar))
 END) as receiptType

Your code makes a new column 'receiptType' with datatype varchar , but the else sends it an INT.  Don't remember if SQL server attempts to automatically cast or not, try and see.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zolfAuthor Commented:
cheers!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.