473,418 Members | 5,105 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Convert NULL to blank or if not NULL, Remove Commas from string

I am trying to create a query such that when pulling data from a table, then

If the result is "NULL", replace "NULL" in the column with a "blank"...OR... if the result is a string (not NULL), strip all commas from the resulting string and show the result in the column (Example: showing "ABC, Inc." as "ABC Inc.")...AND... label the column with a heading.

So far, I have totally failed in attempts to write this line of my query...

Many Thanks!
Frustrated Newbie
May 18 '10 #1

✓ answered by cbjones

@Jim Doherty
EUREKA! Thanks for the ideas! Here is the winner in the clubhouse; it first determines if the content is NULL and, if so, suppresses displaying NULL, and if not, removes the commas from the existing string, if any, and replaces the commas with a "-" {dash}:

(CASE WHEN I.MailAddress2 is NULL THEN IsNull(I.MailAddress2, '') ELSE REPLACE(I.MailAddress2, ',','-') END) as [Insured Address 2]

Thank you thank you thank you!

7 30395
Jim Doherty
897 Expert 512MB
@cbjones
Look at case statement useage typically

Expand|Select|Wrap|Line Numbers
  1. SELECT YourColumnName,CASE WHEN ColumnName Is Null THEN 'blank' ELSE replace(YourColumnName,',','') END AS MyColumn from dbo.YourTableName
I know you did not want specifically the word 'blank' but you get the idea just remove and leave single quotes instead

I know some query designers GUI interfaces dont support case statement useage Access ADP project files for one, but then you just create the view on the server and call the view. Case statement is flexible as you can see you can add many arguments to it.
May 19 '10 #2
Many thanks...here's what I did:

SELECT MailAddress2,CASE WHEN MailAddress2 Is Null THEN '' ELSE replace(MailAddress2,',',' ') END AS [Insured Address 2] from Insured

and I got two columns. What am I doing wrong?

MailAddress2 Insured Address 2
Suite C10 Suite C10
NULL
NULL
NULL
NULL
NULL
NULL
NULL
1020 Hidden Harbour Dr. C-1 1020 Hidden Harbour Dr. C-1
NULL
NULL
May 19 '10 #3
Jim Doherty
897 Expert 512MB
@cbjones
You are doing nothing wrong. You asked for two columns of data in your SELECT portion one being the data column and the other being the derived from a calculation Case statement. If you dont wantt the first column just remove it

Expand|Select|Wrap|Line Numbers
  1. SELECT CASE WHEN MailAddress2 Is Null THEN '' ELSE replace(MailAddress2,',',' ') END AS [Insured Address 2] from Insured
May 19 '10 #4
@Jim Doherty
Manually remove it? I can do that if need be. Thanks Jim.
May 19 '10 #5
Jim Doherty
897 Expert 512MB
Just manually change the SQL to match my last post. Your MailAddress2 data will remain in your table as it should do of course. The SQL Case statement is merely testing that data and returning a calculated column ONLY if you understand me
May 20 '10 #6
@Jim Doherty
EUREKA! Thanks for the ideas! Here is the winner in the clubhouse; it first determines if the content is NULL and, if so, suppresses displaying NULL, and if not, removes the commas from the existing string, if any, and replaces the commas with a "-" {dash}:

(CASE WHEN I.MailAddress2 is NULL THEN IsNull(I.MailAddress2, '') ELSE REPLACE(I.MailAddress2, ',','-') END) as [Insured Address 2]

Thank you thank you thank you!
May 21 '10 #7
Jim Doherty
897 Expert 512MB
@cbjones
Hahahah so pleased you conquered it. Well done! :)
May 21 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Tilmann | last post by:
I'm currently using UDB 7.2 on WIN 2000 and also DB2 Version 7 on z/OS. I have one table with a column defined as NOT NULL. I had to change this column from NOT NULL to NULL. Is there any better...
0
by: Dana | last post by:
I am using the XMLTextWriter to build an XML string and pass it to the XMLDocument. When I get the data from SQL Server, some of the values passed to the XML are NULL in the database. When I try...
3
by: Mark Kamoski | last post by:
Hi-- What is the difference between Convert.ToString(obj) and CType(obj, String)? (Assume obj is a variable of type Object.) Please advise. Thank you.
6
by: young_leaf | last post by:
Using STL how do I convert a variable to a binary string but with special case which for example BYTE x, i need only the first 3 bits of this byte, is that possible? to be more specific, from...
13
by: Pep | last post by:
I have to interface to an older library that uses strings and there is no alternative. I need to pass a string that is padded with null bytes. So how can I append these null bytes to the...
7
by: Manuel | last post by:
Hi, I need to convert a text file in a string or char*. exists some function that does it? or how can i do it? Thanks, bye
10
by: satishrajana | last post by:
Hi, My SQL returns a NULL in a datefield if there is no date in that field. If there is a NULL in this column, I want to replace it with spaces in my SELECT statement when I am selecting these...
7
by: santoshsri | last post by:
Hello all, I am entangled in a difficult situation. I wish to remove commas in a string ( for example : 1,20,000 ) that is coming as an output from another system. I will have to process the...
1
by: jrlittle86 | last post by:
I know I'm overlooking something very simple but How can I check if the text within an XML tag is blank? The code below works fine as long as there are values within an XML node (For example the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.