I'm new to sql and working with a column name where names are listed with spaces.
Example: Alan Joe
I am using LTRIM and RTRIM to display name as 'AlanJoe'
select LTRIM(name)
Any help how to remove spaces between the names or any links I can learn from?
Thank you
use replace() function
select replace(name,' ','')
You can try with
ISNULL(LTRIM(RTRIM((FirstName,''),'') + ' ','') + LTRIM(RTRIM((LastName,''),'')
this will make FirstName LastName combination with 1 Space if FirstName Value exists else will give only Last Name.
For removing the spaces simply use REPLACE(Name,' ','')
Related
I have a column in my SQL Server database and it has white spaces from left and right site of the record. Basically it's a nvarchar(250) column.
I have tried removing white spaces completely like this:
UPDATE MyTable
SET whitespacecolumn = LTRIM(RTRIM(whitespacecolumn))
But this didn't work out at all, the whitespace is still there. What am I doing wrong here?
Check the below;
Find any special characters like char(10), char(13) etc in the field value.
Check the status of ANSI_PADDING ON. Refer this MSDN article.
I think replace is the way as you are looking to update
UPDATE MyTable SET whitespacecolumn = Replace(whitespacecolumn, ' ', '')
you can try doing select first and then prefer to update
SELECT *, Replace(whitespacecolumn, ' ', '') from MyTable
LTRIM, RTRIM will remove spaces in front and rear of column. In 2016 you can use TRIM function as below to trim special characters as well:
SELECT TRIM( '.,! ' FROM '# test .') AS Result;
Output:
# test
I have a column as name which is having a results like.
name
ABC
XYZ
ader
fer
I want to remove the blank space before ader and it should print in the output like
ader.
How to achieve that?
Depending on your database you can use trim(), ltrim()/rtrim(), or replace():
select replace(name, ' ', '')
select trim(name, ' ')
select ltrim(rtrim(name))
You can use the LTRIM and RTRIM functions to remove trailing and leading spaces.
SELECT
RTRIM(LTRIM(name)) AS name
FROM yourTable
Is there anyway to ignore spaces in a SQL query only from right and left of a string so that I can find ' alex ' (not ' i am alex jolig ') by searching for 'alex'?
I've read this question and its answer, but the solution removes all the spaces in the string.
SELECT * FROM mytable
WHERE REPLACE(username, ' ', '') = REPLACE("John Bob Jones", ' ', '')
By the way, I'm using Sql Server Compact Edition.
Thank you.
You can use RTRIM() to remove spaces from the right and LTRIM() to remove spaces from the left hence left and right spaces removed as follows:
SELECT * FROM mytable
WHERE LTRIM(RTRIM(username)) = LTRIM(RTRIM("John Bob Jones"))
You can use RTRIM() to remove spaces from the right of string and LTRIM() to remove spaces from the left of string.
Hence left and right spaces removed as follows:
SELECT * FROM tableName
WHERE LTRIM(RTRIM(username)) = LTRIM(RTRIM("bod alias baby"))
I'm using PostgreSQL and I have a list of names in the following format:
"Abbing, Mr. Anthony"
"Abbott, Mr. Rossmore Edward"
"Abbott, Mrs. Stanton (Rosa Hunt)"
And I want to extract the title (i.e. "Mr", "Mrs"). It is always between the comma and the dot.
I'm only new to regular expressions, this is what i'm trying to use but i'm not getting the correct answer.
SELECT SUBSTRING(name from ',..')
I get ", M" as an answer.
I assume this is something very simple to fix.
Thanks
substring(name from '(, (Mr)(s){0,1}\.)')
will extract , Mr. or , Mrs.. Note the parentheses around the whole expression. substring( ... from ..) will return the match from the first group in the regex. As I have used (Mr) and (s) to match the titles, I have to put everything between parentheses to make substring() return the whole pattern
to get rid of the leading ', ' you can use trim()
trim(substring(name from '(, (Mr)(s){0,1}\.)'), ', ')
Try
SELECT SUBSTRING(name from ',\s([^\.]+)\.')
You could use position and substring function to do this:
SELECT samplename
,trim(substring(samplename, position(',' IN samplename) + 1
, position('.' IN samplename) - position(',' IN samplename))) AS initials
FROM test
SQL Fiddle Demo
I need a query that could remove unnecessary characters (a not-so-needed trailing comma as an example) from the string stored in my database table.
So that
EMAIL_ADD
abc#gmail.com,
abc#yahoo.com,def#example.org,
abs-def#ac.uk,
would update it into something like this:
EMAIL_ADD
abc#gmail.com
abc#yahoo.com,def#example.org
abs-def#ac.uk
Using TRIM() function with TRAILING option removes a specific unwanted character from end of string , in your case being a comma present at end.
UPDATE tableName
SET EMAIL_ADD = TRIM(TRAILING ',' FROM EMAIL_ADD)
See documentation here TRIM()
If you have a specific list of characters to filter out at the start and end use trim functions:
select ltrim(ltrim(rtrim(rtrim(email_add, ','), ' '), ','), ' ')
from tableX
Here I nested ltrim and rtrim to remove leading and trailing , and .
Or using trim:
select trim(trim(both ',' from email_add))
from tableX
if you only whant to remove the last character of a string you can use
update mytable set my_column = substr(my_column ,0,len(trim(my_column)-1) where mycolumn like '%,'
It is an untested example.