Hi, I need a SQL query each cell in a column must contain a least one alpha character (A-Z, a-z) - sql

I need help with a a SQL query where each cell in a column must contain a least one alpha character (A-Z, a-z).
I have tried different combinations of ISNUMERIC, LIKE & ISALPHABET & also searched google but cant work it out. IS Alphanumeric also does not work for this situation.
The input in the column is :
3
3-5
3 RYDE ST
RYDE ST
I want the output to be Row 1 & 2 as below, because these do not include an alpha character. I will then manually alter the cells to ensure they meet the condition to have at least one alpha character:
3
3-5

SELECT column_name
FROM table_name
WHERE column_name
LIKE '%[A-Za-z]%'
The % matches anything. So you are looking for anything followed by a letter followed by anything.
http://www.w3schools.com/sql/sql_wildcards.asp

Related

Select column ignore beginning numbers

I have a column that I need to select but it has an inconsistent amount of numbers/formatting in the beginning
The column values are ideally supposed to be structured like:
# Question_-_Answer
But here are some examples which make it hard to remove the numbers in the beginning
0 Question1_-_50-60
1.Question_-_apple
12Question_-_40/50
13 Question_-_orange
14.Question_-_apple
15. Question_-_orange2
Is there a way I can query this column so that it ignores everything until the first alphabetical character while also not removing any characters/alphanumerical values in the question and answer portion?
You can use PATINDEX and STUFF to achieve this:
SELECT STUFF(V.YourString,1,PATINDEX('%[A-z]%',V.YourString)-1,'')
FROM (VALUES('0 Question1_-_50-60'),
('1.Question_-_apple'),
('12Question_-_40/50'),
('13 Question_-_orange'),
('14.Question_-_apple'),
('15. Question_-_orange2'))V(YourString);
This removes all characters up to the first alpha character.

SQL Query with criteria of 6 chars and wildcards

I'm stuck with an SQL query to get what I need. My input is this:
COD SINOM
A 123456
B 987654, 123456, 111111
C 123456 , 234501
D 9912345699
E 99123456, 789012
F 77123456
Both fields are text type, even if they are numbers on it (we use text type because sometimes it has leading zeros). Mycriteria is 123456
I'm trying to query all COD where SINOM contains my criteria, but only as 6 chars in a single word. It does not matter if it has any leading blanks or commas or semicolons (all of them are possible), but len of string must be 6, and could be more text. And criteria can be at left, right or middle of SINOM.
My actual query is this:
SELECT Table1.cod, Table1.sinom
FROM Table1
WHERE (((Table1.sinom)="123456")) OR (((Table1.sinom) Like "*123456,*")) OR (((Table1.sinom) Like "*123456 *"))
But the output I'm getting is wrong:
The right ones would be only the first 3 rows (A,B,C). Rest of rows are wrong. My expected output would be:
9912345699, 99123456, 789012 and 77123456 are wrong because it contains 123456 but not as a single word.
Looking for an SQL query to apply this and trying to avoid the use of an VBA function if possible
Other SYNOM that would be correct would be:
998877, 123456, 029384
012310,123456
I hope this is clear, but please, do not hesitate to ask if any doubt arises.
Thanks!
I think you want something like this:
WHERE "," & REPLACE(Table1.sinom, " ", "") & ",") Like "*,123456,*"
The key is to look for the delimited strings -- but to be sure that the column has commas at the beginning and end.
One caveat here is the spaces. This removes the spaces, assuming they are merely spaces.
I might suggest something along the lines of the following:
select * from table1 where "," & table1.sinom & "," Like "*[!0-9]123456[!0-9]*"
The use of [!0-9] will account for the possibility of any delimiter surrounding the numerical values, and the concatentation with commas (which could be any non-numerical character) accounts for cases in which the number appears on its own or at the start or end of the string.

Underscore and LEFT function

I have a column that has values that look like the following:
17_data...
18_data...
1801151...data
The data isn't the cleanest in this columns, so I am trying to use a LEFT function to identify the rows that have the 2017 year followed by an underscore LEFT(column, 3) = '17[_]' This doesn't return a single column. So to troubleshoot, I added this WHERE clause to the SELECT statement to see what was getting returned, and I got the value 175 where the actual first three characters are "17_".
Why is this, and how can I structure my WHERE clause to pick up those rows?
When you tried adding 'where' with a rule of LEFT(column, 3) = '17[_]', it was doomed to fail. Operator '=' performs exact comparison: both sides must be equal. That is, it would look for rows whose first 3 characters (left,3) are equal to 17[_], that is, 5 characters, one, seven, bracket, underscore, bracket. Text of 3 characters will not exactly-match 5 characters, ever.
You should have written simply:
WHERE LEFT(column, 3) = '17_'
I guess that you've got the idea for adding a bracket from reading about LIKE patterns. LIKE operator allows you to look for strings contained at start/end/middle of the data.
WHERE column LIKE 'mom%' - starts with mom
WHERE column LIKE '%dad' - ends with dad
and so on. LIKE supports '%' meaning "and then text of any length", and also "_" meaning "and then just one character". This forms a problem: when you want to say "starts with _mom", you cannot write
WHERE column LIKE '_mom%'
because it would also match 9mom, Bmom, and so on, due to _ meaning 'any single character'. That's why in such cases, only in LIKE, you have to write the underscore in brackets:
WHERE column LIKE '[_]mom%' - starts with _mom
Knowing that, it's obvious that you could construct your 'starts with 17_' with LIKE as well:
SELECT column1, column2, ..., columnN
FROM sometable
WHERE column LIKE '17[_]%'

Pattern Matching with SQL Like with first part letters and second part numbers of varying length

Is there a way to use Pattern Matching with SQL LIKE, to match first part of letters and a second part of variable number of numbers?
For example, I want to select only ABC1002, ABC23, ABC569, CDE48569.
Here is one method:
where col like '[A-Z][A-Z][A-Z][0-9]%' and
col not like '[A-Z][A-Z][A-Z]%[^0-9]%'
The logic says:
The column starts with three letters and a digit.
Nothing other than a digit follows the three letters.

DB2 SELECT using alpha wildcard and the length of 4 characters

I need to do a SELECT for a product_id where the length the product_id is 4 characters long and the last character can be any letter. I know the wildcard for selecting any letters but I don't know how to denote that it has to be the last character and that the product_ids I'm looking for must be 4 characters long.
Does that make sense?
Have you tried RIGHT and standard LIKE? (There is a LEFT in DB2). If not, SUBSTR.
In DB2 you can use LENGTH
So hopefully
WHERE
RIGHT(product_id, 1) LIKE [A-Z] AND LEN(product_id) = 4