473,387 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Trying to get a case statement to work correctly

219 100+
I'm trying to test some logic I want to include in a SQL query I have.
I want to be able to check against a group of values when a user sets a parameter value (location). I tried the query below but get no results, and I know why. The number values I have below need to have quotes around each number, but I can't do that because then the sql statement becomes invalid and I get an error:

Server: Msg 170, Level 15, State 1, Line 6
Line 6: Incorrect syntax near ','.

Expand|Select|Wrap|Line Numbers
  1. DECLARE @location as char(1)
  2. SET @location = 'B'
  3. select * 
  4. from smsdss.encv f
  5. where cast(f.hpohierhpoobjid as varchar(25)) IN 
  6.     (case when @location = 'B' then ('1004088425,1002573976,1003204285')
  7.     else null
  8.     end)
  9.  
Any help would be appreciated.
May 12 '09 #1
4 1603
dmorand
219 100+
I tried to see if this would work, but no luck

Expand|Select|Wrap|Line Numbers
  1. DECLARE @location as char(1)
  2. SET @location = 'B'
  3. DECLARE @value as varchar(255)
  4. SET @value = '''1004088425'',''1002573976'',''1003204285'''
  5. select @value
  6. select top 100 * 
  7. from smsdss.encv f
  8. where cast(f.hpohierhpoobjid as varchar(10)) IN 
  9.     (case when @location = 'B' then (@value)
  10.     end)
  11.  
May 12 '09 #2
ck9663
2,878 Expert 2GB
You need to put a quote on each...

case when @location = 'B' then ('1004088425','1002573976','1003204285')

or not convert your f.hpohierhpoobjid and just use

case when @location = 'B' then (1004088425,1002573976,1003204285)

your NULL however might have a problem....

read this

Good luck


--- CK
May 12 '09 #3
dmorand
219 100+
I tried this but get an error (Line 6: Incorrect syntax near ','.) :

Expand|Select|Wrap|Line Numbers
  1. DECLARE @location as char(1)
  2. SET @location = 'B'
  3. select top 100 * 
  4. from smsdss.encv f
  5. where f.hpohierhpoobjid IN 
  6.     (case when @location = 'B' then (1004088425,1002573976,1003204285)
  7.     end)
  8.  
May 12 '09 #4
ck9663
2,878 Expert 2GB
why not just include the @location check on the where clause

something like...

Expand|Select|Wrap|Line Numbers
  1. where f.hpohierhpoobjid IN  (1004088425,1002573976,1003204285)
  2.  and  @location = 'B'
  3.  
--- CK
May 13 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Alan Silver | last post by:
Hello, I have an ASP that takes a connection string and SQL statement in the querystring and is supposed to return the XML representation of the recordset to the Response stream (don't worry,...
3
by: Matik | last post by:
Hello all, I belive, my problem is probably very easy to solve, but still, I cannot find solution: declare @i int declare @z int create table bubusilala (
6
by: ryan.mclean | last post by:
Hi all, first, let me preface this by saying that I am very new to sql server, coming from oracle. Here is my problem: I would like to have a case statement (similar to decode in oracle) that...
3
by: Richard Fritzler | last post by:
I was given the task of designing a complete web based document prep system. In simplest terms (using a msword explanation) create a database of merge fields, and a library of templates. Allow the...
7
by: Shapper | last post by:
Hello, I have a "Select Case MyVar" in which I define the values of an Array according to the value of MyVar. I need to use the Array Values in a Loop after End Select. It seems the Array is...
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
7
by: Christopher Nelson | last post by:
In Bourne shell, you can do: case ($x) in foo*) ;; *bar) ;; esac so that the first case matches any string starting with "foo", the
7
by: Kurt | last post by:
Hi. I have a c++ project that is basically a set of implementation hiding classes, so that we can convert a static lib with a lot of dependancies into a dynamic lib with less dependancies, so that...
1
by: Zak | last post by:
Hello, I need some help trying to grab the data selected from a checkboxlist. The checklist is acting like a menu. The first selection is accepted but not the multiple ones. For example is the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.