How to cast column within select query - sql

I have this query:
SELECT * FROM students
WHERE name LIKE '%ka%' OR
tel LIKE '%12%' OR
address LIKE '%ka%';
but tel is an integer-type column and therefore I can't use the %abc% operator there (although I need to do it like that)
How can I cast the tel column within this query?

Try this
SELECT * FROM students
WHERE name LIKE '%ka%' OR
CAST(tel as text) LIKE '%12%' OR
address LIKE '%ka%';

Try this:
SELECT * FROM students
WHERE name LIKE '%ka%' OR
CONVERT(VARCHAR, tel) LIKE '%12%' OR
address LIKE '%ka%';

Convert the tel to a text type first
cast(tel as text) LIKE '%12%' OR
You should consider changing your column type, if you are not using an int type like an int
Indexes and other db optimizations are ineffective with a query like this.

Related

Search with multiple criteria in LIKE query

I have a problem when I work with SQL query. I have 2 table Student and Class, and I want to search by both student name, student id, class name, ...
If I write SQL Query like:
SELECT * FROM Student,Class
WHERE Student.classname = Class.classname
AND student_name LIKE '%ab%' OR class_name LIKE '%ab%'
It works well! But when I change %ab% part to ? to store value of textbox like this:
SELECT * FROM Student,Class
WHERE Student.classname = Class.classname
AND student_name LIKE ? OR class_name LIKE ?
It doesn't work and can not do anything. So, what can I do to compare multiple criteria with one input text in SQL?
I don't know what framework or tool you are using with your SQL, but given the following LIKE expression:
student_name LIKE ? OR class_name LIKE ?
if you wanted to find students or classes containing ab, then you would bind %ab% to both ? placeholders. You would do this from the tool.
I don't know what you want to have, but I guess you would like to declare a variable as one input?
Declare #input varchar(20)
Set #input = '%ab%'
SELECT * FROM Student,Class
WHERE Student.classname = Class.classname
AND student_name LIKE #input OR class_name LIKE #input
Do not use LIKE. Instead, use a regular expression.
There are only two benefits of LIKE:
under some circumstances, it is indexable, (your query is not)
and
LIKE is part of the standard, while regexps are DBMS dependent

using % in IN operator of sql

I have a query like this
select * from table_employee where name in ('Jack','Jon','Jade');
and it gives me three results.
but I want to use % with the names I mentioned in query like:
select * from table_employee where name in ('%Jack%','%Jon%','%Jade%');
The query execution is completed but now I get no results. Whats wrong here and how can i use % in IN operator?
You should use LIKE combined with OR, IN does not support wildcards
select * from table_employee where name LIKE '%Jack%'
OR name LIKE '%Jon%';
The in keyword does not work like that. You can use a like operand and bind the three values with or
example
SELECT *
FROM table_employee
WHERE NAME LIKE '%Jack%'
OR NAME LIKE '%Jon%'
OR NAME LIKE '%Jade%';
Convert it to an OR:
SELECT *
FROM table_employee
WHERE NAME LIKE '%Jack%'
OR NAME LIKE '%Jon%'
OR NAME LIKE '%Jade%';
If your fields data are exact what you want get in the result is no needed to use like . You should use like only when you don't know the data in a field but you know that it could contains a string Jack for example .
I'm sayng this because if you have an index on the field where you use LIKE it will be not used , so you will get performance problem .

Whats the right syntax for the string comparison query involving variable name in PostgreSQL?

Suppose I have a Table Name
First Name Last Name
--------------------------
Kris Kristos
I want a right syntax for the query which appears like
Select *
from Name
where First_name like '%'Last_Name'%'
You could use strpos:
SELECT *
FROM name
WHERE strpos(LastName, FirstName) > 0;
SqlFiddleDemo
I think I got the answer. 'concat' can be used for this purpose.
Select *
from name
where firstname like concat('%',lastname)

Selecting only integer data within a column with characters

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'

sql LIKE statement combined from three "like"`s

how to combine sql query from three like statements? I`m trying like this:
SELECT * FROM myTable
WHERE
(NAME LIKE '%someChars%' AND
(CITY LIKE '%someChars%' AND
TYPE LIKE'% someChars%')
);
It doesn`t work, can you help me with that please ?
The query is correct except the fact that Type is a sql keyword, so try putting it between bracket like that:
SELECT * FROM [myTable]
WHERE ([NAME] LIKE '%someChars%' AND
([CITY] LIKE '%someChars%' AND
[TYPE] LIKE '%someChars%'));
PS: The parenthesis are not needed
SELECT *
FROM myTable
WHERE
NAME LIKE '%someChars%' AND
CITY LIKE '%someChars%' AND
TYPE LIKE '%someChars%';