Special Pattern by SQL LIKE clause - sql

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.

Related

Repetition of expressions in SQL LIKE

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]%' `

In SQL, how can we match using LIKE up to but not exceeding a number of wildcard characters?

E.g. in regex, we can use {n,m} to specify that we want to match the previous element at least n times, but no more than m times.
Is there a way to do this in SQL (specifically sqlite) whereby we match a wildcard '_' up to 5 times but no more?
E.g. I want up to 5 characters between the letters j and z
So I would get Jaz or Jaaaaaz but not Jaaaaaaz
Thanks!
You can add a regexp module into SQLite. That is probably the best solution to your problem.
You can do what you want as:
where col not like '______' -- 6 underscores
Or, more simply:
when length(col) < 6
That is the direct answer to your question.
EDIT:
For up to five characters, you can do:
where col like 'j%z' and
col not like 'j______z'
Or use the more brute force:
where col like 'jz' or
col like 'j_z' or
col like 'j__z' or
col like 'j___z' or
col like 'j____z' or
col like 'j_____z'
If you want these patterns within a string (rather than the whole string), then include % at the beginning and end of the pattern.
To answer your specific question, you could just use
WHERE col LIKE 'J%z' AND LENGTH(col) < 8
e.g.
SELECT 'Jaaaz' LIKE 'J%z' AND LENGTH('Jaaaz') < 8, 'Jaaaaaaz' LIKE 'J%z' AND LENGTH('Jaaaaaaz') < 8
Output
1 0

how to use the sql character array wilcard?

Hi I am using SQL DB2 and trying to find all values in a column that start with F25 and end with any character that does not contain ..
So for example the following are allowed
F2501
F25AB
F25ab
F25A.
I had imagined I could do something like:
where col like 'F25[!..]'
however this reutrns no rows where I know such codes exist i.e. F25AB.
what am I doing wrong here?
Neither [ nor ! is a wildcard in SQL.
The only wildcards that SQL LIKE supports are % for any number of characters and _ for a single character.
To do what you want, use
where col LIKE 'F25%' -- starts with F25
and col NOT LIKE '%..' -- does not end with ..
Try regular expresión by using xquery. Or if you hace db2 luw 11.1 you can use regular expresión functions directly from SQL.

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

How to select all the string characters preceding a . in oracle

I am using Oracle 11 G and have the following set of data:
12.0
4.2
Version.1
7.9
abc.72
I want to return all string characters before the period. What sort of query would I run in order to achieve this? Any help would be greatly appreciated, thanks!
You can try a combination of instr and substr.
Something like this:
select substr(field, 1, instr(field, '.') - 1)
from your_table;
Assuming field always contains a . character on it.
You can also deal with strings without a . by using case, if or any other similar valid conditional function on Oracle's SQL language implementation.
Of course, you can always put this on a function to make it look nicer on your query.