SQL Server regex alternation - sql-server

What would be the proper format for the following regular expression in SQL Server?
select top 100 *
from posts
where tags like '(c++)|(performance)'

SQL Server does not support formal regex, so you will have to use LIKE here:
SELECT TOP 100 *
FROM posts
WHERE tags LIKE '%c++%' OR tags LIKE '%performance%';
Note: If the tags column really stores one tag per record, then just use equality checks:
SELECT TOP 100 *
FROM posts
WHERE tags IN ('%c++', 'performance');

Related

Sybase like clause for matching a pattern between the string

I want to build a sybase ASE query to match lastname, firstname for a person. There are few different formats for name. It can be "lastname, firstname" OR it can be "lastname,firstname" (no space in between , and firstname). I have tried using name like 'lastname[,][ ]firstname' but it does not work. I can not use lastname,%firstname as it would match with any character for firstname. The valid character is either space or nothing. Any suggestions?
Unfortunately SAP/Sybase ASE does not provide support for regex patterns (eg, 'zero or more spaces'), so you're left with a few basic options ...
union (all) two queries:
select *
from names_table
where name like 'lastname, firstname'
union all
select *
from names_table
where name like 'lastname,firstname'
NOTE: Both queries should use an index on the name column assuming statistics show an index access plan is the best option.
or two where clauses:
select *
from names_table
where (name like 'lastname, firstname' or name like 'lastname,firstname')
NOTE: Whether or not this uses an index on the name column will depend on the statistics for the index and column and/or the complexity of the actual query.
Strip out spaces and match what's left:
select *
from names_table
where str_replace(name,' ',null) like 'lastname,firstname'
NOTE: In most cases this will disable the use of an index on the name column.
From an indexing perspective ...
If you need to run this type of query often, and the performance of said query is less than acceptable, you could look at a couple additional indexing options:
(materialized) computed column + index on said computed column
function-based index (ASE basically creates a 'system' computed column under the covers and then creates the index on said column)

Camel sql vs calling own bean for dynamic sql (variable columns where clause)

Using the camel sql component seems like a good thing in a project using camel. But i dont see the point for cases when dynamic sql is needed. Use case :
on front end user can
select a type of record only and submit search, in this case where clause is : "from table1 where col1 = valueX1"
also select a date range for offer start date so then where clause looks like "from table1 where col1 = valueX1 and dateCol between (...)"
and so on for other UI if values are given total of 10 different columns, in different combinations
I tried to use a dynamic sql figured out three choices:
1. using a receipient list so route is selected at run time, seemed over kill.
2. using the body as a sql and using the useMessageBodyForSql=true
3. using a custom prepareStatementStrategy
For 2 and 3 i was not able to send parameter names or specify headers or properties to be part of values to be used in Prepared statement.
For .2. had to give the sql like :
select c1, c2 ... from t1 where x = ? and y = ?
and then a java util list with the values in order.
So - is there any advantage to using this? Any feature of the sql component that makes it better to use than to directly use the spring jdbc template that it uses?
I would suggest to use Camel Templating to make the statements dynamic like that:
to("freemarker://sql/template.ftl")
.log("${body}")
.to("sql:ignored?useMessageBodyForSql=true");
Note that query parameters are represented by a ? instead of a # symbol if the statement comes from the body:
-- sql/template.ftl
select count(*) as count
from a_table
<#if headers.namePattern?has_content>
where name like :?namePattern
</#if>
You might also switch to the MyBatis component which supports advanced templating via MyBatis but this comes with a much higher overhead in terms of coding and configuration.

Mistake in like SQL Server with Arabic language

There is a database and in it a data table contains lots of values in Arabic language. When I use like, I get a wrong answer.
Code that I used
Select *
From customers
Where cusname like ''%جديد%
But kindly note the data already contains the values
Use the Unicode prefix (N)
Select * from customer where cusname like N'%جديد٪'

MS SQL RegEx finding 6 numbers exactly

I am trying to search a MS SQL database column for 6 numbers. The data in the column is a string of characters example: ab01234555cd0122abc987654efg
Using RegEx : [0-9]{6}
Results are : 012345 and 987654
Here is my current MS SQL code:
SELECT * FROM users WHERE seo LIKE '%[0-9]{6}%'
The {6} does not work.
How can I use a regex qualifier to match the characters count?
SQL Server doesn't actually have REGEX functionality. You would need to repeat the pattern six times:
SELECT * FROM users WHERE seo LIKE '%[0-9][0-9][0-9][0-9][0-9][0-9]%'
SQL Server LIKE is does not handle regular expressions, though it can do pattern matching. Since regexp is not supported, you cannot do things like [0-9]{6}. Instead, you need to repeat your pattern, which would look like this: [0-9][0-9][0-9][0-9][0-9][0-9].
So your query would be:
SELECT * FROM users WHERE seo LIKE '%[0-9][0-9][0-9][0-9][0-9][0-9]%';

Find columns that match in two tables

I need to query two tables of companies in the first table are the full names of companies, and the second table are also the names but are incomplete. The idea is to find the fields that are similar. I put pictures of the reference and SQL code I'm using.
The result I want is like this
The closest way I found to do so:
SELECT DISTINCT
RTRIM(a.NombreEmpresaBD_A) as NombreReal,
b.EmpresaDB_B as NombreIncompleto
FROM EmpresaDB_A a, EmpresaDB_B b
WHERE a.NombreEmpresaBD_A LIKE 'VoIP%' AND b.EmpresaDB_B LIKE 'VoIP%'
The problem with the above code is that it only returns the record specified in the WHERE and if I put this LIKE '%' it returns the Cartesian product of two tables. The RDBMS is Microsoft SQL Server. I would greatly appreciate if you help me with any proposed solution.
Use the short name plus appended '%' as argument in the LIKE expression:
Edit with info that we deal with SQL Server:
SELECT a.NombreEmpresaBD_A as NombreReal
,b.NombreEmpresaBD_B as NombreIncompleto
FROM EmpresaDB_A a, EmpresaDB_B b
WHERE a.NombreEmpresaBD_A LIKE (b.NombreEmpresaBD_B + '%');
According to your screenshot you had the column name wrong!
String concatenation in T-SQL with + operator.
Above query finds a case where
'Computex S.A' LIKE 'Computex%'
but not:
'Voip Service Mexico' LIKE 'VoipService%'
For that you would have to strip blanks first or use more powerful pattern matching functions.
I have created a demo for you on data.SE.
Look up pattern matching or the LIKE operator in the manual.
I would suggest adding a foreign key between the tables linking the data. Then you can just search for the one table and join the second to get the other results.

Resources