Spaces when concatenating multiple columns and one column is null - Oracle
I need to concatenate several columns into one, with spaces between each value. The problem is when one value is null, I end up with a double space between two values.
Example
SELECT (FIRST_NAME || ' ' || MIDDLE_NAME || ' ' || LAST_NAME
FROM TABLE_A;
If the middle name happens to be NULL, then I end up with two spaces between the first and last name. Any way to get around this and only have one space when there's a null value?
oracle null concatenation space
add a comment |
I need to concatenate several columns into one, with spaces between each value. The problem is when one value is null, I end up with a double space between two values.
Example
SELECT (FIRST_NAME || ' ' || MIDDLE_NAME || ' ' || LAST_NAME
FROM TABLE_A;
If the middle name happens to be NULL, then I end up with two spaces between the first and last name. Any way to get around this and only have one space when there's a null value?
oracle null concatenation space
add a comment |
I need to concatenate several columns into one, with spaces between each value. The problem is when one value is null, I end up with a double space between two values.
Example
SELECT (FIRST_NAME || ' ' || MIDDLE_NAME || ' ' || LAST_NAME
FROM TABLE_A;
If the middle name happens to be NULL, then I end up with two spaces between the first and last name. Any way to get around this and only have one space when there's a null value?
oracle null concatenation space
I need to concatenate several columns into one, with spaces between each value. The problem is when one value is null, I end up with a double space between two values.
Example
SELECT (FIRST_NAME || ' ' || MIDDLE_NAME || ' ' || LAST_NAME
FROM TABLE_A;
If the middle name happens to be NULL, then I end up with two spaces between the first and last name. Any way to get around this and only have one space when there's a null value?
oracle null concatenation space
oracle null concatenation space
asked Aug 14 '12 at 16:36
dstnrgrsdstnrgrs
33114
33114
add a comment |
add a comment |
10 Answers
10
active
oldest
votes
another option is to use decode :
SELECT decode(FIRST_NAME,'','',FIRST_NAME ||' ') ||
decode(MIDDLE_NAME,'','',MIDDLE_NAME ||' ') || LAST_NAME
FROM TABLE_A;
I ended up using the NVL() method you had posted before. I did figure out that the table had some values that were blank (or so I thought) but actually had a space, so those rows were messing with me
– dstnrgrs
Aug 14 '12 at 18:05
...I take that back. I had to incorporate the decode as in the above. Thanks for the help
– dstnrgrs
Aug 14 '12 at 18:32
Since''is null in Oracle, thenvlcalls here add nothing of value. "If FIRST_NAME is null, return null, otherwise return FIRST_NAME" is the same as "return FIRST_NAME".
– Jeffrey Kemp
Aug 15 '12 at 4:25
Thanks, I've already figured it out(see Tebbe's post)
– Grisha Weintraub
Aug 15 '12 at 4:35
add a comment |
SELECT TRIM(TRIM(FIRST_NAME || ' ' || MIDDLE_NAME) || ' ' || LAST_NAME)
FROM TABLE_A;
3
This one is the most correct, as the accepted approach doesn't suppose that the first two arguments could be NULL.
– g00dy
Jan 7 '15 at 10:26
add a comment |
From the Oracle's documentation:
CONCAT_WS(separator,str1,str2,...)
CONCAT_WS() stands for Concatenate With Separator and is a special
form of CONCAT(). The first argument is the separator for the rest of
the arguments. The separator is added between the strings to be
concatenated. The separator can be a string, as can the rest of the
arguments. If the separator is NULL, the result is NULL.
And the very important comment:
CONCAT_WS() does not skip empty strings. However, it does skip any
NULL values after the separator argument.
So in your case it should be:
CONCAT_WS(',', FIRST_NAME, MIDDLE_NAME, LAST_NAME);
4
Correct me if I'm wrong butCONCAT_WS()is available in MySQL, not Oracle RDBMS.
– Erik Anderson
Mar 16 '16 at 22:54
add a comment |
with indata as
(
select 'John' as first_name, 'W' as middle_name, 'Smith ' as last_name from dual
union
select null as first_name, null as middle_name, 'Adams' as last_name from dual
union
select 'Tom' as first_name, null as middle_name, 'Jefferson' as last_name from dual
)
select
regexp_replace(trim(indata.first_name || ' ' || indata.middle_name || ' ' || indata.last_name), 's{2,}', ' ')
from indata;
ha! just about to post the regexp_replace methodology!
– Harrison
Aug 14 '12 at 17:38
lol beat you to it ;-)
– tbone
Aug 14 '12 at 17:42
add a comment |
You could use RPAD() to add in the space character:
SELECT RPAD(first_name, LENGTH(first_name)+1, ' ')||RPAD(middle_name, LENGTH(middle_name)+1, ' ')||last_name
FROM TABLE_A;
When any of the parameters to RPAD are NULL, the result will be NULL, and in Oracle appending NULL to a string returns the original string.
add a comment |
This is how I typically concatenate several fields and remove whitespace in Oracle:
TRIM(REGEXP_REPLACE(HOUSE_NO || ' ' || PREFIX || ' ' || STREET_NAME || ' ' || STREET_TYPE || ' ' || SUFFIX, ' +', ' '))
- Concatenate all the fields necessary with a space in between each. Empty strings and NULL values will result in a two or more spaces;
- Use a regular expression to change any occurrences of multiple spaces [' +'] to a single space [' '];
- Finally, trim any whitespace at the beginning and/or end from the resulting string.
add a comment |
Yet another option:
SELECT first_name
|| DECODE(middle_name
, NULL, NULL
, ' ' || middle_name)
|| DECODE(last_name
, NULL, NULL
, ' ' || last_name) full_name
FROM table_a
;
if middle_name or last_name is empty string (i.e. '') - it doesn't work properly...
– Grisha Weintraub
Aug 14 '12 at 17:24
1
In Oracle it will; Oracle makes no distinction between the empty string andNULL.
– Tebbe
Aug 14 '12 at 20:14
Wow... I am working with Oracle about a year and didn't know it(worked with MSSQL before). Thanks a lot for your comment it will be useful for me.
– Grisha Weintraub
Aug 15 '12 at 3:32
add a comment |
Or you could simply use the REPLACE function:
with indata as
(select 'John' as first_name, 'W' as middle_name, 'Smith ' as last_name from dual
union
select null as first_name, null as middle_name, 'Adams' as last_name from dual
union
select 'Tom' as first_name, null as middle_name, 'Jefferson' as last_name from dual)
SELECT REPLACE(TRIM(indata.first_name || ' ' || indata.middle_name || ' ' || indata.last_name), ' ', ' ')
FROM indata
(And thanks to @tbone for the example data :-)
add a comment |
I have this work around with example. Hope this helps. Just go to SQL server, select new query and CP following query:
DECLARE @NULL_SAMLES TABLE
(
NS_ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED NOT NULL
,COL_01 VARCHAR(10) NULL
,COL_02 VARCHAR(10) NULL
,COL_03 VARCHAR(10) NULL
,COL_04 VARCHAR(10) NULL
)
INSERT INTO @NULL_SAMLES(COL_01,COL_02,COL_03,COL_04)
VALUES
('A','B','C','D')
,(' ' ,'B','C','D')
,(' ',NULL,'C','D')
,('A','B',NULL,'D')
,('A','B','C',NULL)
,(NULL,'B',NULL,'D')
,(NULL,'B','C',NULL)
,('A',NULL,'C',NULL)
,('A',NULL,NULL,'D')
,('A',NULL,NULL,NULL)
,(NULL,'B',NULL,NULL)
,(NULL,NULL,'C',NULL)
,(NULL,NULL,NULL,'D')
SELECT
NS.COL_01
,NS.COL_02
,NS.COL_03
,NS.COL_04,
Stuff(
Coalesce(', ' + nullif(NS.COL_01, ''), '')
+ Coalesce(', ' + nullif(NS.COL_02, ''), '')
+ Coalesce(', ' + nullif(NS.COL_03, ''), '')
+Coalesce(', ' + nullif(NS.COL_04, ''), '')
, 1, 1, '') AS CONC_COLS
FROM @NULL_SAMLES NS
add a comment |
Don't underestimate the simple power of the CASE statement, which can be concatenated. Here's a self-contained example you can run as-is:
SELECT
CASE WHEN x.FIRST_NAME IS NULL THEN x.FIRST_NAME ELSE x.FIRST_NAME || ' ' END ||
CASE WHEN x.MIDDLE_NAME IS NULL THEN x.MIDDLE_NAME ELSE x.MIDDLE_NAME || ' ' END ||
x.LAST_NAME
FROM (SELECT 'John' AS FIRST_NAME, NULL AS MIDDLE_NAME, 'Doe' AS LAST_NAME FROM DUAL) x;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11956844%2fspaces-when-concatenating-multiple-columns-and-one-column-is-null-oracle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
10 Answers
10
active
oldest
votes
10 Answers
10
active
oldest
votes
active
oldest
votes
active
oldest
votes
another option is to use decode :
SELECT decode(FIRST_NAME,'','',FIRST_NAME ||' ') ||
decode(MIDDLE_NAME,'','',MIDDLE_NAME ||' ') || LAST_NAME
FROM TABLE_A;
I ended up using the NVL() method you had posted before. I did figure out that the table had some values that were blank (or so I thought) but actually had a space, so those rows were messing with me
– dstnrgrs
Aug 14 '12 at 18:05
...I take that back. I had to incorporate the decode as in the above. Thanks for the help
– dstnrgrs
Aug 14 '12 at 18:32
Since''is null in Oracle, thenvlcalls here add nothing of value. "If FIRST_NAME is null, return null, otherwise return FIRST_NAME" is the same as "return FIRST_NAME".
– Jeffrey Kemp
Aug 15 '12 at 4:25
Thanks, I've already figured it out(see Tebbe's post)
– Grisha Weintraub
Aug 15 '12 at 4:35
add a comment |
another option is to use decode :
SELECT decode(FIRST_NAME,'','',FIRST_NAME ||' ') ||
decode(MIDDLE_NAME,'','',MIDDLE_NAME ||' ') || LAST_NAME
FROM TABLE_A;
I ended up using the NVL() method you had posted before. I did figure out that the table had some values that were blank (or so I thought) but actually had a space, so those rows were messing with me
– dstnrgrs
Aug 14 '12 at 18:05
...I take that back. I had to incorporate the decode as in the above. Thanks for the help
– dstnrgrs
Aug 14 '12 at 18:32
Since''is null in Oracle, thenvlcalls here add nothing of value. "If FIRST_NAME is null, return null, otherwise return FIRST_NAME" is the same as "return FIRST_NAME".
– Jeffrey Kemp
Aug 15 '12 at 4:25
Thanks, I've already figured it out(see Tebbe's post)
– Grisha Weintraub
Aug 15 '12 at 4:35
add a comment |
another option is to use decode :
SELECT decode(FIRST_NAME,'','',FIRST_NAME ||' ') ||
decode(MIDDLE_NAME,'','',MIDDLE_NAME ||' ') || LAST_NAME
FROM TABLE_A;
another option is to use decode :
SELECT decode(FIRST_NAME,'','',FIRST_NAME ||' ') ||
decode(MIDDLE_NAME,'','',MIDDLE_NAME ||' ') || LAST_NAME
FROM TABLE_A;
edited Aug 15 '12 at 4:37
answered Aug 14 '12 at 16:49
Grisha WeintraubGrisha Weintraub
6,41311438
6,41311438
I ended up using the NVL() method you had posted before. I did figure out that the table had some values that were blank (or so I thought) but actually had a space, so those rows were messing with me
– dstnrgrs
Aug 14 '12 at 18:05
...I take that back. I had to incorporate the decode as in the above. Thanks for the help
– dstnrgrs
Aug 14 '12 at 18:32
Since''is null in Oracle, thenvlcalls here add nothing of value. "If FIRST_NAME is null, return null, otherwise return FIRST_NAME" is the same as "return FIRST_NAME".
– Jeffrey Kemp
Aug 15 '12 at 4:25
Thanks, I've already figured it out(see Tebbe's post)
– Grisha Weintraub
Aug 15 '12 at 4:35
add a comment |
I ended up using the NVL() method you had posted before. I did figure out that the table had some values that were blank (or so I thought) but actually had a space, so those rows were messing with me
– dstnrgrs
Aug 14 '12 at 18:05
...I take that back. I had to incorporate the decode as in the above. Thanks for the help
– dstnrgrs
Aug 14 '12 at 18:32
Since''is null in Oracle, thenvlcalls here add nothing of value. "If FIRST_NAME is null, return null, otherwise return FIRST_NAME" is the same as "return FIRST_NAME".
– Jeffrey Kemp
Aug 15 '12 at 4:25
Thanks, I've already figured it out(see Tebbe's post)
– Grisha Weintraub
Aug 15 '12 at 4:35
I ended up using the NVL() method you had posted before. I did figure out that the table had some values that were blank (or so I thought) but actually had a space, so those rows were messing with me
– dstnrgrs
Aug 14 '12 at 18:05
I ended up using the NVL() method you had posted before. I did figure out that the table had some values that were blank (or so I thought) but actually had a space, so those rows were messing with me
– dstnrgrs
Aug 14 '12 at 18:05
...I take that back. I had to incorporate the decode as in the above. Thanks for the help
– dstnrgrs
Aug 14 '12 at 18:32
...I take that back. I had to incorporate the decode as in the above. Thanks for the help
– dstnrgrs
Aug 14 '12 at 18:32
Since
'' is null in Oracle, the nvl calls here add nothing of value. "If FIRST_NAME is null, return null, otherwise return FIRST_NAME" is the same as "return FIRST_NAME".– Jeffrey Kemp
Aug 15 '12 at 4:25
Since
'' is null in Oracle, the nvl calls here add nothing of value. "If FIRST_NAME is null, return null, otherwise return FIRST_NAME" is the same as "return FIRST_NAME".– Jeffrey Kemp
Aug 15 '12 at 4:25
Thanks, I've already figured it out(see Tebbe's post)
– Grisha Weintraub
Aug 15 '12 at 4:35
Thanks, I've already figured it out(see Tebbe's post)
– Grisha Weintraub
Aug 15 '12 at 4:35
add a comment |
SELECT TRIM(TRIM(FIRST_NAME || ' ' || MIDDLE_NAME) || ' ' || LAST_NAME)
FROM TABLE_A;
3
This one is the most correct, as the accepted approach doesn't suppose that the first two arguments could be NULL.
– g00dy
Jan 7 '15 at 10:26
add a comment |
SELECT TRIM(TRIM(FIRST_NAME || ' ' || MIDDLE_NAME) || ' ' || LAST_NAME)
FROM TABLE_A;
3
This one is the most correct, as the accepted approach doesn't suppose that the first two arguments could be NULL.
– g00dy
Jan 7 '15 at 10:26
add a comment |
SELECT TRIM(TRIM(FIRST_NAME || ' ' || MIDDLE_NAME) || ' ' || LAST_NAME)
FROM TABLE_A;
SELECT TRIM(TRIM(FIRST_NAME || ' ' || MIDDLE_NAME) || ' ' || LAST_NAME)
FROM TABLE_A;
answered Aug 14 '12 at 16:39
RedFilterRedFilter
134k30242253
134k30242253
3
This one is the most correct, as the accepted approach doesn't suppose that the first two arguments could be NULL.
– g00dy
Jan 7 '15 at 10:26
add a comment |
3
This one is the most correct, as the accepted approach doesn't suppose that the first two arguments could be NULL.
– g00dy
Jan 7 '15 at 10:26
3
3
This one is the most correct, as the accepted approach doesn't suppose that the first two arguments could be NULL.
– g00dy
Jan 7 '15 at 10:26
This one is the most correct, as the accepted approach doesn't suppose that the first two arguments could be NULL.
– g00dy
Jan 7 '15 at 10:26
add a comment |
From the Oracle's documentation:
CONCAT_WS(separator,str1,str2,...)
CONCAT_WS() stands for Concatenate With Separator and is a special
form of CONCAT(). The first argument is the separator for the rest of
the arguments. The separator is added between the strings to be
concatenated. The separator can be a string, as can the rest of the
arguments. If the separator is NULL, the result is NULL.
And the very important comment:
CONCAT_WS() does not skip empty strings. However, it does skip any
NULL values after the separator argument.
So in your case it should be:
CONCAT_WS(',', FIRST_NAME, MIDDLE_NAME, LAST_NAME);
4
Correct me if I'm wrong butCONCAT_WS()is available in MySQL, not Oracle RDBMS.
– Erik Anderson
Mar 16 '16 at 22:54
add a comment |
From the Oracle's documentation:
CONCAT_WS(separator,str1,str2,...)
CONCAT_WS() stands for Concatenate With Separator and is a special
form of CONCAT(). The first argument is the separator for the rest of
the arguments. The separator is added between the strings to be
concatenated. The separator can be a string, as can the rest of the
arguments. If the separator is NULL, the result is NULL.
And the very important comment:
CONCAT_WS() does not skip empty strings. However, it does skip any
NULL values after the separator argument.
So in your case it should be:
CONCAT_WS(',', FIRST_NAME, MIDDLE_NAME, LAST_NAME);
4
Correct me if I'm wrong butCONCAT_WS()is available in MySQL, not Oracle RDBMS.
– Erik Anderson
Mar 16 '16 at 22:54
add a comment |
From the Oracle's documentation:
CONCAT_WS(separator,str1,str2,...)
CONCAT_WS() stands for Concatenate With Separator and is a special
form of CONCAT(). The first argument is the separator for the rest of
the arguments. The separator is added between the strings to be
concatenated. The separator can be a string, as can the rest of the
arguments. If the separator is NULL, the result is NULL.
And the very important comment:
CONCAT_WS() does not skip empty strings. However, it does skip any
NULL values after the separator argument.
So in your case it should be:
CONCAT_WS(',', FIRST_NAME, MIDDLE_NAME, LAST_NAME);
From the Oracle's documentation:
CONCAT_WS(separator,str1,str2,...)
CONCAT_WS() stands for Concatenate With Separator and is a special
form of CONCAT(). The first argument is the separator for the rest of
the arguments. The separator is added between the strings to be
concatenated. The separator can be a string, as can the rest of the
arguments. If the separator is NULL, the result is NULL.
And the very important comment:
CONCAT_WS() does not skip empty strings. However, it does skip any
NULL values after the separator argument.
So in your case it should be:
CONCAT_WS(',', FIRST_NAME, MIDDLE_NAME, LAST_NAME);
answered Feb 24 '14 at 21:26
Piotr DePiotr De
6,80933450
6,80933450
4
Correct me if I'm wrong butCONCAT_WS()is available in MySQL, not Oracle RDBMS.
– Erik Anderson
Mar 16 '16 at 22:54
add a comment |
4
Correct me if I'm wrong butCONCAT_WS()is available in MySQL, not Oracle RDBMS.
– Erik Anderson
Mar 16 '16 at 22:54
4
4
Correct me if I'm wrong but
CONCAT_WS() is available in MySQL, not Oracle RDBMS.– Erik Anderson
Mar 16 '16 at 22:54
Correct me if I'm wrong but
CONCAT_WS() is available in MySQL, not Oracle RDBMS.– Erik Anderson
Mar 16 '16 at 22:54
add a comment |
with indata as
(
select 'John' as first_name, 'W' as middle_name, 'Smith ' as last_name from dual
union
select null as first_name, null as middle_name, 'Adams' as last_name from dual
union
select 'Tom' as first_name, null as middle_name, 'Jefferson' as last_name from dual
)
select
regexp_replace(trim(indata.first_name || ' ' || indata.middle_name || ' ' || indata.last_name), 's{2,}', ' ')
from indata;
ha! just about to post the regexp_replace methodology!
– Harrison
Aug 14 '12 at 17:38
lol beat you to it ;-)
– tbone
Aug 14 '12 at 17:42
add a comment |
with indata as
(
select 'John' as first_name, 'W' as middle_name, 'Smith ' as last_name from dual
union
select null as first_name, null as middle_name, 'Adams' as last_name from dual
union
select 'Tom' as first_name, null as middle_name, 'Jefferson' as last_name from dual
)
select
regexp_replace(trim(indata.first_name || ' ' || indata.middle_name || ' ' || indata.last_name), 's{2,}', ' ')
from indata;
ha! just about to post the regexp_replace methodology!
– Harrison
Aug 14 '12 at 17:38
lol beat you to it ;-)
– tbone
Aug 14 '12 at 17:42
add a comment |
with indata as
(
select 'John' as first_name, 'W' as middle_name, 'Smith ' as last_name from dual
union
select null as first_name, null as middle_name, 'Adams' as last_name from dual
union
select 'Tom' as first_name, null as middle_name, 'Jefferson' as last_name from dual
)
select
regexp_replace(trim(indata.first_name || ' ' || indata.middle_name || ' ' || indata.last_name), 's{2,}', ' ')
from indata;
with indata as
(
select 'John' as first_name, 'W' as middle_name, 'Smith ' as last_name from dual
union
select null as first_name, null as middle_name, 'Adams' as last_name from dual
union
select 'Tom' as first_name, null as middle_name, 'Jefferson' as last_name from dual
)
select
regexp_replace(trim(indata.first_name || ' ' || indata.middle_name || ' ' || indata.last_name), 's{2,}', ' ')
from indata;
answered Aug 14 '12 at 17:34
tbonetbone
11.9k12536
11.9k12536
ha! just about to post the regexp_replace methodology!
– Harrison
Aug 14 '12 at 17:38
lol beat you to it ;-)
– tbone
Aug 14 '12 at 17:42
add a comment |
ha! just about to post the regexp_replace methodology!
– Harrison
Aug 14 '12 at 17:38
lol beat you to it ;-)
– tbone
Aug 14 '12 at 17:42
ha! just about to post the regexp_replace methodology!
– Harrison
Aug 14 '12 at 17:38
ha! just about to post the regexp_replace methodology!
– Harrison
Aug 14 '12 at 17:38
lol beat you to it ;-)
– tbone
Aug 14 '12 at 17:42
lol beat you to it ;-)
– tbone
Aug 14 '12 at 17:42
add a comment |
You could use RPAD() to add in the space character:
SELECT RPAD(first_name, LENGTH(first_name)+1, ' ')||RPAD(middle_name, LENGTH(middle_name)+1, ' ')||last_name
FROM TABLE_A;
When any of the parameters to RPAD are NULL, the result will be NULL, and in Oracle appending NULL to a string returns the original string.
add a comment |
You could use RPAD() to add in the space character:
SELECT RPAD(first_name, LENGTH(first_name)+1, ' ')||RPAD(middle_name, LENGTH(middle_name)+1, ' ')||last_name
FROM TABLE_A;
When any of the parameters to RPAD are NULL, the result will be NULL, and in Oracle appending NULL to a string returns the original string.
add a comment |
You could use RPAD() to add in the space character:
SELECT RPAD(first_name, LENGTH(first_name)+1, ' ')||RPAD(middle_name, LENGTH(middle_name)+1, ' ')||last_name
FROM TABLE_A;
When any of the parameters to RPAD are NULL, the result will be NULL, and in Oracle appending NULL to a string returns the original string.
You could use RPAD() to add in the space character:
SELECT RPAD(first_name, LENGTH(first_name)+1, ' ')||RPAD(middle_name, LENGTH(middle_name)+1, ' ')||last_name
FROM TABLE_A;
When any of the parameters to RPAD are NULL, the result will be NULL, and in Oracle appending NULL to a string returns the original string.
answered Aug 14 '12 at 16:45
ninesidedninesided
18.9k1373103
18.9k1373103
add a comment |
add a comment |
This is how I typically concatenate several fields and remove whitespace in Oracle:
TRIM(REGEXP_REPLACE(HOUSE_NO || ' ' || PREFIX || ' ' || STREET_NAME || ' ' || STREET_TYPE || ' ' || SUFFIX, ' +', ' '))
- Concatenate all the fields necessary with a space in between each. Empty strings and NULL values will result in a two or more spaces;
- Use a regular expression to change any occurrences of multiple spaces [' +'] to a single space [' '];
- Finally, trim any whitespace at the beginning and/or end from the resulting string.
add a comment |
This is how I typically concatenate several fields and remove whitespace in Oracle:
TRIM(REGEXP_REPLACE(HOUSE_NO || ' ' || PREFIX || ' ' || STREET_NAME || ' ' || STREET_TYPE || ' ' || SUFFIX, ' +', ' '))
- Concatenate all the fields necessary with a space in between each. Empty strings and NULL values will result in a two or more spaces;
- Use a regular expression to change any occurrences of multiple spaces [' +'] to a single space [' '];
- Finally, trim any whitespace at the beginning and/or end from the resulting string.
add a comment |
This is how I typically concatenate several fields and remove whitespace in Oracle:
TRIM(REGEXP_REPLACE(HOUSE_NO || ' ' || PREFIX || ' ' || STREET_NAME || ' ' || STREET_TYPE || ' ' || SUFFIX, ' +', ' '))
- Concatenate all the fields necessary with a space in between each. Empty strings and NULL values will result in a two or more spaces;
- Use a regular expression to change any occurrences of multiple spaces [' +'] to a single space [' '];
- Finally, trim any whitespace at the beginning and/or end from the resulting string.
This is how I typically concatenate several fields and remove whitespace in Oracle:
TRIM(REGEXP_REPLACE(HOUSE_NO || ' ' || PREFIX || ' ' || STREET_NAME || ' ' || STREET_TYPE || ' ' || SUFFIX, ' +', ' '))
- Concatenate all the fields necessary with a space in between each. Empty strings and NULL values will result in a two or more spaces;
- Use a regular expression to change any occurrences of multiple spaces [' +'] to a single space [' '];
- Finally, trim any whitespace at the beginning and/or end from the resulting string.
edited Mar 17 '16 at 21:48
answered Mar 16 '16 at 22:59
Erik AndersonErik Anderson
2,4902120
2,4902120
add a comment |
add a comment |
Yet another option:
SELECT first_name
|| DECODE(middle_name
, NULL, NULL
, ' ' || middle_name)
|| DECODE(last_name
, NULL, NULL
, ' ' || last_name) full_name
FROM table_a
;
if middle_name or last_name is empty string (i.e. '') - it doesn't work properly...
– Grisha Weintraub
Aug 14 '12 at 17:24
1
In Oracle it will; Oracle makes no distinction between the empty string andNULL.
– Tebbe
Aug 14 '12 at 20:14
Wow... I am working with Oracle about a year and didn't know it(worked with MSSQL before). Thanks a lot for your comment it will be useful for me.
– Grisha Weintraub
Aug 15 '12 at 3:32
add a comment |
Yet another option:
SELECT first_name
|| DECODE(middle_name
, NULL, NULL
, ' ' || middle_name)
|| DECODE(last_name
, NULL, NULL
, ' ' || last_name) full_name
FROM table_a
;
if middle_name or last_name is empty string (i.e. '') - it doesn't work properly...
– Grisha Weintraub
Aug 14 '12 at 17:24
1
In Oracle it will; Oracle makes no distinction between the empty string andNULL.
– Tebbe
Aug 14 '12 at 20:14
Wow... I am working with Oracle about a year and didn't know it(worked with MSSQL before). Thanks a lot for your comment it will be useful for me.
– Grisha Weintraub
Aug 15 '12 at 3:32
add a comment |
Yet another option:
SELECT first_name
|| DECODE(middle_name
, NULL, NULL
, ' ' || middle_name)
|| DECODE(last_name
, NULL, NULL
, ' ' || last_name) full_name
FROM table_a
;
Yet another option:
SELECT first_name
|| DECODE(middle_name
, NULL, NULL
, ' ' || middle_name)
|| DECODE(last_name
, NULL, NULL
, ' ' || last_name) full_name
FROM table_a
;
answered Aug 14 '12 at 17:18
TebbeTebbe
1,183510
1,183510
if middle_name or last_name is empty string (i.e. '') - it doesn't work properly...
– Grisha Weintraub
Aug 14 '12 at 17:24
1
In Oracle it will; Oracle makes no distinction between the empty string andNULL.
– Tebbe
Aug 14 '12 at 20:14
Wow... I am working with Oracle about a year and didn't know it(worked with MSSQL before). Thanks a lot for your comment it will be useful for me.
– Grisha Weintraub
Aug 15 '12 at 3:32
add a comment |
if middle_name or last_name is empty string (i.e. '') - it doesn't work properly...
– Grisha Weintraub
Aug 14 '12 at 17:24
1
In Oracle it will; Oracle makes no distinction between the empty string andNULL.
– Tebbe
Aug 14 '12 at 20:14
Wow... I am working with Oracle about a year and didn't know it(worked with MSSQL before). Thanks a lot for your comment it will be useful for me.
– Grisha Weintraub
Aug 15 '12 at 3:32
if middle_name or last_name is empty string (i.e. '') - it doesn't work properly...
– Grisha Weintraub
Aug 14 '12 at 17:24
if middle_name or last_name is empty string (i.e. '') - it doesn't work properly...
– Grisha Weintraub
Aug 14 '12 at 17:24
1
1
In Oracle it will; Oracle makes no distinction between the empty string and
NULL.– Tebbe
Aug 14 '12 at 20:14
In Oracle it will; Oracle makes no distinction between the empty string and
NULL.– Tebbe
Aug 14 '12 at 20:14
Wow... I am working with Oracle about a year and didn't know it(worked with MSSQL before). Thanks a lot for your comment it will be useful for me.
– Grisha Weintraub
Aug 15 '12 at 3:32
Wow... I am working with Oracle about a year and didn't know it(worked with MSSQL before). Thanks a lot for your comment it will be useful for me.
– Grisha Weintraub
Aug 15 '12 at 3:32
add a comment |
Or you could simply use the REPLACE function:
with indata as
(select 'John' as first_name, 'W' as middle_name, 'Smith ' as last_name from dual
union
select null as first_name, null as middle_name, 'Adams' as last_name from dual
union
select 'Tom' as first_name, null as middle_name, 'Jefferson' as last_name from dual)
SELECT REPLACE(TRIM(indata.first_name || ' ' || indata.middle_name || ' ' || indata.last_name), ' ', ' ')
FROM indata
(And thanks to @tbone for the example data :-)
add a comment |
Or you could simply use the REPLACE function:
with indata as
(select 'John' as first_name, 'W' as middle_name, 'Smith ' as last_name from dual
union
select null as first_name, null as middle_name, 'Adams' as last_name from dual
union
select 'Tom' as first_name, null as middle_name, 'Jefferson' as last_name from dual)
SELECT REPLACE(TRIM(indata.first_name || ' ' || indata.middle_name || ' ' || indata.last_name), ' ', ' ')
FROM indata
(And thanks to @tbone for the example data :-)
add a comment |
Or you could simply use the REPLACE function:
with indata as
(select 'John' as first_name, 'W' as middle_name, 'Smith ' as last_name from dual
union
select null as first_name, null as middle_name, 'Adams' as last_name from dual
union
select 'Tom' as first_name, null as middle_name, 'Jefferson' as last_name from dual)
SELECT REPLACE(TRIM(indata.first_name || ' ' || indata.middle_name || ' ' || indata.last_name), ' ', ' ')
FROM indata
(And thanks to @tbone for the example data :-)
Or you could simply use the REPLACE function:
with indata as
(select 'John' as first_name, 'W' as middle_name, 'Smith ' as last_name from dual
union
select null as first_name, null as middle_name, 'Adams' as last_name from dual
union
select 'Tom' as first_name, null as middle_name, 'Jefferson' as last_name from dual)
SELECT REPLACE(TRIM(indata.first_name || ' ' || indata.middle_name || ' ' || indata.last_name), ' ', ' ')
FROM indata
(And thanks to @tbone for the example data :-)
answered Aug 14 '12 at 18:28
Bob JarvisBob Jarvis
33.8k55784
33.8k55784
add a comment |
add a comment |
I have this work around with example. Hope this helps. Just go to SQL server, select new query and CP following query:
DECLARE @NULL_SAMLES TABLE
(
NS_ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED NOT NULL
,COL_01 VARCHAR(10) NULL
,COL_02 VARCHAR(10) NULL
,COL_03 VARCHAR(10) NULL
,COL_04 VARCHAR(10) NULL
)
INSERT INTO @NULL_SAMLES(COL_01,COL_02,COL_03,COL_04)
VALUES
('A','B','C','D')
,(' ' ,'B','C','D')
,(' ',NULL,'C','D')
,('A','B',NULL,'D')
,('A','B','C',NULL)
,(NULL,'B',NULL,'D')
,(NULL,'B','C',NULL)
,('A',NULL,'C',NULL)
,('A',NULL,NULL,'D')
,('A',NULL,NULL,NULL)
,(NULL,'B',NULL,NULL)
,(NULL,NULL,'C',NULL)
,(NULL,NULL,NULL,'D')
SELECT
NS.COL_01
,NS.COL_02
,NS.COL_03
,NS.COL_04,
Stuff(
Coalesce(', ' + nullif(NS.COL_01, ''), '')
+ Coalesce(', ' + nullif(NS.COL_02, ''), '')
+ Coalesce(', ' + nullif(NS.COL_03, ''), '')
+Coalesce(', ' + nullif(NS.COL_04, ''), '')
, 1, 1, '') AS CONC_COLS
FROM @NULL_SAMLES NS
add a comment |
I have this work around with example. Hope this helps. Just go to SQL server, select new query and CP following query:
DECLARE @NULL_SAMLES TABLE
(
NS_ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED NOT NULL
,COL_01 VARCHAR(10) NULL
,COL_02 VARCHAR(10) NULL
,COL_03 VARCHAR(10) NULL
,COL_04 VARCHAR(10) NULL
)
INSERT INTO @NULL_SAMLES(COL_01,COL_02,COL_03,COL_04)
VALUES
('A','B','C','D')
,(' ' ,'B','C','D')
,(' ',NULL,'C','D')
,('A','B',NULL,'D')
,('A','B','C',NULL)
,(NULL,'B',NULL,'D')
,(NULL,'B','C',NULL)
,('A',NULL,'C',NULL)
,('A',NULL,NULL,'D')
,('A',NULL,NULL,NULL)
,(NULL,'B',NULL,NULL)
,(NULL,NULL,'C',NULL)
,(NULL,NULL,NULL,'D')
SELECT
NS.COL_01
,NS.COL_02
,NS.COL_03
,NS.COL_04,
Stuff(
Coalesce(', ' + nullif(NS.COL_01, ''), '')
+ Coalesce(', ' + nullif(NS.COL_02, ''), '')
+ Coalesce(', ' + nullif(NS.COL_03, ''), '')
+Coalesce(', ' + nullif(NS.COL_04, ''), '')
, 1, 1, '') AS CONC_COLS
FROM @NULL_SAMLES NS
add a comment |
I have this work around with example. Hope this helps. Just go to SQL server, select new query and CP following query:
DECLARE @NULL_SAMLES TABLE
(
NS_ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED NOT NULL
,COL_01 VARCHAR(10) NULL
,COL_02 VARCHAR(10) NULL
,COL_03 VARCHAR(10) NULL
,COL_04 VARCHAR(10) NULL
)
INSERT INTO @NULL_SAMLES(COL_01,COL_02,COL_03,COL_04)
VALUES
('A','B','C','D')
,(' ' ,'B','C','D')
,(' ',NULL,'C','D')
,('A','B',NULL,'D')
,('A','B','C',NULL)
,(NULL,'B',NULL,'D')
,(NULL,'B','C',NULL)
,('A',NULL,'C',NULL)
,('A',NULL,NULL,'D')
,('A',NULL,NULL,NULL)
,(NULL,'B',NULL,NULL)
,(NULL,NULL,'C',NULL)
,(NULL,NULL,NULL,'D')
SELECT
NS.COL_01
,NS.COL_02
,NS.COL_03
,NS.COL_04,
Stuff(
Coalesce(', ' + nullif(NS.COL_01, ''), '')
+ Coalesce(', ' + nullif(NS.COL_02, ''), '')
+ Coalesce(', ' + nullif(NS.COL_03, ''), '')
+Coalesce(', ' + nullif(NS.COL_04, ''), '')
, 1, 1, '') AS CONC_COLS
FROM @NULL_SAMLES NS
I have this work around with example. Hope this helps. Just go to SQL server, select new query and CP following query:
DECLARE @NULL_SAMLES TABLE
(
NS_ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED NOT NULL
,COL_01 VARCHAR(10) NULL
,COL_02 VARCHAR(10) NULL
,COL_03 VARCHAR(10) NULL
,COL_04 VARCHAR(10) NULL
)
INSERT INTO @NULL_SAMLES(COL_01,COL_02,COL_03,COL_04)
VALUES
('A','B','C','D')
,(' ' ,'B','C','D')
,(' ',NULL,'C','D')
,('A','B',NULL,'D')
,('A','B','C',NULL)
,(NULL,'B',NULL,'D')
,(NULL,'B','C',NULL)
,('A',NULL,'C',NULL)
,('A',NULL,NULL,'D')
,('A',NULL,NULL,NULL)
,(NULL,'B',NULL,NULL)
,(NULL,NULL,'C',NULL)
,(NULL,NULL,NULL,'D')
SELECT
NS.COL_01
,NS.COL_02
,NS.COL_03
,NS.COL_04,
Stuff(
Coalesce(', ' + nullif(NS.COL_01, ''), '')
+ Coalesce(', ' + nullif(NS.COL_02, ''), '')
+ Coalesce(', ' + nullif(NS.COL_03, ''), '')
+Coalesce(', ' + nullif(NS.COL_04, ''), '')
, 1, 1, '') AS CONC_COLS
FROM @NULL_SAMLES NS
edited Aug 14 '18 at 10:58
anothernode
2,681102542
2,681102542
answered Aug 14 '18 at 10:25
Chetan BirajdarChetan Birajdar
277
277
add a comment |
add a comment |
Don't underestimate the simple power of the CASE statement, which can be concatenated. Here's a self-contained example you can run as-is:
SELECT
CASE WHEN x.FIRST_NAME IS NULL THEN x.FIRST_NAME ELSE x.FIRST_NAME || ' ' END ||
CASE WHEN x.MIDDLE_NAME IS NULL THEN x.MIDDLE_NAME ELSE x.MIDDLE_NAME || ' ' END ||
x.LAST_NAME
FROM (SELECT 'John' AS FIRST_NAME, NULL AS MIDDLE_NAME, 'Doe' AS LAST_NAME FROM DUAL) x;
add a comment |
Don't underestimate the simple power of the CASE statement, which can be concatenated. Here's a self-contained example you can run as-is:
SELECT
CASE WHEN x.FIRST_NAME IS NULL THEN x.FIRST_NAME ELSE x.FIRST_NAME || ' ' END ||
CASE WHEN x.MIDDLE_NAME IS NULL THEN x.MIDDLE_NAME ELSE x.MIDDLE_NAME || ' ' END ||
x.LAST_NAME
FROM (SELECT 'John' AS FIRST_NAME, NULL AS MIDDLE_NAME, 'Doe' AS LAST_NAME FROM DUAL) x;
add a comment |
Don't underestimate the simple power of the CASE statement, which can be concatenated. Here's a self-contained example you can run as-is:
SELECT
CASE WHEN x.FIRST_NAME IS NULL THEN x.FIRST_NAME ELSE x.FIRST_NAME || ' ' END ||
CASE WHEN x.MIDDLE_NAME IS NULL THEN x.MIDDLE_NAME ELSE x.MIDDLE_NAME || ' ' END ||
x.LAST_NAME
FROM (SELECT 'John' AS FIRST_NAME, NULL AS MIDDLE_NAME, 'Doe' AS LAST_NAME FROM DUAL) x;
Don't underestimate the simple power of the CASE statement, which can be concatenated. Here's a self-contained example you can run as-is:
SELECT
CASE WHEN x.FIRST_NAME IS NULL THEN x.FIRST_NAME ELSE x.FIRST_NAME || ' ' END ||
CASE WHEN x.MIDDLE_NAME IS NULL THEN x.MIDDLE_NAME ELSE x.MIDDLE_NAME || ' ' END ||
x.LAST_NAME
FROM (SELECT 'John' AS FIRST_NAME, NULL AS MIDDLE_NAME, 'Doe' AS LAST_NAME FROM DUAL) x;
answered Nov 15 '18 at 20:32
Gustavo LopezGustavo Lopez
11
11
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11956844%2fspaces-when-concatenating-multiple-columns-and-one-column-is-null-oracle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown