Delete rows if letters in cell SQL Server - sql

I needed some guidance as to how I can exlude rows that contains any form of letter in a cell that is supposed to contain integers only.
This is what the data looks like now:
CustomerID
----------
ea176680
27906857
62675436
62566247
1bed413e
61110053
61113323
34441416
AS you can see in the table, the first,ID contains letters. How can I remove complete rows if a cell were to contain any form of letters?

How about this where clause?
where CustomerId not like '%[^0-9]%'
It filters out any values that have a non-digit. Unfortunately, SQL Server doesn't support regular expressions, but this will do what you want.
If you specifically wanted to look for letters, you can do:
where CustomerId like '%[a-zA-Z]%'

SQL Server can match on a specific pattern in your statement using like. It looks somewhat like regex, but isn't exactly that.
Use this statement if you want to match on all CustomerIds that contains characters in the range A-Z and a-z:
select *
from table
where CustomerID like '%[A-za-z]%'

Related

SQL check if row value in one column is in another column that is a list of strings

I am pulling from a table that looks something like this:
Name
List
John
'John,Mary,Fred'
Mary
'Jack, John'
I need to pull only the rows where the value in the Name column is in the comma-separated list of names in the List column. In the example above, it would return just the first row. How can I do this using SQL (AWS Redshift version)?
This work?
SELECT *
FROM <table>
WHERE list LIKE '%'||name||'%'
This has a hazard that 'jon' will match 'jonas'. You can fix this will a bit extra conditions. Also do you need this to be case insensitive? Then use ILIKE
If you need to match complete names you need to cover the 4 cases of how a name can show up in a list - first in list, middle of list, end of list, or all of the list. Like so:
SELECT *
FROM <table>
WHERE list LIKE name||',%'
OR list LIKE '%,'||name||',%'
OR list LIKE '%,'||name
OR list LIKE name

Teradata Character Column with non alphabet values

I have a name column in Teradata that has customer full name all in one column. There are some names with -,_,.,/,#,! in between the name characters. I want to be able to pull records where there are names with these conditions. Is there a better option to pull records with the scenario below?
Currently, I am writing query like this
SELECT NAME FROM TABLESOURCE WHERE NAME LIKE ANY('%-%','%.%','%#%','%~%','%!%')
Thanks in advance.
I haven't tested this but I think you could test for equality when those characters are removed from the name using otranslate
select name
from tablesource
where name <> otranslate(name,'-.#~!','')

check if column contains one of many values

i have an array of strings and a column which may contain one or more of those strings(seperated by space) i want to get all rows where this column contains one of the strings. Since the values all have 3 letters and therefore can't contain each other, i know i could just write
SELECT * FROM table WHERE
column LIKE '%val1%' OR
column LIKE '%val2%' OR
column LIKE '%val3%' OR
column LIKE '%val4%'
But i'm wondering if there isn't an easier statement, like column IN ('val1', 'val2', 'val3', 'val4') (This one seems only to work when the entry is equal to one of the values, but not if it just contains them)
Try reading this Is there a combination of "LIKE" and "IN" in SQL? and Combining "LIKE" and "IN" for SQL Server , this will solve you question.
Something like this from the first link.
SQL Server:
WHERE CONTAINS(t.something, '"bla*" OR "foo*" OR "batz*"')
Ist oracle you could use regular expressions
select *
from table
where regexp_like (column,'val(1|2|3|4)')

SQL - just view the description for explanation

I would like to ask if it is possible to do this:
For example the search string is '009' -> (consider the digits as string)
is it possible to have a query that will return any occurrences of this on the database not considering the order.
for this example it will return
'009'
'090'
'900'
given these exists on the database. thanks!!!!
Use the Like operator.
For Example :-
SELECT Marks FROM Report WHERE Marks LIKE '%009%' OR '%090%' OR '%900%'
Split the string into individual characters, select all rows containing the first character and put them in a temporary table, then select all rows from the temporary table that contain the second character and put these in a temporary table, then select all rows from that temporary table that contain the third character.
Of course, there are probably many ways to optimize this, but I see no reason why it would not be possible to make a query like that work.
It can not be achieved in a straight forward way as there is no sort() function for a particular value like there is lower(), upper() functions.
But there is some workarounds like -
Suppose you are running query for COL A, maintain another column SORTED_A where from application level you keep the sorted value of COL A
Then when you execute query - sort the searchToken and run select query with matching sorted searchToken with the SORTED_A column

Matching the beginning characters in a LIKE clause

Suppose in my like clause for my query, i would like to match all records that begins with the characters 'Harris' in my specified column. But their can also be names such as 'Harrison' that I would also like to match. How would I go about finding this
select *
from MyTable
where somecol LIKE 'Harris%'