Replace LIKE by SUBSTR - sql

I tried to select the name of students that end with 'a'. I wrote this code:
Select name form students where name like '%a' ;
How can I get the same results using SUBSTR?

I actually think using RIGHT() would make the most sense here:
SELECT name
FROM students
WHERE RIGHT(name, 1) = 'a'
The above query would work on MySQL, SQL Server, and Postgres, but not Oracle, where you would have to use SUBSTR():
SELECT name
FROM students
WHERE SUBSTR(name , -1) = 'a'

Not all platforms accept negative start integers or length integers for SUBSTR()
Can you try if your DBMS supports the RIGHT() string function?
Works like this:
SQL>SELECT RIGHT('abcd',1) AS rightmost_char;
rightmost_char
--------------
d
Happy playing ...
Marco

You can use :
Select name from students where SUBSTR(name, -1, 1) = 'a' ;

Using SUBSTR().
SELECT name
FROM students
WHERE SUBSTR(name , -1) = 'a'

Related

List all the members whose name starts with R or G and contains letter I in it

List all the members whose name starts with R or G and contains letter I in it using REGEXP ?
Query
SELECT *
FROM member
WHERE REGEXP_LIKE (member_name, '^[R|G]', 'i')
and REGEXP_LIKE (member_name, 'i', 'i');
Can I combine both the REGEXP_LIKE expression?
Simplest way is to use LIKE to achieve the desired result as below:
SELECT
name
FROM myName
WHERE name LIKE '%l%' AND (name LIKE 'r%' OR name LIKE 'g%')
This works for me:
SELECT *
FROM member
WHERE REGEXP_LIKE (member_name, '^[R|G](.+)?i');
This will select all member where there name start with R or G followed by any or no
characters and the i.
Try this query in SQL:
SELECT *
FROM member
where member_name LIKE '[R-G]%'

Select statement with column contains '%'

I want to select names from a table where the 'name' column contains '%' anywhere in the value. For example, I want to retrieve the name 'Approval for 20 % discount for parts'.
SELECT NAME FROM TABLE WHERE NAME ... ?
You can use like with escape. The default is a backslash in some databases (but not in Oracle), so:
select name
from table
where name like '%\%%' ESCAPE '\'
This is standard, and works in most databases. The Oracle documentation is here.
Of course, you could also use instr():
where instr(name, '%') > 0
One way to do it is using replace with an empty string and checking to see if the difference in length of the original string and modified string is > 0.
select name
from table
where length(name) - length(replace(name,'%','')) > 0
Make life easy on yourselves and just use REGEXP_LIKE( )!
SQL> with tbl(name) as (
select 'ABC' from dual
union
select 'E%FS' from dual
)
select name
from tbl
where regexp_like(name, '%');
NAME
----
E%FS
SQL>
I read the documentation mentioned by Gordon. The relevent sentence is:
An underscore (_) in the pattern matches exactly one character (as opposed to one byte in a multibyte character set) in the value
Here was my test:
select c
from (
select 'a%be' c
from dual) d
where c like '_%'
The value a%be was returned.
While the suggestions of using instr() or length in the other two answers will lead to the correct answer, they will do so slowly. Filtering on function results simply take longer than filtering on fields.

Return values that have first three characters are letters

Is there a way to only return rows that have the first 3 values are alphabetic.
Something like
Select GUITAR_DESC
From Prod.INVENTOR
WHERE LEFT(GUITAR_DESC,3)LIKE('[A-Z]%'
Should return
FEN314
GIB452
This doesn't work.
Currently is there anyway?
Thanks!
Many SQL engines support range of letters in LIKE clause, so you could be using it like this:
Select GUITAR_DESC
From Prod.INVENTOR
WHERE GUITAR_DESC LIKE '[A-Z][A-Z][A-Z]%'
Unfortunately it seems that DB2 does not support this option. As a workaround you can use substr function and BETWEEN operator like this:
Select GUITAR_DESC
From Prod.INVENTOR
WHERE (substr(GUITAR_DESC,1,1) BETWEEN 'A' AND 'Z') AND
(substr(GUITAR_DESC,2,1) BETWEEN 'A' AND 'Z') AND
(substr(GUITAR_DESC,3,1) BETWEEN 'A' AND 'Z')
If you have a fairly new version of DB2 (tested on 10.1 for z/OS, and 9.7 Linux/Unix/Windows), you can use regex with Xquery:
SELECT *
FROM your_table A
WHERE
XMLCAST(
XMLQUERY('fn:matches($col, "^[A-Z]")' passing A.your_column AS "col")
AS INTEGER) = 1

like search on number column in SQL

How do I do a like search on a number column in SQL?
I want numbers which are like '0.0000%'.
I tried with
select * from emp where emp_id & '' like '123%'
select * from emp where CONVERT(varchar(20), emp_id) like '123%'
but in vain.
Please help me
Regardless of which DBMS you are using AND assuming you have a valid reason to do this, you have several ways to solve problems like these. I can think of three right now:
Convert the number to a string and use a LIKE operator on this:
select *
from emp
where to_char(emp_id) like '123%';
Use mathematical operators directly (like Andrey suggests), for example:
select *
from table
where num between 0 and 0.0001;
Construct a mathematical expression (actually, this is just another case of method 2), for example:
select *
from table
where abs(num - round(num, 5)) < 0.00001;
Use comparison operators (> and <):
select * from table where num > 0 and num < 0.00001
select 0.0001*1000 from dual if it is <1 means the 0.0001 has 3 or more zeros.
so i did like
select * from emp where emp_id*10000<1

Searching Technique in SQL (Like,Contain)

I want to compare and select a field from DB using Like keyword or any other technique.
My query is the following:
SELECT * FROM Test WHERE name LIKE '%xxxxxx_Ramakrishnan_zzzzz%';
but my fields only contain 'Ramakrishnan'
My Input string contain some extra character xxxxxx_Ramakrishnan_zzzzz
I want the SQL query for this. Can any one please help me?
You mean you want it the other way round? Like this?
Select * from Test where 'xxxxxx_Ramakrishnan_zzzzz' LIKE '%' + name + '%';
You can use the MySQL functions, LOCATE() precisely like,
SELECT * FROM WHERE LOCATE("Ramakrishnan",input) > 0
Are the xxxxxx and zzzzz bits always 6 and 5 characters? If so, then this is doable with a bit of string cutting.
with Test (id,name) as (
select 1, 'Ramakrishnan'
union
select 2, 'Coxy'
union
select 3, 'xxxxxx_Ramakrishnan_zzzzz'
)
Select * from Test where name like '%'+SUBSTRING('xxxxxx_Ramakrishnan_zzzzz', 8, CHARINDEX('_',SUBSTRING('xxxxxx_Ramakrishnan_zzzzz',8,100))-1)+'%'
Results in:
id name
1 Ramakrishnan
3 xxxxxx_Ramakrishnan_zzzzz
If they are variable lengths, then it will be a horrible construction of SUBSTRING,CHARINDEX, REVERSE and LEN functions.