Quotes in variable - sql

The variable #it_codigo is used to get a ntext value from other table in a procedure and is used for a select like:
SELECT * FROM anaProdutos WHERE cod_produto LIKE #it_codigo
But this doesn't work, missing quotes. I tried
''+#it_codigo+''
and
QUOTENAMES(#it_codigo, '')
But that didn't solve the problem.

If you're using LIKE you'd want something as per below;
LIKE '%' + #it_codigo + '%'
You need the percentage signs for the LIKE to work.

Try
WHERE cod_produto LIKE '%' + #it_codigo + '%'
Check this link for more information on LIKE keyword.

Related

SQL Server check if value is substring inside isnull

I have a field in UI interface that passes to a stored procedure a null value (when field is unfilled) or a contract number when it is filled. Substrings of the contract number are accepted as input.
Inside the procedure, I need to filter the results by this parameter.
I need something similar to this:
SELECT * FROM tableName tn
WHERE
tn.ContractNumber LIKE ISNULL('%' + #contractNumber + '%', tn.ContractNumber)
What do you think it is the best approach? Problem is that using a condition like this does not return values.
Simply:
SELECT *
FROM tableName tn
WHERE tn.ContractNumber LIKE '%' + #contractNumber + '%'
OR #contractNumber IS NULL
You are really checking multiple condition, so having them separated reads more intuitive (for most people, anyway).
I assume this is just a sample query, and you are not selecting * in reality...
Another one:
SELECT *
FROM tableName tn
WHERE tn.ContractNumber LIKE '%' + ISNULL(#contractNumber, '%') + '%'

SQL Using Like, One Variable, Multiple Columns

--where 'email#aol.com' in(o.SuOrderEmailAddress, o.SuBusEmailAddress, o.SuReturnEmail, O.SuPartsEmail) and es.ictid = 7
I want to search one variable (email address) in the multiple columns. This is what I have now, and it works.
I think I need to use Like, because I want to use wildcards. For instance, being able to use *#aol.com would be very helpful. Right now I have to enter an exact email address.
--Where s.suname like 'rizt%'and es.ictid = 7
It was simple enough in this above example, but once I rearrange the syntax to do multiple columns Im lost as to where I can use LIKE so that I can use Wildcards.
Thanks
You could just look at the end of the email address. Most databases support the right() function:
where '#aol.com' in (right(o.SuOrderEmailAddress, 8),
right(o.SuBusEmailAddress, 8),
right(o.SuReturnEmail, 8),
right(o.SuPartsEmail, 8)
) and
es.ictid = 7
(Even databases that don't have right() have an equivalent function, in general.)
Some databases support regular expressions, which is another option. Or, in your case, string concatenation would even work.
EDIT:
If you have a variable, you can do:
where #var in (right(o.SuOrderEmailAddress, len(#var)),
right(o.SuBusEmailAddress, len(#var)),
right(o.SuReturnEmail, len(#var)),
right(o.SuPartsEmail, len(#var))
) and
es.ictid = 7
This is the general idea. Usually, #search would be a parameter to a stored proc. Look at the WHERE clause first.
declare #search varchar(50) = '#aol.com'
set #search = '%' + #search + '%'
select case when o.SuOrderEmailAddress like #search then o.SuOrderEmailAddress
when o.SuBusEmailAddress like #search then o.SuBusEmailAddress
when o.SuReturnEmail like #search then o.SuReturnEmail
when o.SuPartsEmail like #search end as matchedEmail
from MyTable as o
where (o.SuOrderEmailAddress like #search
or o.SuBusEmailAddress like #search
or o.SuReturnEmail like #search
or o.SuPartsEmail like #search) and ...
You could control the adding of '%'s on the front end. This seems to me to be written clearly for the reader and gives you back the email address that was the match.
I think that the real answer is to use Full Text Indexing (if you are using MS SQL - otherwise seeing if there is an equivalent for whatever server you are using). However, here is an ugly hack that should work:
WHERE ISNULL(o.SuOrderEmailAddress, '') + '|'
+ ISNULL(o.SuBusEmailAddress, '') + '|'
+ ISNULL(o.SuReturnEmail, '') + '|'
+ ISNULL(O.SuPartsEmail, '') LIKE #pattern
This simply concatenates all of the relevant fields into a pipe-delimited list before checking that concatenated string for the #pattern.

performing sql select against a full name using wildcards

I have a stored procedure that I am passing in a general string variable called #SearchText as a varchar. This variable contains names, either part of a name, or a full name. I need to do a select on a table based on this variable, using wildcards. The inbound variable could be anything like (for the name 'john smith'):
'j', 'joh', 'john', 'sm', 'smith', 'john s', john smith'... you get the point.
So, the blunt approach I took is
select x from TableA
where FirstName like '%' + #SearchText + '%'
OR LastName like '%' + #SearchText + '%'
Obviously when a space is encountered it screws up the result set. Can someone please help me understand how to tweak this so it can match on any "amount" of the full name?
If this has already been answered, I couldn't find it... a hotlink to an existing solution would be just as appreciated here.
I might suggest something like this:
where FirstName + ' ' + Lastname like '%' + replace(#Searchtest, ' ', '%') + '%' or
LastName + ' ' + Firstname like '%' + replace(#Searchtest, ' ', '%') + '%'
However, if you are trying to do such full text searches, you might consider using a full text index. That generally provides the right level of functionality for these types of queries.
You can do this as follows:
select x from TableA
where FirstName+' '+LastName like '%' + #SearchText + '%'
Basically, you concatenate the first and last name first and apply the LIKE operator on the concatenation.

how to compare string in SQL without using LIKE

I am using SQL query shown below to compare AMCcode. But if I compare the AMCcode '1' using LIKE operator it will compare all the entries with AMCcode 1, 10,11,12,13. .. 19, 21,31.... etc. But I want to match the AMCcode only with 1. Please suggest how can I do it. The code is given below :
ISNULL(CONVERT(VARCHAR(10),PM.PA_AMCCode), '') like
(
CASE
WHEN #AMCCode IS NULL THEN '%'
ELSE '%'+#AMCCode+ '%'
END
)
This is part of the code where I need to replace the LIKE operator with any other operator which will give the AMCcode with 1 when I want to search AMCcode of 1, not all 10,11,12..... Please help
I think you are looking for something like this:
where ',' + cast(PM.PA_AMCCode as varchar(255)) + ',' like '%,' + #AMCCodes + ',%'
This includes the delimiters in the comparison.
Note that a better method is to split the string and use a join, something like this:
select t.*
from t cross apply
(select cast(code as int) as code
from dbo.split(#AMCCodes, ',') s(code)
) s
where t.AMCCode = s.code;
This is better because under some circumstances, this version can make use of an index on AMCCode.
If you want to exactly match the value, you don't need to use '%' in your query. You can just use like as below
ISNULL(CONVERT(VARCHAR(10),PM.PA_AMCCode), '') like
(
CASE
WHEN #AMCCode IS NULL THEN '%'
ELSE #AMCCode
END
)
Possibly you can remove case statement and query like
ISNULL(CONVERT(VARCHAR(10),PM.PA_AMCCode), '') like #AMCCode

How to look for a specific string using the LIKE command

I have the following WHERE statement in a SQL stored procedure that I am checking against a table which stores data as XML:
WHERE
(
CAST ([content_html] AS XML).value('(/root/Physicians/specialty/a/text())[1]', 'varchar(max)') LIKE '%' + #strService + '%'
OR
CAST ([content_html] AS XML).value('(/root/Physicians/specialty2/a/text())[1]', 'varchar(max)') LIKE '%' + #strService + '%'
OR
CAST ([content_html] AS XML).value('(/root/Physicians/specialty3/a/text())[1]', 'varchar(max)') LIKE '%' + #strService + '%'
OR
CAST ([content_html] AS XML).value('(/root/Physicians/specialty4/a/text())[1]', 'varchar(max)') LIKE '%' + #strService + '%'
OR
CAST ([content_html] AS XML).value('(/root/Physicians/specialty5/a/text())[1]', 'varchar(max)') LIKE '%' + #strService + '%'
OR
CAST ([content_html] AS XML).value('(/root/Physicians/specialty6/a/text())[1]', 'varchar(max)') LIKE '%' + #strService + '%'
)
strService is sent in from the front end to my code-behind which runs the stored procedure.
Each of the OR statement refers to each textbox in the XML document.
For example, If I send #strService = Urology the query searches for Urology, and Neurology, because Urology is also in Neurology
How can I check to see if content is equal to #strService OR if the word exists somewhere within content with non-letters around it OR if it's at the very beginning or very end of content with a non-letter either following or preceding respectively.
Here is an example:
I have the following entries:
Pediatric Urology
Urology
Neurology
I have the following LIKE statement:
LIKE 'Urology' = only Urology is displayed
LIKE '% Urology = nothing is displayed
LIKE '%[^a-zA-Z]Urology' = only Pediatric Urology is displayed
I would like to show Pediatric Urology and Urology in this case.
Pretty much as long as the word exist with a space in front of it, I would like to show.
Use the following expression:
CONTENT LIKE #strService
OR CONTENT LIKE '%[^a-zA-Z0-9]' + #strService + '[^a-zA-Z]%'
OR CONTENT LIKE #strService + '[^a-zA-Z]%'
OR CONTENT LIKE '%[^a-zA-Z]' + #strService
Heres an explanation:
CONTENT LIKE #strService
will check for equality
CONTENT LIKE '%[^a-zA-Z0-9]' + #strService + '[^a-zA-Z]%'
will check for the case where the word exists somewhere within content with non-letters around it
CONTENT LIKE #strService + '[^a-zA-Z]%'
will check for the case where the word exists at the very beginning of content with a non-letter following
CONTENT LIKE '%[^a-zA-Z]' + #strService
will check for the case where the word exists at the very end of content with a non-letter preceding.
If you'd like to exclude numbers from the set of possible adjacent letters, use [^a-zA-Z0-9] in place of [^a-zA-Z].
Hope this helps!
The concatenation of the string depends on the type of RDBMS you are using
For eg:
Oracle and Postgrasql uses "||"
While SQL-server uses "+"
And the use of correct wild character will help you to find what you are looking for.