SQL Alphanumeric Select - sql

I apologize if this has been covered previously. I could not find exactly what I was looking for by searching. So I hope it's okay that I ask.
I have a column with many different types of alphanumeric values (e.g. A101, F576, AI01, etc.). What I'm wanting to do is find members of a specific portion with a specific pattern, F100 through F9999. Using between F1000 and F9999 gets me what I need. But, I don't think that's exactly the right way to go about querying for future reference.
Does anyone have any suggestions? Any help would be greatly appreciated!

This should do the trick:
SELECT * FROM THE_TABLE
WHERE THE_COLUMN LIKE 'F[1-9][0-9][0-9][0-9]'
That will match F1000 through F9999 The key point is you can use [0-9] as a range. If you want to do other patterns things like [A-Z] work too.
If you want F100 to F9999 you could do:
SELECT * FROM THE_TABLE
WHERE THE_COLUMN LIKE 'F[1-9][0-9][0-9][0-9]'
OR THE_COLUMN LIKE 'F[1-9][0-9][0-9]'

Related

BigQuery REGEXP_REPLACE referencing capture group in the replacement expression

I'm very new to Regex so this may seem a very dumb question.
I've been playing around with captured groups in Google Sheets, without any problems, but when I try and apply it to BigQuery, it doesn't seem to work and I can't find out how to implement the syntax.
I looked round and this seems to be the closest answer, but I can't make it work:
Find and replace using regular expression, group capturing, and back referencing
I want to reference a capture group in the replacement expression to either extract or replace £ 1,000.23 in this text:
random text £ 1,000.23 other text
I've got 3 groups:
(.+)
(£\ *[\d\.\,]+)
(.+)
It may not be the best example, but I really want to understand how to use a capture group in the replacement part so I'm not looking for an alternative solution.
The code below literally returns '$2' rather than '£ 1,000.23'.
SELECT
note,
REGEXP_REPLACE(note,r'(.+)(£\ *[\d\.\,]+)(.+)','$2') AS note2
FROM
`project.dataset.table`
LIMIT
100
Thanks for any help!
According to the replacement note in the doc, I think the following should work:
SELECT
note,
REGEXP_REPLACE(note,r'(.+)(£\ *[\d\.\,]+)(.+)','\\2') AS note2
FROM
`project.dataset.table`
LIMIT
100

Replace function, keep unknown substrings/wildcards

