I can't find the number data with SQL Server like - sql

The data is registered in the database with the number 5056381825. But when I like this number with 905056381825, a blank result is returned. What's the solution?

The LIKE seems to be in the wrong direction. Try:
where '905056381825' LIKE CONCAT('%', number, '%')
It is not clear from your question what the name of the column is in the database.

I assume that the correct query is:
SELECT *
FROM prCurrAccCommunication
WHERE CommAddress LIKE CONCAT('%', number, '%')

Related

Compare two columns with 'LIKE' Operator in SQL / Laravel

I'm trying to compare two columns from a table. In which i have to check the email is containing his mobile number or not.
TableName:- Table1
TableColumns:- id,email,MOB
Ex.
SQL:
'SELECT * FROM Tabel1 WHERE email LIKE %MOB%'
Laravel :
Tabel1::whereColumn('email', 'LIKE','%MOB%')->get();
I have Tried this above query but it is showing syntax error.
Lets start by answering you question SQL-wise, anything in quotes is a literal to SQL, so you need to use column reference and add the wildcard symbols. You can do that like this:
SELECT * FROM Table1 WHERE email LIKE CONCAT('%', MOB, '%');
Now lets look at Laravel now, the 3rd argument to where expects a literal value not another column. You can overcome this via either whereRaw or DB::raw:
Table1::whereRaw("email LIKE CONCAT('%', MOB, '%')");
or
Table1::where('email', 'LIKE', DB::raw("CONCAT('%', MOB, '%')"));
You should try this
SELECT * FROM Tabel1 WHERE email LIKE '%' +MOB+'%'
SELECT * FROM `table` WHERE `email` LIKE '%MOB%';

SQL with Optional Parameters. When parameters are populated we need to use LIKE operator, otherwise return all values

We have a query:
SELECT *
FROM wo_hdr
WHERE tm_no LIKE CONCAT('%', $P{tm_no}, '%')
However when the parameter $P{tm_no} is null we need to return all values.
I have seen others use
SELECT *
FROM wo_hdr
WHERE tm_no is null or tm_no LIKE CONCAT('%', $P{tm_no}, '%')
This does not work for us because then when we don't want to return null values when $P{tm_no} is not null.
Does anyone know a good solution for this? I thought possibly a CASE statement would work but it looked confusing to use one in the WHERE clause. It needs to work with both ORACLE and SQL Server.
Thank You.
Just modify your where clause with :
SELECT *
FROM wo_hdr
WHERE ($P{tm_no} IS NULL OR tm_no LIKE CONCAT('%', $P{tm_no}, '%'));

SQL Server query on nvarchar with asterisk in data and in where clause

I have a table Product which looks like this:
Name (nvarchar(100), not null)
It has data that looks like this:
4503-U**F19
When I query like this:
select * from Product where Name = '4503-U**F19'
I get nothing back... what gives?
Is there some special way to escape a where clause that contains an asterisk?
I'm probably staring at the answer and can't see it.
Have you tried the N in the prefix?
select * from Product where Name = N'4503-U**F19'

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 .

SQL Wildcards using %%%

I'm wondering what problems I may not foresee by using this query:
SELECT *
FROM tanswer
WHERE CourseID LIKE '%%%' AND Q39 LIKE 'p' AND Q42 LIKE 'a' AND Q43 LIKE 'a'
In particular using '%%%'.
The reason being, I have a dropdown that would send a '%' and in the sql I enclose it with the other two % to search through a set of courses, this query leaving the courseID searching through all courses.
The recordset returned seems to work fine, but I'm not sure if there's anything I may not be seeing by using three %%% together. Is this a special use of wildcards that I've stumbled across?
These are equivalent:
CourseID LIKE '%%%'
CourseID LIKE '%%'
CourseID LIKE '%'
Which is why your query behaves as you expected it to.
You need to use ! to escape the inner %: i.e. %!%%
see here