Solved

Returning a Different Value if a Column Equals a Certain Value

Posted on 2007-11-26
7
167 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
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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
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 backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

726 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