Convert search from SQL Server to MySQL - sql

i need to convert this one from SQL Server into MySQL
IF IsNull(#SearchText, '') <> '' BEGIN
SET #SearchText = '%' + #SearchText + '%'
SELECT NewsID,DeptID,DeptName,Title,Details ,NewsDate,img
FROM #tbSearchtextTb
WHERE IsNull(Title,'')+IsNull(Details,'') LIKE #SearchText END
this code will search fro my search word in this columns: Title, Details.
i tried to convert this line but i had lots of errors:
these are my unsuccessful attempts
IF ISNULL(SearchText,'') <> '' THEN
SELECT CatID,CatTitle,CatDescription,CatTitleAr,CatDescriptionAr,PictureID,Published,DisplayOrder,CreatedOn
FROM tmp
WHERE CatTitle + CatDescription + CatTitleAr + CatDescriptionAr
LIKE $SearchText;
and this one
IF $SearchText IS NOT NULL THEN
SELECT CatID,CatTitle,CatDescription,CatTitleAr,CatDescriptionAr,PictureID,Published,DisplayOrder,CreatedOn
FROM tmp
WHERE ISNULL(CatTitle,'') +ISNULL(CatDescription ,'') +ISNULL(CatTitleAr ,'') +ISNULL(CatDescriptionAr,'') LIKE $SearchText;
and many many other ways but i could not find any.
so if you know please let me know, thanks and best regards.

you can use fulltext search in mysql
http://dev.mysql.com/doc/refman/5.5/en/fulltext-search.html

The SQL Server version can also be written:
IF #SearchText <> '' -- null is implicitly <> ''
BEGIN
SELECT NewsID,DeptID,DeptName,Title,Details ,NewsDate,img
FROM #tbSearchtextTb
WHERE IsNull(Title,'')+IsNull(Details,'') LIKE '%' + #SearchText + '%'
END
IsNull is IFNull in MySQL.
Strings are joined using Concat in MySQL
Nulls are implicitly '' in concat
IF SearchText <> '' THEN # << again, this test is sufficient
SELECT
CatID,CatTitle,CatDescription,CatTitleAr,CatDescriptionAr,
PictureID,Published,DisplayOrder,CreatedOn
FROM tmp
WHERE Concat(CatTitle, CatDescription, CatTitleAr, CatDescriptionAr)
LIKE Concat('%',$SearchText,'%');

Related

Stored procedure with 2 Like parameters

I am looking to use a stored procedure to filter my datagridview by either the user entering the part name into txtPartName or MRPID into txtMRPID. However, this is not filtering as expected and just shows all results from the parts table regardless.
I have a feeling this is due to the way I have implemented the AND or perhaps that I can't perform 2 LIKE statements in one procedure perhaps? Anyway can someone point me in the right direction as how to properly perform this procedure.
CREATE PROCEDURE Parts_ViewAllOrSearch
#PartNameSearch nvarchar(255),
#MRPIDSearch nvarchar(255)
AS
BEGIN
SELECT *
FROM Parts
WHERE #PartNameSearch = ''
OR PartName LIKE '%' + #PartNameSearch + '%'
AND #MRPIDSearch = ''
OR MRP_ID LIKE '%' + #MRPIDSearch + '%'
END
Basically you need parentheses around the OR condition:
SELECT *
FROM Parts
WHERE
(#PartNameSearch = '' OR PartName LIKE '%' + #PartNameSearch + '%')
AND (#MRPIDSearch = '' OR MRP_ID LIKE '%' + #MRPIDSearch + '%')
Why you need that is because AND has higher logical prescedence than OR. So without the parentheses, the WHERE clause is equivalent to:
WHERE
#PartNameSearch = ''
OR (PartName LIKE '%' + #PartNameSearch + '%' AND #MRPIDSearch = '')
OR MRP_ID LIKE '%' + #MRPIDSearch + '%'
... which obviously is not what you want.
Finally, please note that, as it stands, your code just does not seem to really these OR expressions. If a parameter is the empty string, then, once surrounded with wildcards it will match on all possible values (except null values). You would just write this as:
WHERE
PartName LIKE '%' + #PartNameSearch + '%'
AND MRP_ID LIKE '%' + #MRPIDSearch + '%'

SQL Sum of numbers from a string

I have a string like this:)
"<"FOSTIMON 75 M.J.>|4|4|4|3|3|3|3|3|3|3|3|3||||||||||||||||||||||||||||"
I need the sum of the numbers (4+4+4+3+...+3), expected result = 39
Thank you for the help!
THIS ANSWERS THE ORIGINAL QUESTION BEFORE EDITING.
Most databases support executing some form of prepared statement. In your case, probably the simplest method is to construct a SQL statement and then execute it dynamically.
The syntax for this varies dramatically from database to database. Out of randomness, I'm choosing SQL Server, but the functionality (although not the syntax) is available in almost any database:
declare #str nvarchar(max) = '|4|4|4|3|3|3|3|3|3|3|3|3||||||||||||||||||||||||||||';
set #str = 'select ' + replace(#str, '|', '+') + ' + 0';
exec sp_executesql #str;
Here is a rextester for this particular version.
Note that this works because + is a unary (numeric) operator, analogous to - but it does not change the sign.
Try this
Assuming the first part of the string upto the first "|" can be ignored
Not sure what version of SQL you're using but if its less than SQL2016 then use this string splitter
DECLARE #s NVARCHAR(50) = '<"FOSTIMON 75 M.J.>|4|4|4|3|3|3|3|3|3|3|3|3||||||||||||||||||||||||||||'
SELECT
SUM(CONVERT(INT, S.[Value]))
FROM dbo.DelimitedSplit8K(#s, '|') S
WHERE S.RN >= 2 AND S.RN <= 13
If you're on SQL2012 + then you can utilise TRY_PARSE and get rid of the WHERE clause altogether
SELECT
SUM(
CASE
WHEN TRY_PARSE(S.[Value] AS INT) IS NOT NULL
THEN CONVERT(INT, S.[Value])
ELSE 0
END
--CONVERT(INT, S.[Value])
)
FROM dbo.DelimitedSplit8K(#s, '|') S
If you're using SQL2016 + then use the inbuilt function, String_Split
SELECT
SUM(
CASE
WHEN TRY_PARSE(S.[Value] AS INT) IS NOT NULL
THEN CONVERT(INT, S.[Value])
ELSE 0
END
)
FROM STRING_SPLIT(#s, '|') S

Trying to use SQL Replace in a non-dynamic way

I have the following statement, where I'm passing in a parameter like this:
'0001,0003'
I was following the REPLACE answer from this question:
SQL IN Statement splitting parameter
But I'm trying to take it out of dynamic sql. My returned result is NULL. Is there anyway to get this to work?
DECLARE #partialLNum varchar(MAX)
DECLARE #lNumConCat varchar(500)
DECLARE #tTemp table(lNum varchar(15))
DECLARE #formatIN varchar(MAX)
set #partialLNum = '0001,0003'
set #formatIN = ''''+ REPLACE(#partialLNum,',',''',''')+''''
insert into #tTemp
select substring(lNum,1,2) + '-' + substring(lNum,3,3) + '-' + substring(lNum,6,2) + '-' + substring(lNum,8,3)
from [rpt].[myView]
where LNum IN (#formatIN)
select #lNumConCat = COALESCE(#lNumConCat +'' , '', '''') + LNum from #tTemp
select #lNumConCat
in takes a list of values. So:
where LNum IN ('0001,0003')
has a list with one element, that happens to have a comment in it.
One way to do what you want is using like:
where ',' + partialLNum + ',' like '%,' + LNum + ',%'
There should suffice, but there are other ways using a split() function as well.

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

Issue With Apostrophe

I have a Proc that was coded in Dynamic SQl for one of my Application. It is using to search the Applicants with their last name. Right now it is searching applicants with either their first 2 digits of their last name or full last name. But i have a problem searching Applicants that have Apostrophe in their last name(Example O'Connor). If the client try to search applicant with O' or O'Connor it is throwing an error. They want to search every Applicant with or without Apostrophe in their last name. Please Help I tried everything, but its not working. Below is my search code that using in the Proc to pull applicants:
Add wildcards if necessary
if Rtrim(#FirstName) <> ''
begin
If(Len(#FirstName) < 30) and (CharIndex('%', #FirstName) = 0) and #FirstName != ''
Set #FirstName = char(39) + #FirstName + '%' + char(39)
end
if Rtrim(#LastName) <> ''
begin
If(Len(#LastName) < 60) and (CharIndex('%', #LastName) = 0) and #LastName != ''
Set #LastName = Char(39) + #LastName + '%' + char(39)
end
Now build dinamically the filter base on input parameters
if Rtrim(#LastName) <> ''
select #Where = #Where + ' and a.LastName like '+ Rtrim(#LastName)
You need to escape the apostrophe in the input string (basically replace a single ' with two '') as you build your SQL string
You need to pay attention to this anywhere you choose to pass user input to a SQL server database as its a security issue (SQL Injection Attacks) c.f. Bobby Tables
if Rtrim(#LastName) <> ''
select #Where = #Where + ' and a.LastName like '+ Replace(Rtrim(#LastName),'''','''''') + ''
My suggestion is to write the query to contain an alternate column to be used for filtering with the apostrophe/single quote replaced by any special character such as a #. This allows you to leave the original column intact in case you want to display it.
To do that in SQL Server, you could do something like this:
Select
tbl.strName_Last,
REPLACE(tblOrder.strName_Last, '''','#')) as StrLastNameForFilter
....
Then change your code to filter based on this alternate column, and in the user-provided filter string, replace the apostrophe/single quote with the special character.