I have a table named emp like this:
And I want to get the enames which start with 'A' or 'B'. So I write the query like this:
select ename from emp where ename like '[AB]%'
But it return nothing instead of 'ALLEN' or 'BLAKE'. So I changed the query into:
select ename from emp where ename REGEXP '[AB]%'
But it still didn't work and said there is no such function name REGEXP. Is there anything wrong ?
The following link may help you when using REGEXP. Sqlite3 supports REGEXP only
How do I use regex in a SQLite query?
However, I think the solution can be like this and no regex is need to be used:
select ename from emp where ename like 'A%' or ename like 'B%'
You have to add it as a user-defined function.
It would be simpler to just rewrite your query without regex. I.e.
select ename from emp where ename like 'A%' or ename like 'B%'
Related
I have a table in ORACLE DB where there are employee names column (ENAME) with 5 letters. Query used was:
SELECT ENAME FROM EMP WHERE LENGTH(ENAME) = 5;
This is not returning names with 5 letters. Can someone please suggest me the correct one?
I think you need 1st trim then use length
SELECT ENAME FROM EMP WHERE
LENGTH (Trim(ENAME))=5
I guess you're looking to count the chars excluding the white spaces
SELECT ENAME
FROM YourTableName
WHERE LENGTH(REPLACE(ENAME, ' ', NULL)) = 5;
select ENAME from EMP where Ename LIKE '____%'
Use REGEXP_COUNT to count only letters
SELECT ENAME FROM EMP where regexp_count(ENAME, '[A-Z]') = 5 ;
REGEXP_COUNT complements the functionality of the REGEXP_INSTR function by returning the number of times a pattern occurs in a source string. The function evaluates strings using characters as defined by the input character set. It returns an integer indicating the number of occurrences of pattern. If no match is found, then the function returns 0.
SELECT ENAME FROM EMP WHERE ENAME LIKE ('_____');
When im executing a command like
select last_name
from table
where last_name in('%a%e%','%e%a%') ;
to retrieve all those rows where the last name has an a and an e in it, I'm not getting the result
The in() clause does not support wildcards. Use like. Then you can simplify that to:
select last_name
from your_table
where last_name like '%a%'
and last_name like '%e%'
You should use LIKE statement when using wildcards
SELECT last_name
FROM table
WHERE last_name LIKE '%a%e%' OR last_name LIKE '%e%a%'
When you use IN it doesn't do wildcard expansion and searches for exact string you put there. In your case for strings '%a%e%' and '%e%a%' and not for strings where a and e are present.
you should use like not in
select last_name
from table
where last_name like '%a%e%'
or last_name like '%e%a%' ;
Lets say my table is like,
data
----
smith700
then my output should be
name sal
----- ---
smith 700
I only can use SQL statement no PL/SQL statement is allowed...How to do it?
Use SubStr
SELECT SUBSTR('smith700',1,5),SUBSTR('smith700',6,3)
UPDATE
For varying lengths of numbers and characters you could try
SELECT REGEXP_REPLACE('smith700', '[A-Za-z]','') FROM DUAL;
SELECT REGEXP_REPLACE('smith700', '[0-9]') FROM DUAL;
select substr(data,1,5) name, substr(data,6,3) sal from your_tab;
I'm trying to query if the characters 'th' is in a column enames for the table emp.
I use the command
select ename from emp where ename like '%th';
However SQL says no row selected.
Any suggestions?
EDIT:
SOLVED
This worked in the end.
SELECT * FROM EMP WHERE ENAME LIKE '%TH%';
Thanks for help!
where ename like '%t%' or ename like '%h%';
or with a regular expression:
where regexp_like(ename, '[th]');
Try:
where UPPER(ename) like '%TH%';
What is the best way to handle optional parameter if null value is passed in select query in oracle
E.g. select * from emp where deptno = 10 and empno in (7782,7934)
In above query, if user will not select any empno then it should fetch all records where deptno = 10
Usually I handle this case in the programming environment that is issuing the query. Within the code, I check for the null case, and then exclude that part of the where clause as needed. Is that something you can do?
edit
If you were using ColdFusion:
<cfquery>
select
*
from
emp
where
deptno = 10
<cfif Len(empno)>
and empno in (<cfqueryparam value="#empno#" list="true" cfsqltype="cf_sql_integer">)
</cfif>
</cfquery>
(query params added for correctness)
Here is a little bit dirty solution for your problem if you just want to instert the value into the string:
SELECT *
FROM emp
WHERE
deptno = 10
AND (empno IN (%user_input%,%not_used_value%)
OR COALESCE(%user_input%,NULL) IS NULL);
Here %user_input% is a placeholder for values, selected by user and %not_used_value% is empno id, which is never used.
So, the resulting query could look like this:
SELECT *
FROM emp
WHERE
deptno = 10
AND (empno IN (7782,7934,-999)
OR COALESCE(7782,7934,NULL) IS NULL);