Solved

SQL case if field has a space?

Posted on 2009-04-09
5
226 Views
Last Modified: 2012-05-06
Hi. The code below works. The problem that I have is that there are some parts that have a space in the Revision_ID field. If there is a space I would like it to put REV. - in the field. How can I use a case to filter these?
CASE(REVISION_ID)
 

              WHEN NULL THEN ' REV. -'
 

	      ELSE ' REV. ' + REVISION_ID

          

              END

Open in new window

0
Comment
Question by:D4430
  • 2
  • 2
5 Comments
 
LVL 25

Accepted Solution

by:
reb73 earned 250 total points
Comment Utility
Like this?

CASE(REVISION_ID)
               WHEN NULL THEN ' REV. -'
              WHEN ' ' THEN ' REV. -'
              ELSE ' REV. ' + REVISION_ID
END
0
 
LVL 39

Assisted Solution

by:BrandonGalderisi
BrandonGalderisi earned 250 total points
Comment Utility
' REV. ' + CASE when REVISION_ID is null then '-'
                     when ltrim(revision_id) = '' then '-'
                     else ltrim(str(revision_id))
             end


if revision_id is a NOT an int or other number field, change this:
else ltrim(str(revision_id))

to:
else revision_id
0
 

Author Comment

by:D4430
Comment Utility
Thanks it is now working when there is a space. I still need to refine it because if Revision_ID is null then the result is Null also. Any ideas?
0
 
LVL 25

Expert Comment

by:reb73
Comment Utility
I believe you tried my solution, which won't quite work as WHEN NULL doesn't quite work as expected..

Try Brandon's solution (24110103) which should work..
0
 

Author Closing Comment

by:D4430
Comment Utility
Thanks for your help today.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now