Solved

double case statement

Posted on 2014-04-07
7
366 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
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 65

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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 65

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 48

Accepted Solution

by:
PortletPaul earned 500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

830 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