Remove special character from customerId column in SQL Server - sql-server

I want remove special character from CustomerId column.
Currently CustomerId column contains ¬78254782 and I want to remove the ¬ character.
Could you please help me with that ?

Applying the REPLACE T-SQL function :
SELECT REPLACE(CustomerId, '¬', '') FROM Customers;

SQL Server does not really have any regex support, which is what you would probably want to be using here. That being said, you could try using the enhanced LIKE operator as follows:
UPDATE yourTable
SET CustomerId = RIGHT(CustomerId, LEN(CustomerId) - 1)
WHERE CustomerId LIKE '[^A-Za-z0-9]%';
Here we are phrasing the condition of the first character being special using [^A-Za-z0-9], followed by anything else. In that case, we substring off the first character in the update.

Related

Using like to query 100% key word in SQL Server

I am trying to query Keyword 100% using Like command.
LIKE (‘%100%%’)
But the command is querying for all keywords with 100 which is not what I
want
Use Escape Character.
Try:
Select * from MyTable m where m.Column1 like '%100\%%' escape '\'
Escape Character can be set as per your convenience.
In the above query, replace MyTable with your table name and Column1 with your Column Name.
You could also take advantage of SQL Server's LIKE operator's regex syntax, and use [%] to represent a literal percent:
SELECT *
FROM yourTable
WHERE col LIKE '%100[%]%';
Demo
I prefer this method to the accepted answer because it makes more explicit the intention to represent a literal character, and it avoids the possible need for an ESCAPE clause.

SQL Server Select LIKE Regex Equivalent to '?' or '\\s*'

I have a short question is there an equivalent to ? in SQL Server SELECT LIKE regex?
I try to select data from table in SQL Server 2012. Example data from column
...
<SOMETHING>
<UNINTERESTING>test</UNINTERESTING>
<INTERESTING>searchedString</INTERESTING>
<SOMEMORE>
<PROBLEMHERE>searchedString</PROBLEMHERE>
</SOMEMORE>
...
or
...
<SOMETHING>
<UNINTERESSTING>test</UNINTERESSTING>
<INTERESING>searchedString but nevertheless wrong</INTERESTING>
<SOMEMORE>
<PROBLEMHERE>searchedString</PROBLEMHERE>
</SOMEMORE>
...
I can select these with
LIKE '%<INTERESING>searchedString</INTERESTING>%'
everything works fine, but it is possible that the value between <INTERESTING> ends sometimes with one or more spaces
e.g.
<INTERESING>searchedString </INTERESTING>
<INTERESING>searchedString </INTERESTING>
If I try to select with %, then I got rows, where the search string is found but not between interesting, or between two interestings, or I get the wrong rows like
<INTERESING>searchedString but nevertheless wrong</INTERESTING>
I tried to select with _, %, [ ] but nothing of them get me the correct rows.
Is there an equivalent in SQL Server LIKE regex for ? (none or one-time)
or anything like \s* (whitespaces, can be none, one or more-times)
Thanks in advance
best regards
Since you problem is spaces... just replace / remove those and your like statement should work and you can avoid other parsing and worrying about REGEX all together.
where replace(column,' ','') like'%<INTERESING>searchedString</INTERESTING>%'
If you expect TAB or NEWLINE then use replace and CHAR(10) and CHAR(13)
where replace(replace(replace(column,' ',''),char(10),''),char(13),'') like'%<INTERESING>searchedString</INTERESTING>%'
Is there an equivalent in SQL Like Regex
No. You should use SQL Server's XQuery to parse the XML and extract the XML Element values, and then use LIKE for examining the element value.

FOR XML PATH always adds trailing space to value

Using the FOR XML PATH structure to create a list of values,
I find that (annoyingly) it always adds a trailing space to selected values.
This ruins my attempts at providing my own delimiters - the trailing space is added after the column and delimiters have been concatenated.
For example:
SELECT country + '-' FROM countryTable...
results in the following string:
china- france- england-
Has anyone else seen this, and is there a way to stop it?
I don't think TRIM() will work, as that would be applied before the extra space is inserted...
I'm using SQL Server 2016.
Thanks
Ok, thanks to John C and his sample query I found the culprit.
I had a AS [data()] clause after the column name/delimiter.
Removing that removed the trailing space.
I don't know how/why but it did...
I suspect the data inside the country column, What if each value in Country column is having leading space. For XML PATH does not add any space to the data
Try this
SELECT RTRIM(LTRIM(country)) + '-' FROM countryTable...
You may have leading/trailing spaces and/or CRLFs. Perhaps this will help
Declare #countryTable table (country varchar(100))
Insert Into #countryTable values
(' china'), -- leading space
(char(13)+'france'), -- leading char(13)
(char(10)+'england') -- leading char(10)
Select Value=Stuff((Select Distinct '-' + ltrim(rtrim(replace(replace(country,char(13),''),char(10),'')))
From #countryTable
Where 1=1
For XML Path ('')),1,1,'')
Returns
Value
china-england-france
FOR XML PATH ... AS [data()] add to this from MS Help
If the path specified as column name is data(), the value is treated as an atomic value in the generated XML. A space character is added to the XML if the next item in the serialization is also an atomic value. This is useful when you are creating list typed element and attribute values.
When you write here ... AS something. Then something is used as open/closing markup tag for each selected value.
Add 2. Is possible concate in select clausule more fileds from each row. For other types than string type, value must be converted into string type CAST AS

Converting all upper case to lower case in SQL server?

I want to update every row in my table from upper case to lower case. I searched everywhere but could not found relevant answer. I dont want it to select using SELECT. I would like to alter permanently may be using ALTER.
I am using SQL server 2008.
Thanks.
UPDATE table_name SET col1 = LOWER(col1), col2 = LOWER(col2), col3 = LOWER(col3);
HTH
Edit: Updating multiple columns. Just keep on adding columns like above. There is no direct automated way to update all the columns with a single command. Well, technically it may be possible using cursors, but I would advise against it since this looks like a one time process and you are better off with writing a command once and for all.
You can do this using string functions:
UPDATE MyTable SET MyColumn = LOWER(MyColumn)
There's the LOWER function. You'll need to UPDATE your table:
UPDATE mytable SET charfld1=LOWER(charfld1), charfld2=LOWER(charfld2), ...
Put all your textual fields after the SET.

WHERE clause on VARCHAR column seems to operate as a LIKE

I've stumbled across a situation I've never seen before. I hope that someone can explain the following.
I've ran the following query, hoping to get only the results of columns whoes value is exactly equal to 1101
select '--' + MyColumn + '--' SeeSpaces, Len(MyColumn) as LengthOfColumn
from MyTable
where MyColumn = '1101'
However, I also see values where 1101 is followed by (what I believe are) spaces.
So SeeSpaces returns
--1101 --
And LengthOfColumn returns 4
MyColumn is a VARCHAR(8), NOT NULL column. Its values (including the spaces) are inserted through a separate workflow.
Why does this select not return only the exact results?
Thanks in advance
The reason is to do with the way that SQL server compares strings with trailing spaces, it follows the ANSI standard and so the strings '1101' and '1101 ' are equivalent.
See the following for more details:
INF: How SQL Server Compares Strings with Trailing Spaces
I think you have to use LTRIM() and RTRIM() function while comparing like :
LTRIM(RTRIM(MYCOLUMN))='1101'
Also LEN function does not count spaces, it only count characters in string. Please refere : http://msdn.microsoft.com/en-us/library/ms190329%28SQL.90%29.aspx

Resources