|
-
Feb 13th, 2012, 02:02 PM
#1
Thread Starter
PowerPoster
[RESOLVED] SQL Server String Concatenation with Possible Nulls
Here's my query so far:
Code:
SELECT
ISNULL(tblHealthCareProviders.HCPPrefix, '') +
tblHealthCareProviders.HCPFName +
' ' +
tblHealthCareProviders.HCPLName +
ISNULL(tblHealthCareProviders.HCPSuffix, ''), etc
What I am doing is creating a single name field out of four possible data in a table: prefix (Dr., Mrs., etc) first name, last name and suffix (Jr., Sr., etc). This query is too simple because what I need to do is: if there is a suffix, append a comma and a space to last name and then append the suffix, otherwise just leave last name alone.
So I would get John Berry, Jr. instead of John BerryJr which is what the query above is giving me.
Do I have to break this out and code something more complex than ISNULL can handle?
Thanks.
There are 10 kinds of people in this world. Those who understand binary, and those who don't.
-
Feb 13th, 2012, 02:14 PM
#2
Re: SQL Server String Concatenation with Possible Nulls
case....
Code:
select
case when tblHealthCareProviders.HCPPrefix is not null then tblHealthCareProviders.HCPPrefix + ' ' else '' end +
tblHealthCareProviders.HCPFName +
' ' +
tblHealthCareProviders.HCPLName +
case when tblHealthCareProviders.HCPSuffix is not null then ', ' + tblHealthCareProviders.HCPSuffix else '' end as FULLNAME
....
-tg
-
Feb 13th, 2012, 02:15 PM
#3
Re: SQL Server String Concatenation with Possible Nulls
I've done this in a UDF
Code:
CREATE FUNCTION dbo.GetFullName_F (@LastName as varchar(35),@FirstName as varchar(20),@MiddleName as varchar(20),@Suffix as varchar(10),@Opt as int)
RETURNS varchar(100) AS
BEGIN
Declare @WN varchar(100)
Set @WN=IsNull(@LastName,'')+', '+IsNull(@FirstName,'')
+Case When IsNull(@MiddleName,'')<>'' Then ' '+@MiddleName Else '' End
+Case When IsNull(@Suffix,'')<>'' Then ', '+@Suffix Else '' End
RETURN @WN
END
-
Feb 13th, 2012, 02:18 PM
#4
Re: SQL Server String Concatenation with Possible Nulls
Only thing missing is the title/prefix, but that's easily added in...
-tg
-
Feb 13th, 2012, 02:21 PM
#5
Re: SQL Server String Concatenation with Possible Nulls
This won't work
tblHealthCareProviders.HCPSuffix is not null
if the field contains blanks...
-
Feb 13th, 2012, 02:24 PM
#6
Thread Starter
PowerPoster
Re: SQL Server String Concatenation with Possible Nulls
 Originally Posted by szlamany
This won't work
tblHealthCareProviders.HCPSuffix is not null
if the field contains blanks...
I cannot test that as the field does not contain blanks, but that does not mean it won't. So thanks for catching that.
There are 10 kinds of people in this world. Those who understand binary, and those who don't.
-
Feb 13th, 2012, 02:27 PM
#7
Re: SQL Server String Concatenation with Possible Nulls
I only use FIELD IS NULL or FIELD IS NOT NULL when testing for date columns
Otherwise I feel safer using
IsNull(FIELD,'')<>''
-
Feb 13th, 2012, 02:33 PM
#8
Thread Starter
PowerPoster
Re: SQL Server String Concatenation with Possible Nulls
Before I saw szlamany's post, I had worked up to this:
Code:
SELECT Name =
CASE
WHEN HCPSuffix IS NULL OR HCPSuffix = '' THEN
ISNULL(tblHealthCareProviders.HCPPrefix, '') +
tblHealthCareProviders.HCPFName +
' ' +
tblHealthCareProviders.HCPLName
ELSE
ISNULL(tblHealthCareProviders.HCPPrefix, '') +
tblHealthCareProviders.HCPFName +
' ' +
tblHealthCareProviders.HCPLName +
', ' + tblHealthCareProviders.HCPSuffix
END
FROM tblHealthCareProviders
to take care of blanks in the suffix field.
I should modify this to IsNull(FIELD,'')<>" - yes?
There are 10 kinds of people in this world. Those who understand binary, and those who don't.
-
Feb 13th, 2012, 02:35 PM
#9
Re: SQL Server String Concatenation with Possible Nulls
depends on the context... I have several places where blank and NULL are two very distinctly different meanings ... NULL being what it is supposed to mean: unknown... while a blank value is a valid value and needs to be treated as such.
-tg
-
Feb 13th, 2012, 02:36 PM
#10
Re: SQL Server String Concatenation with Possible Nulls
 Originally Posted by MMock
