How to add a space between two text in SQL code - sql

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.

Related

How to hard code spaces at end of result?

I need to add spaces to the end of a column I have called NPI (nvarchar(20)). NPI is always 10 digits, but the requirements for the report want the first 10 digits to be the NPI followed by 10 spaces (text file formatting issues I assume). I have tried the following:
cast([NPI] as nvarchar(20)) + ' '
cast([NPI] as nvarchar(20)) + Space(10)
However, the result set does not change. It just shows the 10 digit NPI and spaces aren't included.
It sounds like you are actually using SQL Server instead of MySQL. VARCHAR() is for variable length strings and it will trim end whitespace. Cast instead to char/nchar for the desired output. It won't look like it in SSMS, so check datalength to confirm nchar(20) = 20 bytes * 2 for unicode.
SELECT CAST([NPI] AS NCHAR(20)) AS formattedNPI,
DATALENGTH(CAST([NPI] AS NCHAR(20))) AS confirmation
FROM your_table
This seems to work. The '*' is added to show that spaces are present..
print cast([NPI] as nchar(20)) + '*'
Here are a couple of other cheesy ways to add padding...
print substring([NPI] + ' ',1,20) + '*'
print [NPI] + space(20 - len([NPI])) + '*'
Add the space inside the cast
cast([NPI] + ' ' as nchar(20))
You're right #dfundako, I was fooled by my editor.
This works but I am using MariaDb (MySql) so it's maybe not relevant now.
select concat([NPI] , ' ')

SQL subtracting a string column from another string column

I am looking to subtract two string columns from another string column.
I have acheived this in the past in Oracle using the below
SELECT
C.MANUFACTURER,
C.MODEL_GROUP,
REGEXP_REPLACE(C.VARIANT, '^'||C.MANUFACTURER || ' +' || C.MODEL_GROUP) "VAR DESC",
C.VARIANT
FROM STD_BI.RL2_CONTRACTS_VW C
I now need to acheive the same in MS SQL any suggestions would be appreciated.
"Subtract" is surely a misnomer here. The OP appears to want to remove a string at the beginning of another string.
From this point on, I am assuming that MANUFACTURER and MODEL_GROUP do not contain any regular expression wildcard characters.
There is a challenge with multiple spaces. If there are not too many, then you can do. For instance, this handles one or two spaces:
SELECT C.MANUFACTURER, C.MODEL_GROUP,
(CASE WHEN C.VARIANT LIKE C.MANUFACTURER + ' ' + C.MODEL_GROUP + '%'
THEN STUFF(C.VARIANT, 1, LEN(C.MANUFACTURER + ' ' + C.MODEL_GROUP), '')
WHEN C.VARIANT LIKE C.MANUFACTURER + ' ' + C.MODEL_GROUP + '%' AND
THEN STUFF(C.VARIANT, 1, LEN(C.MANUFACTURER + ' ' + C.MODEL_GROUP), '')
ELSE C.VARIANT
END) as [VAR DESC],
C.VARIANT
FROM STD_BI.RL2_CONTRACTS_VW C;
EDIT:
Here is a more complete solution:
SELECT C.MANUFACTURER, C.MODEL_GROUP,
(CASE WHEN C.VARIANT LIKE C.MANUFACTURER + ' %' + C.MODEL_GROUP + '%' AND
C.VARIANT NOT LIKE C.MANUFACTURER + ' %[^ ]%' + C.MODEL_GROUP + '%'
THEN STUFF(LTRIM(STUFF(C.VARIANT, 1, LEN(C.MANUFACTURER), '')), 1, LEN(C.MODEL_GROUP), '')
ELSE C.VARIANT
END) as [VAR DESC],
C.VARIANT
FROM STD_BI.RL2_CONTRACTS_VW C;
This tests for just spaces between the two strings (the not like is checking for something other than a space). The replacement is then in three steps -- remove the manufacturer, then the spaces, then the model group.
I have spent some time on this one as much for my own satisfaction and in addition to the answer above, I have come up with the following which also generates the correct answer.
SUBSTRING(FV.VARIANT, LEN(FV.MANUFACTURER + ' ' + FV.MODEL_RANGE + ' ') + 1 , LEN(FV.VARIANT))
My problem was kinda easier to solve, I just needed to remove "xxxx" (the first 4 characters) at the start of a column from some rows:
UPDATE coluna SET columToEdit = SUBSTRING(columToEdit, 5, 200);

Using SQL Server to replace line breaks in columns with spaces

I have a large table of data where some of my columns contain line breaks. I would like to remove them and replace them with some spaces instead.
Can anybody tell me how to do this in SQL Server?
Thanks in advance
SELECT REPLACE(REPLACE(#str, CHAR(13), ''), CHAR(10), '')
This should work, depending on how the line breaks are encoded:
update t
set col = replace(col, '
', ' ')
where col like '%
%';
That is, in SQL Server, a string can contain a new line character.
#Gordon's answer should work, but in case you're not sure how your line breaks are encoded, you can use the ascii function to return the character value. For example:
declare #entry varchar(50) =
'Before break
after break'
declare #max int = len(#entry)
; with CTE as (
select 1 as id
, substring(#entry, 1, 1) as chrctr
, ascii(substring(#entry, 1, 1)) as code
union all
select id + 1
, substring(#entry, ID + 1, 1)
, ascii(substring(#entry, ID + 1, 1))
from CTE
where ID <= #max)
select chrctr, code from cte
print replace(replace(#entry, char(13) , ' '), char(10) , ' ')
Depending where your text is coming from, there are different encodings for a line break. In my test string I put the most common.
First I replace all CHAR(10) (Line feed) with CHAR(13) (Carriage return), then all doubled CRs to one CR and finally all CRs to the wanted replace (you want a blank, I put a dot for better visability:
Attention: Switch the output to "text", otherwise you wont see any linebreaks...
DECLARE #text VARCHAR(100)='test single 10' + CHAR(10) + 'test 13 and 10' + CHAR(13) + CHAR(10) + 'test single 13' + CHAR(13) + 'end of test';
SELECT #text
DECLARE #ReplChar CHAR='.';
SELECT REPLACE(REPLACE(REPLACE(#text,CHAR(10),CHAR(13)),CHAR(13)+CHAR(13),CHAR(13)),CHAR(13),#ReplChar);
I have the same issue, means I have a column having values with line breaks in it. I use the query
update `your_table_name` set your_column_name = REPLACE(your_column_name,'\n','')
And this resolves my issue :)
Basically '\n' is the character for Enter key or line break and in this query, I have replaced it with no space (which I want)
Keep Learning :)
zain

Single quote in SQL WHERE IN statement

Okay so my T-SQL statement is the following
IF #dept <> '' BEGIN
SET #query = #query + ' AND T6.Region2 IN (' + REPLACE(#dept, '^', '''') + ') '
END
This is basically for a French site and the region names have ' in the names. What I'm trying to do is pass something like the following to SQL
#dept = 'Cote-d^Armor', 'Val-d^Oise'
and replace the ^ with a '. When I do a replace it's just causing an error as if the replaced ^ is a ' and escaping the rest of the code.
Hope that makes sense.
You need some extra quotes outside the replace. Try this:
IF #dept <> '' BEGIN
SET #query = #query + ' AND T6.Region2 IN (''' + REPLACE(#dept, '^', '''') + ''') '
END
I wouldn't swap "'" for "^" to store it and then try and convert it back. It would be much simpler to store "Cote-d'Armor", then in your stored procedure whenever there is a "'" in the search string, to double it.
SQL Server can happily cope with ' and " in the data, they just need to be escaped properly.

String Concatenation with comma

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.