Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sherrireid
Yak Posting Veteran
58 Posts |
Posted - 2009-09-18 : 15:34:24
|
Despite numerous attempts and googling and reading the MS BOL, I cannot seem to figure out how to put these two functions together.I have an INT field that I want to be able to be able to use CASE against and say if it is 1 then do X and if it is 2 then do Y. Etc. However because X and Y are char and not INT, it won't let me. Here is the code I am trying to run with the case statement in it. I've removed the various convert and/or CAST wording and left it at just a normal CASE statement - that would - under normal circumstance run if it weren't for the error message I get: "Conversion failed when converting the varchar value 'Visa' to data type int." select [Transaction].Time, Batch.RegisterID, [Transaction].CashierID, CASE when TenderEntry.TenderID = 8 then 'Visa' when TenderEntry.TenderID = 9 then 'M-Card' when TenderEntry.TenderID = 11 then 'Disc' else TenderEntry.TenderID End 'CC-Type', Tenderentry.Amount, TenderEntry.CreditCardApprovalCode AS AuthCodeFROM TenderEntry JOIN [Transaction] ON [Transaction].TransactionNumber = TenderEntry.TransactionNumber JOIN Batch ON Batch.BatchNumber = [Transaction].BatchNumberWHERE TenderEntry.TenderID IN (8,9,11) and [Transaction].Time >= '09/01/2009' and [Transaction].Time <= '09/05/2009'order by [Transaction].Time Thank you in advance for any help you can provide.Regards,SherriSLReidForum NewbieRenton, WA USA |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-09-18 : 16:05:28
|
| It is good practice to have all possible CASEs result in the same datatype. Three of the four possiblities are varchar but your ELSE case is INT. If you don't have a difined type for TenerIDs besides those three then perhaps return 'Other' or NULL. Another alternative for your ELSE case is to cast TenderID as varchar. But even though that will work I don't logically like the idea of returning a mixture of descriptions and IDs in the same column.Be One with the OptimizerTG |
 |
|
|
sherrireid
Yak Posting Veteran
58 Posts |
Posted - 2009-09-18 : 16:34:32
|
quote: Originally posted by TG It is good practice to have all possible CASEs result in the same datatype. Three of the four possiblities are varchar but your ELSE case is INT. If you don't have a difined type for TenerIDs besides those three then perhaps return 'Other' or NULL. Another alternative for your ELSE case is to cast TenderID as varchar. But even though that will work I don't logically like the idea of returning a mixture of descriptions and IDs in the same column.Be One with the OptimizerTG
Thank you for your comments and under normal circumstances in the right universe - maybe I wouldn't have to be doing this. Unfortunately, I don't have a choice. This is a vendor database that I unfortuantely get to write code for and support at the site where I work. The vendor decided that 8 = Visa - not me. I am trying to make the report readable for the user and 8 isn't readable -- Visa is. I can't expect the user to know that a 2 = checks and a 6 = GIft Cards, etc. This is why I am trying to do what I am trying to do.SLReidForum NewbieRenton, WA USA |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-09-18 : 16:38:41
|
| So you're saying that 8 isn't readable but 2 and 6 are? If so then you can go with the CAST alternative I suggested.Be One with the OptimizerTG |
 |
|
|
sherrireid
Yak Posting Veteran
58 Posts |
Posted - 2009-09-18 : 17:04:36
|
quote: Originally posted by TG So you're saying that 8 isn't readable but 2 and 6 are? If so then you can go with the CAST alternative I suggested.Be One with the OptimizerTG
It sounds like I confused the issue. I was just saying that the vendor decided that all of these Credit Card and Cash types were numeric and that these numeric values weren't readable by the end-user because they would have to have a cross-check list that told them what each of these numeric values were when they tried to read the report. This particular report only needs to detail Visa, MC and Discover. I was only using 2/Checks and 6/Gift Cards as a way to further my example in my previous post - but I obviously just confused the issue. Sorry for that.I still need to do what I said in the original post. Also, I just re-read your original post and I see now where I misunderstood you. What you were trying to say to me was that the ELSE in my code was an INT and the other TenderIDs I was trying to make CHAR -- and you were saying to keep them all the same. Since my code ONLY looks for TenderIDs of 8, 9, 11 in the WHERE clause, I can just take the else out as there won't be anything else but the 8,9 and 11's.AND as soon as I took out the ELSE, I was able to run the code and got my 8, 9s and 11s to show up all nice and pretty as Visa, M-Card and Disc!!!! YEAH!!!! Sorry it took me a couple of reads to get what you were saying - and thanks for the help. SherriSLReidForum NewbieRenton, WA USA |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-09-18 : 17:25:12
|
| cool! yeah, I missed that you were limiting to just those three in the where clause so you're right - just removing the ELSE is the way to go.Have a good weekend!Be One with the OptimizerTG |
 |
|
|
sherrireid
Yak Posting Veteran
58 Posts |
Posted - 2009-09-18 : 18:29:14
|
| Thanks! You TOO!!SLReidForum NewbieRenton, WA USA |
 |
|
|
|
|
|
|
|