Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

double case statement

Posted on 2014-04-07
7
Medium Priority
?
373 Views
Last Modified: 2014-05-07
I have a case statement I am trying to use to prioritize the ordering.
How can I add 2 clauses to the case?

When I do this is works:
CASE WHEN state = @stateAb THEN 0 ELSE 1 END

when I try to do this, I don't get it returned state = only
CASE WHEN state = @stateAb and usedForCity = 0 THEN 0 ELSE 1 END
0
Comment
Question by:rivkamak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 6

Expert Comment

by:Dulton
ID: 39983442
Try nesting the case statements.... what you're trying works in later versions of SQL, not familiar with 2005 enough to say if this was added later. the below should work though.
case when state=@stateAb THEN CASE WHEN usedForCity=0 THEN 0 ELSE 1 END ELSE 1 END

Open in new window

0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39983476
>when I try to do this, I don't get it returned state = only
Offhand the syntax appears correct, assuming state and @stateAb does not contain a NULL value.  If there are NULLs, then you'll have to use the ISNULL function to handle them.   Can you post your full T-SQL?

In case it helps, an example of nesting CASE statements is in SQL Server CASE Solutions, do a find on 'CASE blocks can also be nested within themselves'.

>use to prioritize the ordering.
Also, the section below that deals with CASE in an ORDER BY clause.
0
 

Author Comment

by:rivkamak
ID: 39983499
select  top 100 percent * into #t1 from k4kReviewsForSite2013 ORDER BY
   -- First sort position
 CASE WHEN state = @stateAb THEN 0 ELSE 1 END
 
 select row_number() over( order by (select 1)) rn, *  into #t2
from #T1 

--select * from #t1
--select * from #t2

select * from #T2 where rn between @startNum and @endNum --order by idforstate

Open in new window


when I write to the temporary table  it works the first way.
when when I add the second clause, i lose the correct ordering.
0
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

 
LVL 66

Expert Comment

by:Jim Horn
ID: 39983515
Define 'the correct ordering'.
0
 

Author Comment

by:rivkamak
ID: 39983531
right ordering is only if state still shows up first.
0
 
LVL 32

Expert Comment

by:awking00
ID: 39983641
Perhaps you could post some sample data and your expected results to help clarify your intent for us?
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 39985080
It may depend on nulls in the usedForCity column, try these

-- if usedForCity is null send it downward in ordering
CASE WHEN state = @stateAb and ISNULL(usedForCity,1) = 0 THEN 0 ELSE 1 END

or

-- if usedForCity is null send it upward in ordering
CASE WHEN state = @stateAb and ISNULL(usedForCity,0) = 0 THEN 0 ELSE 1 END


If state can be null also then more may be required, e.g.

-- if usedForCity is null send it downward in ordering
CASE WHEN ISNULL(state,'') = @stateAb and ISNULL(usedForCity,1) = 0 THEN 0 ELSE 1 END

Sample data and expected results would help us help you
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question