Selecting only integer data within a column with characters - sql

I am trying to write a query that will select items with only integer data such as: 019280498 as opposed to 0129830INK. These are sku's in a product database and my current solution is something to this effect:
Column name is SKU
SELECT *
FROM mydb
WHERE SKU not like '%a%' or SKU not like '%b%' or SKU not like '%c%' ..... or SKU not like '%z%'
This would only return values with no characters in them. However it does not like what I have written and i'm sure there is a more elegant way to execute this.

Using your original method with like, you can do:
SELECT *
FROM mydb
WHERE SKU not like '%[^0-9]%';
This has the advantage that expressions like '3e9' are not accepted as a numeric value.
Or, if is specifically alphabetic characters that you want to keep out:
SELECT *
FROM mydb
WHERE SKU not like '%[a-z]%'; -- or for case sensitive collations '%[a-zA-Z]%'

There's an IsNumeric() function that could be used.
Select *
from mydb
Where IsNumeric(sku) = 0x1

SELECT DISTINCT Pincode
FROM ps_all_india_pin_code
WHERE Pincode between '100000' and '999999'

Related

DB2 efficient select query with like operator for many values (~200)

I have written the following query:
SELECT TBSPACE FROM SYSCAT.TABLES WHERE TYPE='T' AND (TABNAME LIKE '%_ABS_%' OR TABNAME LIKE '%_ACCT_%')
This gives me a certain amount of results. Now the problem is that I have multiple TABNAME to select using the LIKE operator (~200). Is there an efficient way to write the query for the 200 values without repeating the TABNAME LIKE part (because there are 200 such values which would result in a really huge query) ?
(If it helps, I have stored all required TABNAME values in a table TS to retrieve from)
If you are just looking for substrings, you could use LOCATE. E.g.
WITH SS(S) AS (
VALUES
('_ABS_')
, ('_ACCT_')
)
SELECT DISTINCT
TABNAME
FROM
SYSCAT.TABLES, SS
WHERE
TYPE='T'
AND LOCATE(S,TABNAME) > 0
or if your substrings are in table CREATE TABLE TS(S VARCHAR(64))
SELECT DISTINCT
TABNAME
FROM
SYSCAT.TABLES, TS
WHERE
TYPE='T'
AND LOCATE(S,TABNAME) > 0
You could try REGEXP_LIKE. E.g.
SELECT DISTINCT
TABNAME
FROM
SYSCAT.TABLES
WHERE
TYPE='T'
AND REGEXP_LIKE(TABNAME,'.*_((ABS)|(ACCT))_.*')
Just in case.
Note, that the '_' character has special meaning in a pattern-expression of the LIKE predicate:
The underscore character (_) represents any single character.
The percent sign (%) represents a string of zero or more characters.
Any other character represents itself.
So, if you really need to find _ABS_ substring, you should use something like below.
You get both rows in the result, if you use the commented out pattern instead, which may not be desired.
with
pattern (str) as (values
'%\_ABS\_%'
--'%_ABS_%'
)
, tables (tabname) as (values
'A*ABS*A'
, 'A_ABS_A'
)
select tabname
from tables t
where exists (
select 1
from pattern p
where t.tabname like p.str escape '\'
);

select TableData where ColumnData start with list of strings

Following is the query to select column data from table, where column data starts with a OR b OR c. But the answer i am looking for is to Select data which starts with List of Strings.
SELECT * FROM Table WHERE Name LIKE '[abc]%'
But i want something like
SELECT * FROM Table WHERE Name LIKE '[ab,ac,ad,ae]%'
Can anybody suggest what is the best way of selecting column data which starts with list of String, I don't want to use OR operator, List of strings specifically.
The most general solution you would have to use is this:
SELECT *
FROM Table
WHERE Name LIKE 'ab%' OR Name LIKE 'ac%' OR Name LIKE 'ad%' OR Name LIKE 'ae%';
However, certain databases offer some regex support which you might be able to use. For example, in SQL Server you could write:
SELECT *
FROM Table
WHERE NAME LIKE 'a[bcde]%';
MySQL has a REGEXP operator which supports regex LIKE operations, and you could write:
SELECT *
FROM Table
WHERE NAME REGEXP '^a[bcde]';
Oracle and Postgres also have regex like support.
To add to Tim's answer, another approach could be to join your table with a sub-query of those values:
SELECT *
FROM mytable t
JOIN (SELECT 'ab' AS value
UNION ALL
SELECT 'ac'
UNION ALL
SELECT 'ad'
UNION ALL
SELECT 'ae') v ON t.vame LIKE v.value || '%'

SQL Server query on nvarchar with asterisk in data and in where clause

I have a table Product which looks like this:
Name (nvarchar(100), not null)
It has data that looks like this:
4503-U**F19
When I query like this:
select * from Product where Name = '4503-U**F19'
I get nothing back... what gives?
Is there some special way to escape a where clause that contains an asterisk?
I'm probably staring at the answer and can't see it.
Have you tried the N in the prefix?
select * from Product where Name = N'4503-U**F19'

SQL search on part of column

Let's say I have multiple 6 character Alphanumeric strings. abc123, abc231, abc456, cba123, bac231, and bac123.
Basically I want a select statement that can search and list all the abc instances.
I just want a select statement that can list all instances with keyword "abc".
Use LIKE and wildcards %
SELECT *
FROM yourtable
WHERE yourfield LIKE '%abc%'
Input
yourfield
abc123
abc231
abc456
cba123
bac231
bac123
Output:
yourfield
abc123
abc231
abc456
SQL Fiddle:
Could you try like this way
SELECT *
FROM yourtable
WHERE field LIKE '%a%' Or field LIKE '%b%' Or field LIKE '%c%'
Select * from tablename
where yourfield like 'yourconstantcharacters%'
You can use LIKE operator in sql
Starting With Case
Select * From Table Where Column LIKE 'abc%';
Ending With Case
Select * From Tablo Where Column Like '%abc';
Contains With Case
Select * From Table Where Column LIKE '%abc%';
So If you want to use escape character in LIKE condition , you must make small changes on your condition
For Example :
SELECT * FROM Table WHERE column LIKE '!%' escape '!';
More information for you is here

How to properly use LIKE statement in sql query

I have database which contains a field named groupid and group name.
sample data
groupid groupname
123 abc
234 bcr
1237 cde
I like to compare groupid with another inputted data, its size is greater than size of group id.
I tried a query that not return correct answer
SELECT *
FROM mydata
WHERE groupid LIKE '12309098';
My expected answer is abc
What are the changes to made for correct answer
thanks in advance
Since you want the value in the row to be the prefix of your input and not the other way around, you can just turn LIKE around the other way;
SELECT *
FROM mydata
WHERE '12309098' LIKE CONCAT(groupid, '%');
An SQLfiddle to test with;
EDIT: Since you asked about SQLite, there you need to use || for concatenation;
SELECT *
FROM mydata
WHERE '12309098' LIKE `groupid` || '%';
Another SQLfiddle.
You could do like below:
SELECT *
FROM my data
WHERE '12309098' LIKE CONCAT('%', groupid, '%');
You could use something like this. But it is highly subjective to your SQL Server. For Oracle following syntax should be fine
SELECT *
FROM mydata
WHERE groupid LIKE SUBSTR('12309098',1, LENGTH(groupid));
If you are sure that input data will start with 123, then:
SELECT * FROM mydata WHERE groupid LIKE '123%';
If you are sure that input data will contain 123, then:
SELECT * FROM mydata WHERE groupid LIKE '%123%';