I am trying to build the string with comma but I get extra space. How can I remove extra space between zipcode and country_name? Here is my string. Thank you for any suggestion.
SELECT
(COALESCE(address + ', ', '') +
COALESCE(city + ', ', '') +
COALESCE(state_code + ' ', '') +
COALESCE(zipcode + ' ', '') +
COALESCE(country_name + '', '')) address
from table1
where a_id = 2
Here is the result:
tewt, test ct, DE 4444 United States
You can use the RTRIM function which remove white space from right side of the variable. Check also LTRIM for the left cases.
I would change table column datatape to varchar(x) from char(x) or to nvarchar(x) from nchar(x). And change the data so they dont contain spaces. Just have to do that once and make changes so that app is not storing white spaces anymore.
char(x) and nchar(x) i would use just when there is fixed length strings, but this does not seem to be the case.
Related
We have a string, 'AMERICAN:BEER~ INVENTIONS CHOCOLATE-FEVER'.
We are trying to remove special characters and replace that with a space and later checking if we have created any double spaces, and we try to replace the double spaces with a single space.
So, if we remove the special characters, and we remove the double spaces, we are left with 'AMERICAN BEER INVENTIONS CHOCOLATE FEVER'.
The problem here is, we have unintentionally removed, the default double space between 'INVENTIONS CHOCOLATE'.
How can we avoid this?
My current approach:
Remove special characters:
UPDATE xxxx.xxxxx
SET xxxx = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(xxxx, '<', ' '), '>', ' '), '(', ' '), ')', ' '), ':', ' '), '#', ' '), '~', ' ')
WHERE xxxx LIKE '%[<>:()#~]%';
Pass the column in a function to remove double spaces:
SET #str = TRIM(#str);
WHILE CHARINDEX(' ', #str) > 0
SET #str = REPLACE(#str, ' ', ' ');
Does the following work for you?
Using translate to convert to a single character, then remove additional spaces and finally replace the unwanted character:
declare #s varchar(50) = 'AMERICAN:BEER ~ INVENTIONS CHOCOLATE-FEVER'
select Replace(Replace(Replace(Translate(#s, '<>:()~-','#######'), '# ', '#'), ' #', '#'), '#', ' ');
Result
AMERICAN BEER INVENTIONS CHOCOLATE FEVER
How to add space in this SQL text?
ISNULL('phone: ' + [phone],' ') + 'some space' + ISNULL('email: ' +[email],' ') +
What can be used in the place of 'some space ' so that space is put in between the texts ? any help , thank you.
EDITED
I want the phone and email displayed far apart eachother may be 100px in between them
SPACE function works in SQL but not in browser. what is wrong?
Instead of just writing some space, literally use spaces
('phone: ' + [phone],' ') + 'some space' + ('email: ' +[email],' ') +
to
('phone: ' + [phone],' ') + ' ' + ('email: ' +[email],' ') +
I'm unclear if this is what you're asking but, depending on your variant of SQL, you might not be able to concatenate text with "+".
Rather there is generally some flavor of the concat() or concatenate() function that serves this purpose.
eg. concat(isnull(concat('phone: ', [phone]),' '), 'some space')... etc
I think you want:
SELECT LTRIM(COALESCE(' phone: ' + [phone], '') + COALESCE(' email: ' + [email], ''))
This will return one or the other (or both) with no leading spaces.
If this is being displayed on a website, then one option is to make sure there is a clear 1-pixel gif on the website, and add the img tag for that gif and style it with the amount of horizontal space that you want.
But then I haven't done web dev in so long that 1-pixel gifs could be totally passe today for all I know.
Use ASCII code for horizontal tab which is CHAR(9) along with CONCAT function.
You can also do a function with a loop to set your spacing
DECLARE #tabSpacing varchar(50)='';
DECLARE #counter int = 50;
WHILE #counter>0
BEGIN
SET #tabSpacing+=CHAR(9);
SET #counter-=1;
END
SELECT CONCAT(ISNULL('phone: ' + [phone],' '),#tabSpacing,ISNULL('email: ' +[email],' ')) AS Info FROM Users
It may help someone when a need arises. My question was solved this way
DECLARE #wider_space NVARCHAR(50);
SET #wider_space = '& nbsp ; & nbsp ; & nbsp ; & nbsp;& nbsp ; & nbsp ;& nbsp ; & nbsp ;';
---avoid spaces when used
ISNULL('Phone: ' + [phone] + #wider_space,' ') + ISNULL('Email: ' +[email] + #wider_space,' ') + ...
and continues like this to concat 6 columns together.
So I have a select
select
PART_NO,
FIPS_COUNTY_CODE as COUNTY_CODE,
LAST AS LNAME,
FIRST AS FNAME,
IsNull(ALTADDR, '') + ' ' + DELADDR as ADDRESS,
CITY,
STATE,
ZIP,
SEX,
RACE,
STATUS,
convert(datetime, BIRTHDATE) as DOB,
H_PHONE,
VOTER_DIV,
VOTER_REG,
JUD_CODE,
[LICENSE NO] AS D_LICENSE,
SIN,
DATE_SELECTED1,
DATE_SELECTED2,
DATE_SELECTED3,
PART_NO - 100000000 AS REC_NUM,
PERM_DISQUAL,
SCAN_CODE,
DIVISION AS DIVISION_CODE,
OFFICIAL_USE,
COURT_EMP,
HISPANIC,
NOTES,
CITIZEN,
DATE_UPD,
DS as SOURCE,
CELL_PHONE,
W_PHONE,
W_PH_LOCAL,
EMAIL,
PENDING_IND,
PENDING_EXPIRES,
LOGIN_LOCK,
DATE_SELECTED4,
QUALIFIED_DATE,
POOL_CREATION_DATE
into FINAL
INTO FINAL
FROM NCOASUPPRESS
And ALTADDR and DELADDR need to be combined, for example:
ALTADDR = APT B1
DELADDR = 1000 Goggins Ln
Expected Result = APT B1 1000 Goggins Ln
But sometimes ALTADDR is blank, so what happens is an extra space gets put in before the expected result. Also after...? But it doesn't do this for any other field. I tried LTRIM/RTRIM but they didn't seem to work. I essentially need this field trimmed for leader and trailing blank space, but the trim functions did not seem to work for me.
Handle the space inside the COALESCE():
COALESCE(ALTADDR + ' ', '') + DELADDR as ADDRESS,
Since you have already tried ltrim() and rtrim(), you probably have extra white space characters in your data that aren't the actual space character.
To replace tabs, new lines, and carriage returns... Try:
address = replace(replace(replace(isnull(altaddr + ' ', '') + deladdr
,char(10),'') /* new line */
,char(13),'') /* carriage return */
,char(9),'') /* tab */
If you still see spaces, wrap the above in ltrim(rtrim(replace...)).
this version would help chop whitespace off the beginning, but not the end:
address = substring((isnull(altaddr + ' ', '') + deladdr)
, patindex('%[a-z0-9]%',(isnull(altaddr + ' ', '') + deladdr))
, 8000)
super trim
SELECT LTRIM(RTRIM(CONCAT(' ' + LTRIM(RTRIM(ALTADDR)), ' ' + LTRIM(RTRIM(DELADDR)))))
Try using CONCAT
CONCAT(ALTADDR + ' ', DELADDR) as ADDRESS
this will handle NULL on both ALTADDR and DELADDR
EDIT
OTT approach: CONCAT(LTRIM(RTRIM(ALTADDR)) + ' ', LTRIM(RTRIM(DELADDR))) as ADDRESS
Perhaps you used L/RTRIM()incorrectly....
declare #ALTADDR varchar(64) = ' '
declare #DELADDR varchar(64) = '1000 Goggins Ln'
SELECT LTRIM(RTRIM(CONCAT(RTRIM(#ALTADDR) + ' ',LTRIM(#DELADDR)))) as ADDRESS
This ensures you don't have leading or trailing spaces regardless if the field is ' ' or NULL
I'm trying to get a row with the concatenation of a welders names.
This is what i've got:
SELECT jsd1.JuntaSoldaduraID,
REPLACE(RTRIM((SELECT s1.Nombre + ' ' + s1.ApPaterno + ' ' +
s1.ApMaterno + '' + CAST('' AS VARCHAR(MAX)) + ' '
FROM JuntaSoldaduraDetalle jsd
INNER JOIN Soldador s1 on s1.SoldadorID = jsd.SoldadorID
WHERE (jsd1.JuntaSoldaduraID = jsd.JuntaSoldaduraID)
and (jsd.TecnicaSoldadorID = 2)
FOR XML PATH (''))),' ',', ') AS NombreSoldador
FROM JuntaSoldaduraDetalle jsd1
INNER JOIN Soldador s
ON s.SoldadorID = jsd1.SoldadorID
GROUP BY jsd1.JuntaSoldaduraID
I'm able to get the information that i want but with a little problem.
What I want is "John Smith, David Rogers, Peter Simons" etc.. in other words, full names separated by commas.
But i'm receiving "John, smith, David, Rogers, Peter, Simons"..
Any help appreciated.
Thanks in advance.
Your REPLACE will replace all spaces with a comma. Instead, make the comma part of your query and use STUFF to remove the first occurrence of the comma. You might also want to incorporate the use of COALESCE in case any of the name fields are NULL.
SELECT jsd1.JuntaSoldaduraID,
STUFF((SELECT ', ' + COALESCE(s1.Nombre + ' ','')
+ COALESCE(s1.ApPaterno + ' ','')
+ COALESCE(s1.ApMaterno,'')
FROM JuntaSoldaduraDetalle jsd
INNER JOIN Soldador s1
on s1.SoldadorID = jsd.SoldadorID
WHERE jsd1.JuntaSoldaduraID = jsd.JuntaSoldaduraID
and jsd.TecnicaSoldadorID = 2
FOR XML PATH ('')),1,2,'') AS NombreSoldador
FROM JuntaSoldaduraDetalle jsd1
INNER JOIN Soldador s
ON s.SoldadorID = jsd1.SoldadorID
GROUP BY jsd1.JuntaSoldaduraID
You are replacing single spaces in the result with a comma and a space.
What is the result if you remove the REPLACE from the query?
I have a table from where I would like to concat address fields by separating on ',' or ' '. But there is a scenario that all fields do not have value in them.
As per screenshot, there can be fields where values are blank, in that case due to ', ' the value just populated as , , and would also lead to trailing and leading spaces.
Initial Approach: (
concat(trim(concat(trim(faddress1), ', ', trim(faddress2))), ', ', trim(fcity), ', ', trim(fstate), ', ', trim(fzip))) stop_location_code,
We should do some conditional logic so we aren't adding spaces when a component is null
Tried second approach to see if some conditional logic can be done, though it removed the spaces issue. All the values are concatentated together without space (refer screenshot)
Second Way:
concat(isnull(trim(faddress1),''),isnull(trim(faddress2),''),isnull(trim(fcity),''),isnull(trim(fstate),''),isnull(trim(fzip),'')) as test,`
Example:
If faddress1 = abc, faddress2 = def, fcity = ghi so on
return "abc def ghi etc.."