SELECT NAME FROM ABCD WHERE NAME LIKE "+aero+%"AND ROWNUM <= 10
what is the syntax error in this line......SELECT NAME FROM ABCD this is working
You need single quotes:
SELECT NAME FROM ABCD WHERE NAME LIKE '+aero+%' AND ROWNUM <= 10
And also a space before AND.
UPDATE
It's not clear in your question what exactly you're searching for. You may need one of the following instead:
SELECT NAME FROM ABCD WHERE NAME LIKE '"+aero+%"' AND ROWNUM <= 10
SELECT NAME FROM ABCD WHERE NAME LIKE '%"+aero+"%' AND ROWNUM <= 10
SELECT NAME FROM ABCD WHERE NAME LIKE '"%+aero+%"' AND ROWNUM <= 10
... or some other variation. But the important thing is that you should surrond literals with single quotes and not double quotes.
EDIT:
con.prepareStatement(
"SELECT name FROM abcd WHERE name LIKE '" + aero + "%' AND ROWNUM <= 10");
Will find all strings starting with your variable aero.
To get all strings containing the String of your variable aero use
con.prepareStatement(
"SELECT name FROM abcd WHERE name LIKE '%" + aero + "%' AND ROWNUM <= 10");
You need to use single-quotes instead of double-quotes:
SELECT name
FROM abcd
WHERE name LIKE '+aero+%'
AND ROWNUM <= 10;
If the double-quotes are in the string you search use '"+aero+%"'.
If you only want to search for strings containing aero, use '%aero%'.
Double quotes around the literal?
No space between the literal and AND?
You need to give the character between the expression and AND operator . And give single quotes for
your regular expression
If you called that from some program codes, following is more likely, where aero is the variable, and % would be acts as wildcard. When aero is ab, it will search for 'ab','abc', 'abcd', .......
"SELECT NAME FROM ABCD WHERE NAME LIKE '"+aero+"%' AND ROWNUM <= 10;"
It isn't clear to me whether your aero is a literal string 'aero' or the name of a variable in your program. If it is a literal then do this:
pr = con.prepareStatement
("SELECT NAME FROM ABCD WHERE NAME LIKE 'aero%' AND ROWNUM <= 10");
If it is a variable then you should use a bind variable. Not only is the syntax much less confusing, it also prevents SQL injection attacks. I'm no ASP expert, but the syntax is something like:
pr = con.prepareStatement
("SELECT NAME FROM ABCD WHERE NAME LIKE ?||'%' AND ROWNUM <= 10");
con.setString(1,aero);
Related
I have a table(say users) in which there is a column say name.
you may think table structure a shown below:
-------------
name
--------------
Abdul Khalid
--------------
Abdul, Khalid
--------------
Abdul - Khalid
--------------
other names
My question is can I do some query to find all the 3 rows in which the name column value is "Abdul Khalid"(basically "Abdul Khalid" or "Abdul, Khalid" or "Abdul - Khalid" if I skip the "," and "-" character).
You can use like:
select t.*
from t
where name like 'Abdul%Khalid';
If you want the names anywhere in the string (but in that order), then put wildcards at the beginning:
select t.*
from t
where name like '%Abdul%Khalid%';
If you are passing in the value as a variable:
select t.*
from t
where name like replace('Abdul Khalid', ' ', '%');
For PostgreSQL is better to use '~'
name ~ '^Abdul[ ,-]Khalid$'
OR if you want also in middle of string:
name ~ 'Abdul[ ,-]Khalid'
Or you can use translate (with index on it) for any SQL:
translate(name, ' ,-') = 'AbdulKhalid'
you also can use REGEXP like this:
SELECT * from yourTable where name REGEXP 'Abdul( |, | - )Khalid';
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.
When i use LIKE statement in my SQL,
for example,
SELECT * FROM table WHERE name = "%k"
It will return all rows, where name ends on k.
It can return : Ok, OOk, OOOk,
How i can do same statement but with one letter, so it returns only Ok.
Or 2 letters, so it returns only OOk?
_ is a single character wildcard.
SELECT * FROM table WHERE name = `_k`
See this IBM Reference
Use the equality operator (=) or the IN operator instead of the LIKE operator:
SELECT * FROM table WHERE name IN ('Ok', 'OOk')
Use the _ wildcard. It matches only a single character.
_k for Ok.
__k for Ook
depending on the length you provide
For 1 character 'X' followed by k
Select *
from table
where length(name) = 2
AND name = "%k"
For 2 character 'X' followed by k
Select *
from table
where length(name) = 3
AND name = "%k"
You should use an underscore (_) character. See the documentation about operator LIKE.
So, the query you need (1 or 2 chars before k) is:
SELECT * FROM table WHERE name LIKE '_k' OR name LIKE '__k'
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.
How can I write a select statement to select only integers (and nothing more) from a char column in SQL Server. For example, my table name is POWDER with 2 columns, ID (int) and Name(char (5))
ID Name
-- ----------
1 AXF22
2 HYWWW
3 24680
4 8YUH8
5 96635
I want to be able to select only those rows that contain an integer and nothing more (ID 3 and ID 5 in this example)
If I try:
SELECT *
FROM POWDER
WHERE Name LIKE '[0-9]%'
...it will return:
ID Name
-- ----------
3 24680
4 8YUH8
5 96635
Any ideas how to get the rows containing just integers?
SELECT * FROM POWDER WHERE IsNumeric(Name) = 1
IsNumeric returns 1 for some other characters that are valid in numbers, such as + and - and $ but for your input you should be fine.
Try this:
SELECT * FROM Table WHERE Name LIKE '[0-9]%%'
To avoid issues with ISNUMERIC and all spaces, -, +, . etc, use the fact that the column is char(5)
SELECT *
FROM POWDER
WHERE Name LIKE '[0-9][0-9][0-9][0-9][0-9]'
Edit: for any number of characters. Double negative...
SELECT *
FROM POWDER
WHERE Name NOT LIKE '%[^0-9]%'
Use positive and negative checks to make sure we have an integer: It must contain a digit. Only digits and spaces are allowed. No spaces are allowed between digits.
SELECT *
FROM POWDER
WHERE Name LIKE '%[0-9]%'
AND Name NOT LIKE '%[^0-9 ]%'
AND Name NOT LIKE '%[0-9]% %[0-9]%'
Try:
SELECT *
FROM POWDER
WHERE Name patindex ('%[a-z]%',name) != 0
The last one is the best,kind of works really really well.
SELECT * FROM POWDER
WHERE Name LIKE '%[0-9]%'
AND Name NOT LIKE '%[^0-9 ]%'
AND Name NOT LIKE '%[0-9]% %[0-9]%'