I have tried looking for answers online, but I am lacking the right nomenclature to find any answers matching my question.
The DB I am working with is an inconsistent mess. I am currently trying to import a number of maintenance codes which I have to link to a pre-existing Excel table. For this reason, the maintenance code I import have to be very universal.
The table is designed to work with 2-3 digit number (time lengths), followed by a time unit.
For example, SERV-01W and SERV-03M .
As these used to be added to the DB by hand, a large number of older maintenance codes are actually written with 1 digit numbers.
For example, SERV-1W and SERV-3M.
I would like to replace the old codes by the new codes. In other words, I want to add a leading 0 if only one digit is used in the code.
REPLACE(T.Code,'-[0-9][DWM]','-0[0-9][DWM]') unfortunately does not work, most likely because I am using wildcards in the result string.
What would be a good way of handling this issue?
Thank you in advance.
Assuming I understand your requirement this should get you what you are after:
WITH VTE AS(
SELECT *
FROM (VALUES('SERV-03M'),
('SERV-01W'),
('SERV-1Q'),
('SERV-4X')) V(Example))
SELECT Example,
ISNULL(STUFF(Example, NULLIF(PATINDEX('%-[0-9][A-z]%',Example),0)+1,0,'0'),Example) AS NewExample
FROM VTE;
Instead of trying to replace the pattern, I used PATINDEX to find the pattern and then inject the extra '0' character. If the pattern wasn't found, so 0 was returned by PATINDEX, I forced the expression to return NULL and then wrapped the entire thing with a further ISNULL, so that the original value was returned.
I find a simple CASE expression to be a simple way to express the logic:
SELECT (CASE WHEN code LIKE '%-[0-9][0-9]%'
THEN code
ELSE REPLACE(code, '-', '-0')
END)
That is, if the code has two digits, then do nothing. Otherwise, add a zero. The code should be quite clear on what it is doing.
This is not generalizable (it doesn't add two zeros for instance), but it does do exactly what you are asking for.

How do I declare priority in SQL statements?

My question seems to be quite simple, but I'm worried the answer might actually be somewhat complex. I am trying to perform a simple Select query that behaves like the following.
Here is the code:
SELECT * FROM tbl_tbl WHERE tbl_tbl.colA LIKE '%foo%' OR tbl.tbl.colA LIKE '%oof%' AND
tbl_tbl.colB LIKE '%bar%' OR tbl_tbl.colB LIKE '%rab%'
So I am just searching for 4 strings (2 in each column), and if I find one in each pair, I want to show that entire entry.
Mathematically, it makes quite a bit of sense to me.
I want to do (This OR That) AND (One OR Another) where any combination of This/One, This/Another, etc. passes the expression.
Pretty simple right?
How do I tell SQL to work right (you know, like that obscure way in my mind)?
Currently, I'm getting entries out of my table where only 1 of the column disciplines match, and that's not giving me the specificity of the priority I am looking for.
You would express it using parentheses and boolean logic in the where clause:
SELECT *
FROM tbl_tbl t
WHERE (t.colA LIKE '%foo%' OR t.colA LIKE '%oof%') AND
(t.colB LIKE '%bar%' OR t.colB LIKE '%bar%');
Do note that this is based on your example in the question. The second clause of the AND has two conditions that are the same. I assume this is a typo in the question, but not knowing the right pattern, I've left it in the answer.

Use the regular expression feature of Oracle to find a double vowel sequence, eg. ‘ie’, ‘ee’, ‘oa’

Use the regular expression feature of Oracle to find all movies whose title includes a word with a double vowel sequence, ‘ie’, ‘ee’, ‘oa’. I know it's something to do with the like clause but the order of the code is what's giving me trouble. Any help appreciated.
"table_name movies"
SELECT *
FROM movies
WHERE REGEXP_LIKE (title, ‘ie’, ‘ee’, ‘oa’);
You have identified the correct function, but it looks like you don't know how to formulate regular expressions. You should read up about regular expressions in a general sense, outside the context of Oracle. There are various dialects of regex, but once you learn one dialect, it should be easy switch to others.
For your specific Oracle question:
select * from movies where regexp_like( title, '[aeiou]{2}' );
This will only work for ms-sql. Leaving the answer in case anyone need it.
where title like '%[aeiou][aeiou]%'

Faceted Search in Coldfusion and SQL?

I'm working on a faceted search in Coldfusion and SQL. I've tried creating a query like this:
SELECT * FROM Turbos
WHERE
PartNumber LIKE '%#trim(SearchCriteria)#%'
OR PartDescription LIKE '%#trim(SearchCriteria)#%'
AND (PumpingSpeed BETWEEN #minimum# AND #URL.speed#)
AND InletFlange LIKE '#URL.inlet#'
AND Bearing LIKE '#URL.bearing#'
AND Condition LIKE '#URL.condition#'
The problem is the server is returning rows that don't contain EVERY piece of data I'm supplying. How can I select ONLY those rows which contain all the criteria?
just wrap the OR bit in brackets:
(PartNumber LIKE '%#trim(SearchCriteria)#%' OR PartDescription LIKE '%#trim(SearchCriteria)#%') AND...
at the moment you have A or B and C which is being read as A or (B and C). You want (A or B) and C.
And make sure you use cfqueryparam as suggested above.
Just a guess really, but change
...'%#trim(SearchCriteria)#%' OR
PartDescription ...
to
...'%#trim(SearchCriteria)#%' AND
PartDescription ...
edit
or are you saying some have null values?
post comment edit
Imagine for example that InletFlange is empty for your part, and the user didn't put it in their search.
Then InletFlange LIKE '#URL.inlet#' will compare "" LIKE "", which is of course true, so the product shows up.
There are many ways to solve this. For example
Default the search criteria to "N/A" or something
Default the column in the database in a similar fashion.
something along the lines of AND NOT ISNULL(InletFlange,'')=='' for each criteria
Okay, I found the answers:
Leigh: +1 to clarifying the question. (It is
unlikely this has any bearing on the
results, but what is the point of
using LIKE without wildcards "%"? If
it is an equality comparison you are
after, just use equals ie ... AND
Condition = '#URL.condition#' )
You were quite right, I was missing the percent symbols in my LIKE comparisons (DUH!). I removed them earlier thinking they were the problem, but they weren't, this was:
Aiden: just wrap the OR bit in brackets:
(PartNumber LIKE '%#trim(SearchCriteria)#%' OR PartDescription LIKE '%#trim(SearchCriteria)#%') AND...
Thank you Aiden!
While this isnt an answer to your question - you might want to consider using a dedicated search solution such as Solr (which is included with CF9). Its a more powerful fulltext solution than just raw SQL and includes facetting