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.
Related
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.
In Snowflake there is a number column storing values like: 8,123,456. I am struggling determining how to structure select statement to return a value like: 00008123456.
In SQL SERVER you can do something like this:
right(replicate('0', 11) + convert(varchar(11), job_no), 11) AS job_no
I understand Snowflake to_varchar function can be used, but not sure how to structure like in SQL Server.
Just add a format string on to_varchar():
select to_varchar(8123456, '00000000000');
Give it try with: lpad(to_char(nos),11,0)
More examples & details: https://docs.snowflake.com/en/sql-reference/functions/lpad.html#examples
a solution provided by Greg Pavlik saved me.
I just switched from DB2 to Snowflake.
So, TO_VARCHAR( <numeric_expr> [, '<format>' ] ) worked.
Below is an Oracle script that I need to execute on an SQL Server.
SELECT
records.pr_id,
SUBSTR (REPLACE (REPLACE (XMLAGG (XMLELEMENT ("x", prad4.selection_value)
ORDER BY prad4.selection_value),'</x>'),'<x>',' ; '),4) as teva_role
FROM records
Thanks for the help,
Barry
I programmed in SQL for years in several environments and it is about 75% the same. So, the SQL statement should work as is, however the functions (REPLACE, SUBSTR) will be what you need to research and change.
Also, you get columns from prad4 without including it in the FROM statement which is a problem.
And, finally, your parentheses aren't balanced which, I would think, would be a problem in Oracle as well.
This is basically concatenating a set of strings with a delimiter. The common way to do this, is using FOR XML PATH('') which seems to be the equivalent of the combination of XMLELEMENT() in Oracle, but with a different syntax. You can also use XML functions to prevent change of certain characters not allowed in XML. The STUFF takes care of the SUBSTR() part of your code. For a more detailed explanation, you can read this article on Creating a comma-separated list.
The code should look similar to this:
SELECT records.pr_id,
STUFF(( SELECT ' ; ' + prad4.selection_value
FROM prad4
WHERE prad4.pr_id = records.pr_id
ORDER BY prad4.selection_value
FOR XML PATH(''), TYPE).value('./text()[1]', 'varchar(max)'), 1, 3, '')
FROM records;
Of course, with the improvements of SQL Server 2017, the code can be simplified to something like this:
SELECT records.pr_id,
STRING_AGG( selection_value, ' ; ') WITHIN GROUP (ORDER BY selection_value ASC)
FROM records;
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.
I am trying to find an example of a specific exploit in our sql server traces and I want to filter the TextData column for '%' occurring three times: %%%.
This means that I have to escape the special character %.
One try was to use '\' as an escape char like this: %\%\%\%%
Another try was to specify it as a range: %[%][%][%]%.
In SQL we can specify the escape character to use for the LIKE operator, but is there a way to do it when we filter in SQL Server Profiler?
-- Syntax for SQL Server and Azure SQL Database
match_expression [ NOT ] LIKE pattern [ ESCAPE escape_character ]
https://learn.microsoft.com/en-us/sql/t-sql/language-elements/like-transact-sql?view=sql-server-ver15