Solved

Returning a Different Value if a Column Equals a Certain Value

Posted on 2007-11-26
7
166 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
  • 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

808 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