I have procedure it is working but i want to improve it, by giving ability to search by couple letters in word. Procedure:
alter PROCEDURE SearchByIncIDroNameOfClientoStreetorEmpID
#IncID int,
#NameOfClient nvarchar (60),
#StrID int,
#EmpID int,
#date date
as
select IncID,NameOfClient,EnterDate,StrName ,AppartmentNumber as 'appartment',Summary,IncStatus,e.LastName,EndDate
from Incident i
JOIN Streets s on i.StrID=s.StrID
join Employee e on i.EmpID=e.EmpID
where IncID =#IncID or NameOfClient = #NameOfClient or s.StrID = #StrID or i.EmpID=#EmpID or EnterDate =#date
go
I need modify this part NameOfClient = #NameOfClient so it will show records which begins with provided letter. so right now if i want to searh for John i have to wright John but I want for example if i will give just Jo it should return me all of the records which starts from Jo
I was trying to add % sign before #NameOfClient but it doesn't work. Any Ideas how I can do it?
You need to use like operator:
NameOfClient like #NameOfClient+'%'
NameOfClient like #NameOfClient + '%'
Doesn't this works?
You must use the LIKE operator:
WHERE NameOfClient LIKE #NameOfClient
Related
I don't know if this question has been asked before. I searched but I couldn't find the answer.
In short, I want to do something in MsSQL like:
DECLARE #VALUE VARCHAR(2000) = 'Joe and Bill and Michael are my friends'
SELECT * FROM my_friends WHERE my_friends.name LIKE %#VALUE%
I want to get records from my_friends table which "name" column is in #VALUE string. For this case, Joe, Bill and Michael.
Since your name is meant to be a substring of your variable, you want
WHERE #VALUE like '%'+name+'%'
You could also use charindex if you want case sensitive matches
WHERE charindex(names,#VALUE)>0
Good day
I have problem with my store procedure as follow:
I need to know wchich manager have which employee
My statement with function WITH as SELECT working fine ,but
my problem is with procedure when iam caling back Iam geting 0 rows back.
I think should by problem with Function LIKE when Iam creating procerure.
Thank you for opinions
create procedure test #Name varchar(200) as
WITH n(Surname,Name) AS
(SELECT Surname,Name
FROM [dbo].[phonebook]
WHERE Name like '%#Name' --I need to be like here beause before name is XT245*/ SUBCODE
UNION ALL
SELECT subs.Surname, subs.Name
FROM [dbo].[phonebook] as subs, n
WHERE n.Surname = subs.Boss)
SELECT Name FROM n
exec test 'XX37485*/John'
This expression:
WHERE Name like '%#Name' --I need to be like here because before name is XT245*/ SUBCODE
is looking for names that have exactly that pattern of characters . . . # N a m e. There is no variable substitution.
If you want to use like, then:
WHERE Name like '%' + #Name
Note that this requires that the pattern end with #Name, so it will not match #Name anywhere in the string.
I am very new to SQL query so, sorry If I miss basic details.
Here is the scenario:
I have to search the user email Id inside the column.
the column contains values like
abc#company.com;xyz#company.com;test#company.com;
Now I got the user email id from query:
DECLARE #emailid AS VARCHAR(50)
SELECT #emailid = emailId FROM dbo.tblUser WHERE userName = 'abc' AND projectId ='P1456'
I also replaced the semicolon with a comma like:
COLUMNNAME IN (REPLACE(#emailid,';' ,','))
Now I got the string like abc#company.com,xyz#company.com,test#company.com,
But IN clause treat it as a single string, So how to append inverted commas in each email ids like below to help IN clause to search?
'abc#company.com','xyz#company.com','test#company.com',
Try this:
DECLARE #emailid AS VARCHAR(50)
SELECT #emailid = 'abc#company.com;xyz#company.com;test#company.com;'
SELECT #emailid AS emailid
,REPLACE(SUBSTRING( ''''+#emailid+'''',
PATINDEX(';',#emailid), 51),';',''+ ''''+','+''''+'') AS [replaced]
I hope you are using MSSQL
I want to make a function that search in a table and returns rows that contain a certain word that I Insert like below. But when I use LIKE it give me an error: Incorrect syntax near '#perberesi'
CREATE FUNCTION perberesit7
(#perberesi varchar(100))
RETURNS #menu_rest TABLE
(emri_hotelit varchar(50),
emri_menuse varchar(50),
perberesit varchar(255))
AS
Begin
insert into #menu_rest
Select dbo.RESTORANTET.Emri_Rest, dbo.MENU.Emri_Pjatës, dbo.MENU.Pershkrimi
From RESTORANTET, MENU
Where dbo.MENU.Rest_ID=dbo.RESTORANTET.ID_Rest and
dbo.MENU.Pershkrimi LIKE %#perberesi%
return
End
Pleae help me...How can I use LIKE in this case
try using:
'%' + #perberesi + '%'
instead of:
%#perberesi%
Some Examples
Ok, I just realized that you are creating a function, which means that you can't use INSERT. You should also really take Gordon's advice and use explicit joins and table aliases.
CREATE FUNCTION perberesit7(#perberesi varchar(100))
RETURNS #menu_rest TABLE ( emri_hotelit varchar(50),
emri_menuse varchar(50),
perberesit varchar(255))
AS
Begin
return(
Select R.Emri_Rest, M.Emri_Pjatës, M.Pershkrimi
From RESTORANTET R
INNER JOIN MENU M
ON M.Rest_ID = R.ID_Rest
Where M.Pershkrimi LIKE '%' + #perberesi + '%')
End
Why do you have to define the return table?
The following is a inline table variable function that performs better than a multi-line table. I wrote one to return columns that have the two letters 'id'. Just modify for your own case.
See article from Wayne Sheffield.
http://blog.waynesheffield.com/wayne/archive/2012/02/comparing-inline-and-multistatement-table-valued-functions/
-- Use tempdb
use tempdb;
go
-- Simple ITVF
create function search_columns (#name varchar(128))
returns TABLE
return
(
select * from sys.columns where name like '%' + #name + '%'
)
go
-- Call the function
select * from search_columns('id');
go
However, since you have a '%' in the like clause at the front of the expression, a full table or index scan is likely. You might want to look at full text indexing if you data is large.
http://craftydba.com/?p=1629
I have two records in my person table in my database, their last names are "تحصیلداری" and "موقر".
when I use
select * from Person where Lastname like N'%ی%'
the record containing "ی" character in last name returns, which is exactly what I need, but I need a little change. I need to use the word between % and % as a parameter.
Something like :
create procedure usp_GetPersonsWhoseNameContains(#LN nvarchar(50))
as
begin
select * from Person where Lastname like N'%'+#LN+'%'
end
declare #LastName nvarchar(50) = 'ی'
exec usp_GetPersonsWhoseNameContains(#LastName)
but it does not work this way, and in my searches I couldn't find the appropriate solution. Does anyone know how to
Try
create procedure usp_GetPersonsWhoseNameContains(#LN nvarchar(50))
as
begin
select * from Person where Lastname like N'%'+#LN+'%'
end
declare #LastName nvarchar(50) = N'ی'
exec usp_GetPersonsWhoseNameContains(#LastName)
Maybe you noticed that SQL Server uses N to any quoted string input when generating DDL scripts for you.