6

I have a basic SQL query, starting with:

SELECT top 20 application_id, [name], location_id FROM apps

Now, I would like to finish it so that it does this (written in Pseudocode)

if @lid > 0 then
    WHERE location_id IN (@lid)
else
    WHERE location_id is all values in location_id column

As requested, here is an example

application_id             name               location_id
----------------------------------------------------------
1                          Joe Blogs          33
2                          Sam Smith          234
3                          Jeremy Carr        33

@locid is the results given by the user, for example '33, 234'

If @lid is empty then I'd like it to output all rows for location_id with name and application_id. Otherwise, I'd like it to output all rows in relation to the provided numbers in @lid (standing for location_id.

So, if @lid is 0:

application_id             name               location_id
----------------------------------------------------------
1                          Joe Blogs          33
2                          Sam Smith          234
3                          Jeremy Carr        33

Otherwise, if @lid contains '33'

application_id             name               location_id
----------------------------------------------------------
1                          Joe Blogs          33
3                          Jeremy Carr        33
2
  • Could you please provide some sample data and desired resultset?
    – Quassnoi
    Jun 23, 2009 at 15:15
  • Hopefully this is enough for you. Thanks in advance.
    – Mike B
    Jun 23, 2009 at 15:26

10 Answers 10

3

Try using Case, which serves the purpose of an IIF or a ternary operator. Please check this link http://msdn.microsoft.com/en-us/library/ms181765.aspx

cheers

2
  • I've tried case, but with mixed results. Could you provide some sample code that I could try against my examples?
    – Mike B
    Jun 23, 2009 at 15:18
  • CASE WHEN MyNum < 10 THEN 'Small' ELSE 'Big' END Jun 23, 2009 at 22:12
3

If @locid is a list eg "33, 234" etc, then no solution here will work. However, I guess these were posted before your update with this information.

I assume that because you said this:

@locid is the results given by the user, for example '33, 234'

You can not expand the variable directly so that location_in IN (33, 234). You are actually asking for location_id = '33, 234', which will fail with a CAST conversion, because of datatype precedence.

You have to parse the list first into a table form for use in a JOIN/EXISTS construct. There are several options and Erland covers them all here: Arrays and Lists in SQL Server 2005

1
  • You are correct, and I have already parsed this varchar "list" so that it already checks every value, as a form of using multiple values in one select statement.
    – Mike B
    Jun 24, 2009 at 9:02
3

Assuming @locid & @lid are the same, I'm going to use @locid... The following would work. I've split it up to keep it looking good on SO.

SELECT application_id, [name], location_id 
FROM apps
WHERE
  ( 
    @locid = 0
    OR 
    CHARINDEX
    ( 
      ',' + CAST(location_id AS VARCHAR(50)) + ','
      , 
      ',' + @locid + ','
      , 
      0 
    ) > 0
  )
1
  • how can i trim extra spaces passed in CSVs? Aug 11, 2014 at 10:22
2

See this entry in my blog:

If your @lid is a comma-delimited list of integers, use this:

WITH    cd AS
        (
        SELECT  1 AS first, CHARINDEX(',', @lid, 1) AS next
        UNION ALL
        SELECT  next + 1, CHARINDEX(',', @lid, next + 1)
        FROM    cd
        WHERE   next > 0
        ),
        lid AS
        (
        SELECT  CAST(SUBSTRING(@lid, first, CASE next WHEN 0 THEN LEN(@lid) + 1 ELSE next END - first)AS INT) AS id
        FROM    cd
        )
SELECT  d.*
FROM    (
        SELECT  DISTINCT id
        FROM    lid
        ) l
JOIN    apps a
ON      a.location_id = l.id
        AND @lid <> '0'
UNION ALL
SELECT  *
FROM    apps a
WHERE   @lid = '0'

This is much more efficient than using OR constructs.

3
  • My aim is to return all rows in the location_id column if there is nothing in @lid, so the same kind of results as "SELECT location_id from enquiries" would produce. If there is nothing in @lid I want it to search for every location_id in the database.
    – Mike B
    Jun 23, 2009 at 15:16
  • Deleted my answer and gave you +1 after some benchmarks on the OR construct with some larger datasets. Never quite clicked to me how inefficient they are (only used on smaller data sets). Anyway, I think that the last query is what the OP is looking for.
    – Eric
    Jun 23, 2009 at 15:45
  • @Eric: Thanks. Yes, SQL Server cannot optimize OR's well. However, your query would work nice in MySQL: it would notice that @lid is not a zero and completely remove the predicate from the plan.
    – Quassnoi
    Jun 23, 2009 at 15:54
2
WHERE CHARINDEX(LocationId, @lid) > 1
2
  • You should better elaborate upon your answer. Nov 10, 2012 at 4:54
  • it will give you wrong results when you pass '33' it will return ids 33 and also id 3, "Brett Veenstra" answer below fixes that
    – Abou-Emish
    Aug 21, 2018 at 11:12
1

One way is to split the string on the commas, strip out the spaces and insert the values into a temporary table. Then you can join your query against the temp table.

This is a T-SQL code snippet that splits a comma-separated list and inserts the members into a temporary table. Once you've populated the table you can join against it.

-- This bit splits up a comma separated list of key columns
-- and inserts them in order into a table. 
--
if object_id ('tempdb..#KeyCols') is not null
    drop table #KeyCols

create table #KeyCols (
      ,KeyCol           nvarchar (100)
)

