How can I use LIKE with #value in SQL Server? - sql

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

Related

How to get value inside quotes in SQL

I want to separate a fixed expression in SQL in order to get the string value inside ' '.
For example in the following SQL:
declare #value varchar(60)
set #value = 'a.[country] LIKE ''US'''
I would like to store separately the information US, is there a way to do it?
Thank you in advance.
You can try this
declare #value varchar(60)
set #value = 'a.[country] LIKE ''US'''
select left(Right(#value,3),2)
--OR this
select substring(Right(#value,3), 1, 2)
The question seems incomplete, but based on the question I suggest to use some Delimiter with the dynamic string and use combination of LEFT, CHARINDEX and SUBSTRING in SQL to fetch values out of SQL.
Example
Following is not the best example, but just explaining how to use it:
e.g
declare #value varchar(60)
set #value = 'a.[country] LIKE §''US'''
SELECT LEFT(SUBSTRING(#value,
CHARINDEX('§', #value) + 1, 100),
CHARINDEX('§', #value) - 1)
Output
'US'
There are already some example about this on StackOverflow.
If you haven't checked out yet other StackOverflow posts, you can refer following:
How to extract this specific substring in SQL Server?

Search criteria using Sql server

I need to create a procedure to return the matching criteria for the passed parameter from the table
example :-
parameter - The big
related match from the table - The Big Bang
The Big
The big
The big bang
I can do the case sensitive search using collation
The output should be "The big". It has to get the closest match possible
Could anyone help me to get that?
Something like this:
create procedure usp_search
(
#SearchCriteria nvarchar(500)
)
as
begin
select Title
from movies
where #SearchCriteria = Title COLLATE Latin1_General_CS
end

How do you pass values for a parameter by position when you need to check multiple values?

I created a stored procedure (spBalanceRange) with 2 optional parameters. They've been set to a default value and the sp works fine when I pass only 1 value per parameter by position. However, I have a situation where I'm trying to pass, by position, two strings immediately followed by a wildcard. I want the user to be able to search for Vendor names that start with either 'C%' or 'F%'. Here's the gist of the CREATE PROC statement:
CREATE PROC spBalanceRange
#VendorVar varchar(40) = '%',
#BalanceMin money = 1.0
...
Here's what I've tried so far, but doesn't work:
EXEC spBalanceRange '(C%|F%)', 200.00;
EXEC spBalanceRange 'C%|F%', 200.00;
Is there a way to check for 2 or more string values with a wildcard when passed by position? Thanks.
EDIT: According to your comments you are looking for the first letter of a vendor's name only.
In this special case I could suggest an easy, not well performing but really simple approach. CHARINDEX returns a number greater than zero, if a character appears within a string. So you just have to pass in all your lookup-first-characters as a simple "chain":
DECLARE #DummyVendors TABLE(VendorName VARCHAR(100));
INSERT INTO #DummyVendors VALUES
('Camel Industries')
,('Fritz and Fox')
,('some other');
DECLARE #ListOfFirstLetters VARCHAR(100)='CF';
SELECT VendorName
FROM #DummyVendors AS dv
WHERE CHARINDEX(LEFT(dv.VendorName,1),#ListOfFirstLetters)>0
This was the former answer
Checking against more than one value needs either a dedicated list of compares
WHERE val=#prm1 OR val=#prm2 OR ... (you know the count before)
...or you use the IN-clause
WHERE LEFT(VenoderName,1) IN ('C','F', ...)
...but you cannot pass the IN-list with a parameter like ... IN(#allValues)
You might think about a created TYPE to pass in all your values like a table and use an INNER JOIN as filter: https://stackoverflow.com/a/337864/5089204 (and a lot of other examples there...)
Or you might think of dynamic SQL: https://stackoverflow.com/a/5192765/5089204
And last but not least you might think of one of the many split string approaches. This is one of my own answers, section "dynamic IN-statement": https://stackoverflow.com/a/33658220/5089204
I'm answering my own question, and maybe other solutions exist but here is what had to happen with my stored procedure in order to pass variables by position:
CREATE PROC spBalanceRange
#VendorVar varchar(40) = '%',
#BalanceMin money = 1.0
AS
IF (#VendorVar = '%' AND #BalanceMin IS NULL OR #BalanceMin = '')
BEGIN
PRINT 'BalanceMin cannot be null.';
END
IF (#VendorVar = % AND #BalanceMin IS NOT NULL)
BEGIN
(sql statement using parameters)
END
EXEC spBalanceRange '[C,F]%', 200.00;
That's what I know.

Changing Case of Strings (Lower to Upper) in SQL Server

I have 2 databases, one with lowercase data and another with uppercase data.
DECLARE #NAME VARCHAR(40)
SELECT #NAME = UPPER(SELECT NAME FROM DELETED)
By executing SELECT NAME FROM DELETED, I am selecting data that is lower case.
By executing SELECT #NAME = UPPER(SELECT NAME FROM DELETED), I would want to select uppercase data on the query inside the UPPER().
Question is can I use the UPPER() with the SELECT, like the query above?
How about
SELECT UPPER(NAME) FROM DELETED
instead of
UPPER(SELECT NAME FROM DELETED)
You'll need an extra pair of brackets
Select #NAME = UPPER((SELECT NAME FROM DELETED));
(Not that I'd do it that way, see Lukas's answer for a better approach).
Use
SELECT #pNAME = UPPER([NAME]) FROM DELETED

Accessing text fields in a stored procedure and insert to another table

I am trying to access a “text” type and inserting that value into another table viw a stored procedure. I’ve tried to cast it, convert it, but nothing works.
My code looks somethings like this:
Declare #Critique varchar(max), #Feedback varchar(max)
…
…
…
SELECT #Critique = CAST(comments as varchar(max)), #Feedback = CAST(public_critique as varchar(max)) FROM ASCO_vEXTERNAL_REVIEW_APPLICATIONS_LIST WHERE wf_task_assignment_id = #WfTaskAssignmentIDP1
– comments and public_critique are defined as text in view (also tried with table) ASCO_vEXTERNAL_REVIEW_APPLICATIONS_LIST
…
…
…
insert into WF_TASK_ASSIGNMENT_REVIEW (wf_task_assignment_review_id, wf_task_assignment_id, grantee_project_id, comments, public_critique) values (#NewID1, #WfTaskAssignmentIDP2, #GranteeProjectID, #Critique, #Feedback)
Can you please help me with this as soon as possible. I would really appreciate this.
Thanks,
Harish
I'm assuming that the WF_TASK_ASSIGNMENT_REVIEW is the one containing the text column you're trying to write into.
The text type is now deprecated in SQL 2005 and 2008. If at all possible try and upgrade the WF_TASK_ASSIGNMENT_REVIEW table to use the nvarchar(max) type instead.
If not, the only way is to use the WRITETEXT statement to write into the target column, in a loop (since WRITETEXT has an upper limit). See the WRITETEXT statement example in the SQL Server docs.
Your question is not sound good to understand .
Dont use text ,it wont support in many cases like where ,group by etc , so try use varchar
This is just an example
Declare #Critique varchar(max)
set #Critique = (select public_critique from ASCO_vEXTERNAL_REVIEW_APPLICATIONS_LIST
where convert(varchar(50), wf_task_assignment_id ) =#WfTaskAssignmentIDP1)