Solved

Returning a Different Value if a Column Equals a Certain Value

Posted on 2007-11-26
7
168 Views
Last Modified: 2010-03-19
Hi,

I have a Stored Procedure in MS SQL Server 2005 that looks like this:

CREATE PROCEDURE spGetHeadlineNewsDetail AS
BEGIN
      SELECT ArticleID, Title, ArticleOrder
      FROM NewsArticle
      ORDER BY ArticleOrder ASC
END
GO

What I'd like is to have this query substitute a different value (Say, a dash) for ArticleOrder if ArticleOrder = 5. In other words, instead of this result:

1 Some Title 3
2 Another Title 5
3 Third Title 2

I want this result:

1 Some Title 3
2 Another Title -
3 Third Title 2

Notice how the Order number 5 was changed to a dash, but the other Order numbers still returned correctly.

What's the tSQL code I need to use to accomplish this?

Thanks in advance.
0
Comment
Question by:CMES-IT
[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
  • 3
  • 2
  • 2
7 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 300 total points
ID: 20350462
CREATE PROCEDURE spGetHeadlineNewsDetail AS
BEGIN
      SELECT ArticleID, Title, case when ArticleOrder = 5 then '-' else cast (ArticleOrder as varchar(10)) end ArticleOrder
      FROM NewsArticle
      ORDER BY ArticleOrder ASC
END
0
 

Author Comment

by:CMES-IT
ID: 20350494
angelIII -

Thanks, that SQL works great returning the dash value when ArticleOrder is 5, but it kills the OrderBy part of the SQL clause. Is there any way to retain the OrderBy part? Right now, the dashes are appearing ahead of, say, the number 1, but those columns are actually 5, and should come after 1.
0
 
LVL 6

Assisted Solution

by:messen1975
messen1975 earned 200 total points
ID: 20350605
CREATE PROCEDURE spGetHeadlineNewsDetail AS
BEGIN
      SELECT ArticleID, Title, case when ArticleOrder = 5 then '-' else cast (ArticleOrder as varchar(10)) end ArticleOrder
      FROM NewsArticle
      ORDER BY NewsArticle.ArticleOrder ASC
END
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20350627
CREATE PROCEDURE spGetHeadlineNewsDetail AS
BEGIN
      SELECT ArticleID, Title, case when ArticleOrder = 5 then '-' else cast (ArticleOrder as varchar(10)) end ArticleOrder
      FROM NewsArticle
      ORDER BY NewsArticle.ArticleOrder ASC
END
0
 

Author Comment

by:CMES-IT
ID: 20350634
Ah, so simple! Why didn't I think of that! ;)

Great, thanks a bunch messen1975.
0
 

Author Comment

by:CMES-IT
ID: 20350648
Everything's working now, thanks Angel and Messen.
0
 
LVL 6

Expert Comment

by:messen1975
ID: 20350656
No problem  :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

688 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