Author |
Topic |
cardgunner
326 Posts |
Posted - 2011-12-23 : 09:28:01
|
I have an isue with the following where cluasedeclare @paid intset @paid=1select *from tmpwhere case when @paid=1 then '' else (cast(t_balc as decimal (9,2))!=0) end It errors saying "Incorrect syntax near '!'"it does work when the where clause is where (cast(t_balc as decimal (9,2))!=0)CardGunner |
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2011-12-23 : 09:31:10
|
You can't have an logical operator in the then or else portions of a CASE statement. It has to be just a value (literal, column or expression)What can be done is something like thisWHERE (case when @paid=1 then '' else cast(t_balc as decimal (9,2)) end) != 0--Gail ShawSQL Server MVP |
 |
|
X002548
Not Just a Number
15586 Posts |
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2011-12-23 : 10:14:29
|
Sure you can. SQL will cast string to int and the '' will become 0.DECLARE @test INT = 1;SELECT * FROM sys.tables AS t WHERE CASE WHEN @test = 1 THEN '' ELSE object_id END != 0 Returns no rows because the '' is cast to an int becoming 0. So that expression reduces to WHERE 0 != 0, which is false for all rows. Change the variable to something else and you get all rows.--Gail ShawSQL Server MVP |
 |
|
cardgunner
326 Posts |
Posted - 2011-12-23 : 10:15:54
|
where cast((case when @paid=1 then '' else facr200.t_balc end) as decimal(9,2)) != 0 Looks like it might work. Need to test the results.Thanks and Merry Christmas.CardGunner |
 |
|
X002548
Not Just a Number
15586 Posts |
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2011-12-23 : 10:24:18
|
No, it's not good practice, but it does work.--Gail ShawSQL Server MVP |
 |
|
cardgunner
326 Posts |
Posted - 2011-12-23 : 10:26:59
|
Correction it is not returning all records when variablle is 1CardGunner |
 |
|
cardgunner
326 Posts |
Posted - 2011-12-23 : 10:33:21
|
Hold on i got messed up. This is not working.I want it to show all records if the variable is 1. If the variable is anything other then 1 show the records where t_balc is not 0.CardGunner |
 |
|
X002548
Not Just a Number
15586 Posts |
|
cardgunner
326 Posts |
Posted - 2011-12-23 : 11:09:11
|
Well I'm using the variable as a toggle of sorts. I want to see the records where there is a balance then see all records. Depending on which, i would set the variable.Ultimately I'm taking this and making a report.Foe example I have to see what still needs to be paid. I can run query. Now I want to see what was paid and what is current and what was wrote off. I would like to run this from the same report using the same query. I was told early on to do as much work as you can in the sql statement vs relying on reporting filters.CardGunner |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-23 : 11:13:59
|
quote: Originally posted by cardgunner Hold on i got messed up. This is not working.I want it to show all records if the variable is 1. If the variable is anything other then 1 show the records where t_balc is not 0.CardGunner
that will translate in sql toSELECT * FROM tmp WHERE (t_balc <> 0OR @variable = 1) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
X002548
Not Just a Number
15586 Posts |
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2011-12-23 : 12:29:38
|
No, Visakhm's query will do the job. I do hope that performance is not a requirement for this query though. This is one of those patterns that devs love because it looks so elegant and DBAs hate because it performs shockingly bad on larger rowcounts.--Gail ShawSQL Server MVP |
 |
|
cardgunner
326 Posts |
Posted - 2011-12-23 : 12:40:54
|
It worked wonderful. And it performs good. At least I'm not waiting for the query to run.CardGunner |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-23 : 13:08:06
|
quote: Originally posted by cardgunner It worked wonderful. And it performs good. At least I'm not waiting for the query to run.CardGunner
its good solution until results are large as Gail points out ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2011-12-23 : 13:30:26
|
Just to clarify, I'm not waving around theoretical problems that have no impact on reality. I clean that code pattern out of clients' code all the time--Gail ShawSQL Server MVP |
 |
|
|