Using like to query 100% key word in SQL Server - 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.

Related

Remove special character from customerId column in 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.

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.

Sql Server: How to search for literal percent character in like clause

I am using Hibernate Criteria with sql server 2000. I want to use the like method to get records from the database that are literal %. Can that be done?
example:
table base_target_unit:
id code name
8a91b32c3be0a5fe013c03cdb37f0002 13010410 户
8a91b32c3af8cd71013bd4fe06160044 12122609 %
8a91b32c3af8cd71013bd106e9dc002e 12122507 元/MB
402881b03a8c83ae013a8ca467f70041 12102399 亿分钟
402881b03a8c83ae013a8ca451610040 12102398 万分钟
402881b03a8c83ae013a8ca42cb3003f 12102397 万户
The sql I have is:
select * from dbo.base_target_unit where name LIKE '%/%%' ESCAPE '/'
I want to get the record that name which is %.
Use brackets. So to look for 88%
WHERE MyColumn LIKE '%88[%]%'
You can escape the percent sign with the SQL LIKE function by using two percents (%%)
This tells you more about how to do it:
http://msdn.microsoft.com/en-us/library/aa933232%28v=sql.80%29.aspx
select * from dbo.base_target_unit where name LIKE '%%' ESCAPE '%'

How to escape quotes in strings in vertica (vsql)?

So I need to insert some values into a vertica database (via vsql), which may contain quotes and all sorts of special characters. But vertica does not seem to understand character escaping. For example:
rpt=> select "asdasda\"asdasdad" from some_table limit 1;
rpt"> ";
ERROR: syntax error at or near "" from some_table limit 1;
"" at character 26
LINE 1: select "asdasda\"asdasdad" from some_table limit 1;
This is not the insert statement, but you should get the idea.
Well, first off I should have used single quotes. Escape sequences used to work in earlier versions (before 4.0 I believe), but now they are off by default. If you do not want to tweak database config parameters you have two options.
Use E' syntax:
select E'somethin\' here' from v_catalog.dual_p;
Or double the quotes that need to be escaped:
select 'somethin'' here' from v_catalog.dual_p;

Strange behavior of sql server

I have following table structure in my DB
ID Name
--------------------------
ID_1 Name1
ID1 Name2
when I execute the following query
SELECT * FROM tblNames
WHERE ID LIKE '_1'
I get both records selected...
any suggestions how to get rid off it??
An underscore (_) is a special character in LIKE expressions that matches a single character.
To match an actual underscore, you need to escape it:
select * from tblNames where id like '%\_1' escape '\'
Here I'm telling SQL Server to consider backslash an escape character, and using it to escape the underscore.
Alternatively, you can represent the underscore as a character range with a single character in it - it will be interpreted literally in this case:
select * from tblNames where id like '%[_]1'
Which is a bit more succinct.
Reference: MSDN.
LIKE operator
Quickie: _ is a wildcard character that matches anything.
SELECT * FROM tblNames
WHERE ID LIKE '\_1' ESCAPE '\'
SELECT * FROM tblNames
WHERE ID LIKE '[_]1'
The underscore character is a wildcard for the like operator, and it matches any one character. In your case the pattern '_1' matches both '_1' and 'D1' in the data.
To use a literal underscore character in a pattern you need to put brackets around it so that it becomes a character range containing only one character:
SELECT * FROM tblNames
WHERE ID LIKE '[_]1'

Resources