...I should modify this to IsNull(FIELD,'')<>" - yes?
I would for this specific situation...
-
Feb 13th, 2012, 02:40 PM
#11
Thread Starter
PowerPoster
Re: SQL Server String Concatenation with Possible Nulls
But in this case, blank and NULL I believe would both signify "there is no suffix". And like I said, right now suffix is never blank. and I just saw that there's close to 500,000 rows in the table. That's a pretty good sampling!
There are 10 kinds of people in this world. Those who understand binary, and those who don't.
-
Feb 13th, 2012, 02:47 PM
#12
Re: SQL Server String Concatenation with Possible Nulls
I am a bit of a purist - and I just don't like this code
HCPSuffix IS NULL OR HCPSuffix = ''
for one single reason.
If a field can possibly contain nulls then it should never - imo - be used in an = or <> situation without IsNull() function.
Null can NEVER equal anything.
Granted your OR statement protects you - but I still can't get around the invalid statement of HCPSuffix = ''.
What if someone comes along later and makes the FORMULA more complex - adding some more OR's - or simply removes the HCPSuffix is null?
The only reason the HCPSuffix = '' works is because you have "sister-code" sitting next to it protecting it from itself.
My two cents!
btw - you still working in CT?
-
Feb 13th, 2012, 02:52 PM
#13
Thread Starter
PowerPoster
Re: SQL Server String Concatenation with Possible Nulls
I appreciate your insistence. And the fact is, we are writing this code for a client and then they are buying the source for maintenance on their own (not usually how we do things here) so it really should be perfect.
Yes - Stratford.
There are 10 kinds of people in this world. Those who understand binary, and those who don't.
-
Feb 13th, 2012, 02:54 PM
#14
Re: SQL Server String Concatenation with Possible Nulls
Shelton - wow - small world - and I do health care software for insurance companies - how coincidental!
-
Feb 13th, 2012, 02:57 PM
#15
Thread Starter
PowerPoster
Re: SQL Server String Concatenation with Possible Nulls
That is funny! Before, I was in West Hartford then Bloomfield. I've been here a year and it's permanent so I hope to stay a long time. This just happens to be healthcare - TLBB. We do a variety of sites, like Hilton Hotels, Owens Corning, Invisalign (braces), etc.
There are 10 kinds of people in this world. Those who understand binary, and those who don't.
-
Feb 13th, 2012, 03:00 PM
#16
Re: SQL Server String Concatenation with Possible Nulls
Health care is just a small part of what I do - but mostly work for not-for-profit like firms - unions, school districts - towns and such.
TLBB? bb as in blue cross/blue shield?
-
Feb 13th, 2012, 03:01 PM
#17
Thread Starter
PowerPoster
Re: SQL Server String Concatenation with Possible Nulls
"The Little Blue Book". It's a directory of practices and providers. I can't remember where the client is, but they are in CT too.
There are 10 kinds of people in this world. Those who understand binary, and those who don't.
-
Feb 13th, 2012, 03:02 PM
#18
Thread Starter
PowerPoster
Re: SQL Server String Concatenation with Possible Nulls
Now I've probably said too much and will get fired for breach of confidentiality...
There are 10 kinds of people in this world. Those who understand binary, and those who don't.
-
Feb 13th, 2012, 03:05 PM
#19
Re: SQL Server String Concatenation with Possible Nulls
Remove it if you feel you need to - the google spiders will find it in the next few minutes!!
-
Feb 13th, 2012, 03:09 PM
#20
Thread Starter
PowerPoster
Re: SQL Server String Concatenation with Possible Nulls
Are you joking? Because I was. But I am learning to be paranoid and am getting better at it. How do you remove a post?
There are 10 kinds of people in this world. Those who understand binary, and those who don't.
-
Feb 13th, 2012, 03:35 PM
#21
Thread Starter
PowerPoster
Re: SQL Server String Concatenation with Possible Nulls
Actually, I just googled it, and TLBB is an acronym for many things, and The Little Blue Book also has several definitions. Wikipedia has a completely different description of it.
Anyway, this is what I ended up with:
Code:
SELECT
CASE
-- Build "Name" out of the prefix, first name, last name and suffix, checking for nulls in the prefix and suffix parts.
-- Note, this: IsNull(FIELD,'')<>'' is a safer way of checking for nulls in FIELD than a direct ISNULL test.
WHEN ISNULL(tblHealthCareProviders.HCPPrefix,'')<>'' THEN
tblHealthCareProviders.HCPPrefix + ' '
ELSE ''
END
+ tblHealthCareProviders.HCPFName
+ ' '
+ tblHealthCareProviders.HCPLName
+
CASE
WHEN ISNULL(tblHealthCareProviders.HCPSuffix,'')<>'' THEN
', ' + tblHealthCareProviders.HCPSuffix
ELSE ''
END
AS Name,
etc (omitted, to protect the innocent)
Thanks.
There are 10 kinds of people in this world. Those who understand binary, and those who don't.
-
Feb 13th, 2012, 03:54 PM
#22
Re: [RESOLVED] SQL Server String Concatenation with Possible Nulls
"but mostly work for not-for-profit like firms " - Crap! Srsly? I need to stop helping you then.... that's our target too...
-tg
-
Feb 13th, 2012, 04:37 PM
#23
Re: [RESOLVED] SQL Server String Concatenation with Possible Nulls
@tg - I tried to PM you - I guess u don't take them !
PM me with "What company do you work for?"
-
Feb 14th, 2012, 07:52 AM
#24
Thread Starter
PowerPoster
Re: [RESOLVED] SQL Server String Concatenation with Possible Nulls
You guys - whatever happened to good, old-fashioned email???
There are 10 kinds of people in this world. Those who understand binary, and those who don't.
-
Feb 14th, 2012, 08:46 AM
#25
Re: [RESOLVED] SQL Server String Concatenation with Possible Nulls
I got 143 spam emails yesterday alone - these are ones that my COMCAST BUSINESS CLASS provider and OUTLOOK didn't already put to junk mail...
I try to limit the use of my email as much as possible - just to my actual clients!
-
Feb 14th, 2012, 08:50 AM
#26
Thread Starter
PowerPoster
Re: [RESOLVED] SQL Server String Concatenation with Possible Nulls
Oh, I see.
You can use the VBForums mail. That's what I use to email other members here.
There are 10 kinds of people in this world. Those who understand binary, and those who don't.
-
Feb 14th, 2012, 08:58 AM
#27
Re: [RESOLVED] SQL Server String Concatenation with Possible Nulls
Wow - I thought PM was the only forum method - didn't really know about vbforum email...
-
Feb 14th, 2012, 09:13 AM
#28
Thread Starter
PowerPoster
Re: [RESOLVED] SQL Server String Concatenation with Possible Nulls
Ha - I just sent you one.
There are 10 kinds of people in this world. Those who understand binary, and those who don't.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|