Repetition of expressions in SQL LIKE - sql

Is there a way to express repetition in SQL LIKE.
I have the following query that matches 4 or 5 digits, then a hyphen, and then 8 additional digits:
SELECT * FROM Table
WHERE Field LIKE '[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
OR Field LIKE '[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
In other languages I would write something like this: [0-9]{4-5}-[0-9]{8}.
Is there a way to simplify the above expression in SQL?

This looks like SQL Server. You could construct the strings:
select '[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]',
replicate('[0-9]', 4) + '-' + replicate('[0-9]', 8)
However, SQL Server doesn't have built-in regular expression support.

Below are the equivalent regular expressions,
Oracle
`REGEXP_LIKE(X, '^[[:digit:]]+$');`
PostgreSql
`like '%[1-9]%' `

Related

How to use LIKE in WHERE clause to get first 5 characters of variable?

I have a variable varchar that always takes in 10 digits. How can I use the LIKE operator to find/use only the first 5 digits of the variable?
my query:
variable IN VARCHAR2
SELECT * FROM items WHERE name LIKE SUBSTRING(variable, 1, 5)
... WHERE name LIKE '12345%'
will match any string that starts 12345. the '%' is a wildcard. You can also use the wildcard to match anywhere in the string: ... WHERE name LIKE '%12345%' will match a string with 12345 anywhere within it.
Edit for completeness: WHERE name LIKE '%12345' will match any string that ends with those five characters.
Try this:
SELECT * FROM items WHERE name LIKE (SUBSTRING(variable, 1, 5) + '%')
I guess you can use LEFT() like this:
SELECT * FROM items WHERE LEFT(name,5)=LEFT(variable,5);
Or if you you want to use LIKE with a wildcard, you can do this:
SELECT * FROM items WHERE name LIKE CONCAT(LEFT(variable,5),'%')
A few more example in the Demo fiddle
Edit: The above solution is for MySQL/MariaDB because earlier the tag of this question have MySQL but it's also my fault for not recognizing OP description of the datatype VARCHAR2. I might as well just post a suggestion related to the rdbms.
So, my first suggestion there using LEFT() however Oracle don't have that function, therefore:
SELECT * FROM items WHERE SUBSTR(name,1,5)=SUBSTR(variable,1,5);
or using concatenation operator
SELECT * FROM items WHERE name LIKE SUBSTR(variable,1,5)||'%'
Demo fiddle

Special Pattern by SQL LIKE clause

Need special pattern for finding values that are 13 length chars, first of 12 are numbers for example 119910023525P
There are 2 Pattern :
LIKE '____________P'
or
LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]P'
Need something as '[0-9]{12}P'
It's Possible In MS-SQL Server ?
There are no quantifiers in the TSQL pattern syntax.
You can use
LIKE REPLICATE('[0-9]',12) + 'P'
You could use this logic:
(x like '%P' and x not like '%[^0-9]%P' and len(x) = 13)
But that is about the same amount of typing as just manually replicated [0-9] 12 times.
Maybe the REGEXP() function will help you with this task.

SQL operator Like

IN SQL can you use the like operator to see if the first 2 letters of a value match the last 2 letters in another value ?
If a have attribute year and attribute phone number , i want to select these rows
which match the description above eg: year(1996) and phone number(9655771243);
No, at least not in a reasonable way. Most SQL engines support left() and right(), so you can do:
where right(year, 2) = left(phone_number, 2)
Databases that don't have other substring functions that do the same thing.
Using like, it would be something like:
where phone_number like right(year, 2) || '%'

SQL : REGEX MATCH - Character followed by numbers inside quotes

I have a column in sql which holds value inside double quotes like "P1234567" , "P1234" etc..
I need to identify only columns which start with letter P and is followed by seven digits (numbers) only. I tried where column like'"P[0-9][0-9][0-9][0-9][0-9][0-9][0-9]"' but it doesn't seem to work.
Can someone please correct me or point me to a thread which can help me out?
Thanks
Standard SQL has no regex support, but most SQL engines have regex extensions added to them on top of the standard SQL. So, for example, if you're using MySQL then you'd do this:
... WHERE column REGEXP '^"P[0-9]{7}"'
And if you're using Postgres then that would be:
... WHERE column ~ '^"P[0-9]{7}"'
(updated to match the double-quote part of the question, I'd misunderstood that to begin with)
How about using length and isnumeric:
Select
*
from
mytable
where
mycolumn like '"P%'
and len(mycolumn) = 10 --2 chars for quotes + 1 for 'P' + 7 for the digits
and isnumeric(substring(mycolumn, 3, 7))=1
This answer is for SQL Server, other DBMS's may have a different syntax for length

Regular expression to match a string in sql

How to write a regular expression to match a string if at least 3 characters from the start are matching?
Here is how my SQL query looks right now -
SELECT * FROM tableName WHERE columnName REGEXP "^[a-zA-Z]{3}someString";
You cannot use CONCAT or alike with REGEX, it will fail. Easiest way to do it, is:
$query = 'SELECT * FROM Test WHERE colb REGEXP "^'.substr($mystring,0,3).'"');
Another is:
SELECT * FROM Test WHERE LEFT(colb, 3) LIKE "{$mystring}%"
Please use jQuery and jqSQL plugin. Note that symbol $ must be escaped in SQL query with this plugin.