set @comma_pos = 0
set @len = len(@KeyCols)
while @len > 0 begin
    set @comma_pos = charindex(',', @KeyCols)
    if @comma_pos = 0 begin
        set @KeyCol = @KeyCols
        set @KeyCols = ''
    end else begin
        set @KeyCol = left (@KeyCols, @comma_pos - 1)
        set @KeyCols = substring(@KeyCols, 
                                 @comma_pos + 1, 
                                 len (@KeyCols) - @comma_pos)
    end
    insert #KeyCols (KeyCol)
    values (@KeyCol)
    set @len = len (@KeyCols)
end
2
  • +1, a useful technique. Any reason to not go with an in-memory temp table only?
    – CRice
    Jun 3, 2013 at 23:32
  • Not particularly - if I recall correctly the sproc that originally came from stated life on SQL Server 2000. You could use a table variable just as easily on modern versions of SQL Server. Jun 4, 2013 at 8:43
0

You don't really need the ternary operator for this:

SELECT top 20 application_id, [name], location_id
FROM apps
WHERE (@lid > 0 AND location_id IN (@lid)) OR @lid <= 0
0
0

The following should do the trick for you.

DECLARE @lid SMALLINT

SET @lid = 0

SELECT top 20 application_id, [name], location_id

FROM apps

WHERE ((@lid > 0 AND location_id = @lid)

  OR (@lid = 0 AND location_id > @lid))

If @lid = 0 then it will return ALL rows. IF @lid has a particular value, only the row for that @lid value is returned.

0

While it is not exactly a best practice to use 1=1, it does the trick here.

SELECT top 20 application_id, [name], location_id FROM apps
WHERE
  (@lid > 0 and @lid = location_id)
or
  (isnull(@lid, 0) <= 0 and 1=1)
0

You are asking different questions here. This is the answer to your original question (about the ternary operator, but as you can see, you don't need anything like a ternary operator for this):

SELECT top 20 application_id, [name], location_id 
FROM apps
WHERE @lid = 0 OR location_id IN (@lid)

But that was before we knew that @lid was a varchar and could contain different comma separated values.

Well, this (about the csv) is another question which has been asked here before:

Passing an "in" list via stored procedure
T-SQL stored procedure that accepts multiple Id values

1
  • @gbn: this has nothing to do with the question at hand, which is about a ternary operator (which is also not what's needed here, but that's again another discution ;-).
    – fretje
    Jun 24, 2009 at 7:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.