DB2 LIKE matching Pattern - sql

My question is simple.
In a DB2 Database, I have my table
Table (Id integer, name varchar)
I want to select entries which names like 'ac1%' or 'ac2%' or 'ac3%', so which names match regex
'^ac[123]*'
Is there any method to have this select query without write :
WHERE name LIKE 'ac1%' OR name LIKE 'ac2%' OR name LIKE 'ac2%'

Probably the most efficient method is:
where name >= 'ac1' and
name < 'ac4'
You can also use regular expressions:
where regexp_like(name, '^ac[1-3]')

Depending on your Db2-server platform (Z/OS, i-Series, Linux/Unix/Windows) and Db2-version, you can use REGEXP_LIKE function. This allows regular expressions in SQL.
See documentation and examples here.

i think below will work cause REGEXP_LIKE(col,'[0-9]{3}-[0-9]{3}-[0-9]{2}') will always return true if col contain any of 0 to 9 value
SELECT * FROM table1
WHERE REGEXP_LIKE(name,'[1-3]')

Related

SQL query for any value

I'm building SQL queries on the fly and am a bit of a beginner with them.
How would I do something like:
Select * from X where Type = *Any*
Basically, I want to select all of them. I know I could not include the where but often times the Type variable does in fact have values. I want to be able to replace the "Any" part with something else on the fly rather than a whole different expression...
I think this is what you are looking for:
--please change the datatype int to match your datatype for type column
DECLARE #type int = 1;
SELECT * FROM X
WHERE x.[Type] = COALESCE(#type,x.[type]);
If you don't pass a value in variable #type, it will default to your type and return everything. Otherwise, it will supply the variable value as needed. Hope this helps!
This kind of depends on what your SQL implementation is. That being said the two concepts you are missing are using the keyword "LIKE" instead of "=", and using wildcards. I advise going through W3Schools SQL section to get started, or better yet buy some introduction to SQL book. http://www.w3schools.com/sql/default.asp
SELECT * FROM X
WHERE Type LIKE '%';
select * from X where Type Like '%';
If you want everything it is simple:
SELECT *
FROM my_table
There is no where clause , in this case. You usually want to first try something like this , though:
SELECT *
FROM my_table
WHERE rownum < 2
But rownum is an Oracle-specific column. You need to look it up for your specific database .

Verify if the second character is a letter in SQL

I want to put a condition in my query where I have a column that should contain second position as an alphabet.
How to achieve this?
I've tried with _[A-Z]% in where clause but is not working. I've also tried [A-Z]%.
Any inputs please?
I think you want mysql query. like this
SELECT * FROM table WHERE column REGEXP '^.[A-Za-z]+$'
or sql server
select * from table where column like '_[a-zA-Z]%'
You can use regular expression matching in your query. For example:
SELECT * FROM `test` WHERE `name` REGEXP '^.[a-zA-Z].*';
That would match the name column from the test table against a regex that verifies if the second character is either a lowercase or uppercase alphabet letter.
Also see this SQL Fiddle for an example of data it does and doesn't match.
agree with #Gordon Linoff, your ('_[A-Z]%') should work.
if not work, kindly add some sample data with your question.
Declare #Table Table
(
TextCol Varchar(20)
)
Insert Into #Table(TextCol) Values
('23423cvxc43f')
,('2eD97S9')
,('sAgsdsf')
,('3Ss08008')
Select *
From #Table As t
Where t.TextCol Like '_[A-Z]%'
The use of '%[A-Z]%' suggests that you are using SQL Server. If so, you can do this using LIKE:
where col like '_[A-Z]%'
For LIKE patterns, _ represents any character. If the first character needs to be a digit:
where col like '[0-9][A-Z]%'
EDIT:
The above doesn't work in DB2. Instead:
where substr(col, 2, 1) between 'A' and 'Z'

ORACLE-way to use multiple parameter in wildcard (LIKE) [duplicate]

This question already exists:
Oracle-is there any way to use multiple parameter in wildcard [closed]
Closed 9 years ago.
I want to find out the name from database which names are start with A to M or is there any way to use multiple parameter in wildcard (LIKE) for eg-
SELECT name
FROM database
WHERE name LIKE (I want to search A to M here);
Try:
select ...
where ... substr(name,1,1) between 'A' and 'M'
Possibly you want to:
select ...
where ... upper(substr(name,1,1)) between 'A' and 'M'
This does have the advantage of potentially being indexable, although the index would probably only be used for much smaller ranges.
Use REGEXP_LIKE, refer here for more in detail.
Try like this,
SELECT name FROM database WHERE REGEXP_LIKE(name, '^[A-M]');
There's no need to use substr or regexp_like or like.
The following statement is much simpler (and has the advantage that it can use an ordinary index):
SELECT name FROM database WHERE name >= 'A' AND name < 'N';
This query works on the basis that 'AA' > 'A' and all strings starting with 'M' sort before 'N'.
As far as index usage is concerned, I'm presuming that since you want half of the alphabet as the first letter that you will also be querying for half of the total number of rows and hence Oracle will prefer a full-table scan.
SELECT name FROM database WHERE name LIKE '[A-M]'

Can SQL SUM() function take an expression as argument?

I'm using SQLite database and I'm wondering whether I'm allowed to write queries as follows:
SELECT SUM(column1 * column2)
FROM my_table;
I googled but references say that SUM function is has the following format:
SUM([DISTINCT|ALL] column)
And my question is: does column mean actually column or does it allow expressions (like above) too?
You can always use a table expression:
SELECT SUM(Calc)
FROM (
SELECT Column1 * Column2 AS 'Calc'
FROM My_Table) t
I don't have SQLite but checking the docs indicates this should work fine.
Yes, you can use an expression like the one you mentioned, if the datatype of both columns allow it.

How can I search for numbers in a varchar column

I've got a simple nvarchar(25) column in an SQL database table. Most of the time, this field should contain alphanumeric text. However, due to operator error, there are many instances where it contains only a number. Can I do a simple search in SQL to identify these cases? That is, determine which rows in the table contain only digits in this column. As an extension, could I also search for those column values which contain only digits and a space and/or slash.
In other languages (eg. Perl, Java) a regular expression would resolve this quickly and easily. But I haven't been able to find the equivalent in SQL.
yes you can achive this by using isnumeric function available in sql sever
check more at this link : http://msdn.microsoft.com/en-us/library/aa933213(SQL.80).aspx
All the answers referred to the isnumeric function, but they are not correct, I've mentioned in a comment for all the answers the flaw in the answers. The correct solution is to use a regular expression in your where clause, which contains not like '%[^0-9]%', see the example below:
select column_name from table_name where column_name not like '%[^0-9]%'
select column_name from table_name where IsNumeric(column_name) <> 1
Numeric Only:
SELECT * FROM Table WHERE ISNUMERIC(Field) = 1
With Space:
SELECT * FROM Table WHERE Field LIKE '% %'
With Slash:
SELECT * FROM Table WHERE Field LIKE '%/%'
Combined:
SELECT * FROM Table WHERE ISNUMERIC(Field) = 1 OR Field LIKE '% %' OR Field LIKE '%/%'