Finding a line break in a text string in a SQL table? - sql

I was trying to find line breaks and carriage returns in a column in a SQL table and I am not sure about the syntax.
I tried:
SELECT foo FROM test
WHERE foo LIKE CHAR(10)
I didn't get any results even though I know that the table should return results. What am I doing wrong?

SELECT foo FROM test WHERE foo LIKE '%' + CHAR(10) + '%'
Edit: to find all various types of line endings you should probably just check both:
SELECT foo FROM test WHERE foo LIKE '%' + CHAR(10) + '%'
OR foo LIKE '%' + CHAR(13) + '%'

SELECT foo FROM Table WHERE foo LIKE '%' + CHAR(13)+CHAR(10) + '%'

Try
SELECT foo FROM test WHERE foo LIKE '%'+ CHAR(10)+'%'

Try
SELECT foo FROM test WHERE foo LIKE CONCAT('%', CHAR(10), '%')

Related

Fuzzy (Like) Join not working

I have the below code to match on similar characters where possible and it only brings through results from subquery A. Please can someone assist? Thanks
select
*
from
(
Select 'Test' T
)a
left join
(
Select 'Test1' T
)b
on
'%' + a.t + '%'
like
'%' + b.t + '%'
The like pattern only goes on the right side of the operator. I think you intend:
on (a.t like '%' + b.t + '%') or
(b.t like '%' + a.t + '%')

How to make my search for multiple columns procedure more efficient?

I have a stored procedure that searches across multiple columns in a combined table.
It works, however, it takes 15 seconds to search for a value in that combined table. The table takes 9 seconds to load so I'm not sure, maybe it's because my table too big?
So I'm just wondering if there's a way to make this query runs faster.
This is my stored procedure:
create procedure LRMWEB_Search
#input nvarchar(1500)
AS
SET NOCOUNT ON;
SELECT tr.ResourceID ,
tr.ProjectFile,
tr.ResourceFile,
tr.ResourceName,
trt.Culture,
trt.TranslatedFlag,
trt.TranslatedValue,
tr.Comments,
tr.IsApproved
FROM tblResourcesTranslated_NEW trt
INNER JOIN tblResources_NEW tr ON trt.ResourceID = tr.ResourceID
where tr.ResourceID like '%'+ #input + '%'
OR tr.ProjectFile like '%'+ #input + '%'
OR tr.ResourceFile like '%'+ #input + '%'
OR tr.ResourceName like '%'+ #input + '%'
OR tr.ResourceValue like '%'+ #input + '%'
OR tr.Comments like '%'+ #input + '%'
OR trt.Uid like '%'+ #input + '%'
OR trt.TranslatedValue like '%'+ #input + '%'
;
Any use of like precludes an index, unless it has a fixed prefix with a wildcard suffix, such as where foo like 'bar%'. Your like expressions (e.g., '%xxx%' ) do not meet that requirement.
As a result, while the join criteria may well have a covering index, nothing else does and so a table scan of the join tables is required.
In a nutshell, there is no way to fix performance outside of either
rethinking what you're doing, or
using something like a full text search
SELECT tblResources_NEW.ResourceID
,tblResources_NEW.ProjectFile
,tblResources_NEW.ResourceFile
,tblResources_NEW.ResourceName
,tblResourcesTranslated_NEW.Culture
,tblResourcesTranslated_NEW.TranslatedFlag
,tblResourcesTranslated_NEW.TranslatedValue
,tblResources_NEW.Comments
,tblResources_NEW.IsApproved
FROM
(
SELECT
tblResources_NEW.ResourceID
,tblResources_NEW.ProjectFile
,tblResources_NEW.ResourceFile
,tblResources_NEW.ResourceName
,tblResources_NEW.Comments
,tblResources_NEW.IsApproved
FROM tblResources_NEW
WHERE
tblResources_NEW.ResourceID like '%'+ #input + '%'
OR tblResources_NEW.ProjectFile like '%'+ #input + '%'
OR tblResources_NEW.ResourceFile like '%'+ #input + '%'
OR tblResources_NEW.ResourceName like '%'+ #input + '%'
OR tblResources_NEW.ResourceValue like '%'+ #input + '%'
OR tblResources_NEW.Comments like '%'+ #input + '%'
) AS tblResources_NEW
INNER JOIN
(
SELECT
tblResourcesTranslated_NEW.ResourceID
,tblResourcesTranslated_NEW.Culture
,tblResourcesTranslated_NEW.TranslatedFlag
,tblResourcesTranslated_NEW.TranslatedValue
FROM tblResourcesTranslated_NEW
WHERE
tblResourcesTranslated_NEW.Uid like '%'+ #input + '%'
OR tblResourcesTranslated_NEW.TranslatedValue like '%'+ #input + '%'
) AS tblResourcesTranslated_NEW ON tblResourcesTranslated_NEW.ResourceID=tblResources_NEW.ResourceID
If You insist on having one input for all fields, I would at least boost some performance by doing dynamic SQL.
Alter PROCEDURE LRMWEB_Search
(
#input nvarchar(1500)
)
AS
declare #sql varchar(2000);
SET NOCOUNT ON;
set #sql = 'SELECT
tblResources_NEW.ResourceID,
tblResources_NEW.ProjectFile,
tblResources_NEW.ResourceFile,
tblResources_NEW.ResourceName,
tblResourcesTranslated_NEW.Culture,
tblResourcesTranslated_NEW.TranslatedFlag,
tblResourcesTranslated_NEW.TranslatedValue,
tblResources_NEW.Comments,
tblResources_NEW.IsApproved
FROM
tblResourcesTranslated_NEW INNER JOIN
tblResources_NEW ON tblResourcesTranslated_NEW.ResourceID=tblResources_NEW.ResourceID
where ';
-- here I would concatenate all your string conditions
if (ISNUMERIC(#input)= 0)
Begin
. . . . . . .
End
-- here I would concatenate all your numeric conditions
if (ISNUMERIC(#input)= 1)
Begin
set #sql = #sql + ' OR tblResources_NEW.ResourceID like ''%#1%''';
. . . . .
End
-- call to execute
EXECUTE sp_executesql #sql, N'#1 varchar', #1 = #input;
This should give you at least some boost by excluding unneeded searches. But really, you should do something like this by using single parameter for every condition. Than you can exclude those that are Null from building into string.
But, again, this design is BAD.

SQL parameterized query not returning correct results

I have a view in my DB and the view has a row I am trying to search for. I've tested it in sql server and it returned the correct result. However when I try it with parameters from vb it won't return anything. The Sql code that I get a query to return a correct result looks like
SELECT *
FROM
(SELECT
ROW_NUMBER() OVER (ORDER BY groupID DESC) AS Row, *
FROM
SchedulingGroup_VIEW
WHERE
(scheduled = 1)
AND ((building LIKE '%dunn%') OR (room LIKE '%dunn%')
OR (requestBy LIKE '%dunn%') OR (requestFor LIKE '%dunn%')
OR (groupID LIKE '%dunn%') OR (description LIKE '%dunn%'))
AND (NOT EXISTS (SELECT gID FROM facilitiesForm
WHERE facilitiesForm.gID <> gID))) AS TMP
WHERE
(Row BETWEEN 0 AND 100)
The SQL with parameter looks like
SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY groupID DESC) AS Row, *
FROM schedulingGroup_VIEW
WHERE (scheduled = 1) AND
( (building LIKE '%' + #search + '%')
OR (room LIKE '%' + #search + '%')
OR (requestBy LIKE '%' + #search + '%')
OR (requestFor LIKE '%' + #search + '%')
OR (groupID LIKE '%' + #search + '%')
OR (description LIKE '%' + #search + '%'))
AND
(NOT EXISTS (SELECT gID FROM facilitiesForm
WHERE facilitiesForm.gID <> gID))) AS TMP WHERE (Row BETWEEN 0 AND 100)
sqlComm.Parameters.AddWithValue("#search", info.search)
with info.search = "dunn".
The sql query returns the appropriate row but the vb.net with parameters returns nothing.
Move your wildcards (i.e. your % characters) into your VB.net string before you pass it as a parameter.
For example, do this in VB.net code...
sqlComm.Parameters.AddWithValue("#search", "%" + info.search + "%");
And in your SQL when you use the #search parameter in the LIKE statement, don't add in wildcards, like shown below...
WHERE building LIKE #search
See this SO post which is essentially the same question...
How to use wildcards in SQL query with parameters

SQL search issue

I know this is a simple question but here it goes. I've created a search button in Visual Studio using an SQL Statement. It works for first name and last name, but I also want it to search int such employee Id's. Here is the code :
SELECT ID, fName, lName, Discription, Box
FROM tb1
WHERE (fName LIKE '%' + #fName + '%') OR (lName LIKE '%' + #lName + '%') OR (ID LIKE '%' + #ID + '%')
When I test it, I get the error :
"Conversion failed when converting the varchar value '%' to data type int.
ID LIKE '%' + #ID + '%'
ID is an integer, '%' + #ID + '%' is a string. It can't really compare. An integer either is a value or it isn't. The % wildcards wouldn't mean anything to an integer.
I'm assuming what you want to do is convert the integer into a string so that you can match substrings of it. So, for example, searching for "1" would match on any integer which contains a "1" (10, 11, 12, 451, etc.). To do that you should simply need to convert the integer value to a string value in the WHERE clause:
(CAST(ID AS varchar(10)) LIKE '%' + #ID + '%'
(This assumes that your integer will never be more than 10 characters long. Adjust that value as necessary.)
You cannot use LIKE with integers, so you will have to convert the ID to varchar.
To convert you can use CAST or CONVERT:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
Ex:
CAST(ID as varchar)
Your query:
SELECT ID, fName, lName, Discription, Box
FROM tb1
WHERE (fName LIKE '%' + #fName + '%') OR (lName LIKE '%' + #lName + '%') OR (CAST(ID as varchar) LIKE '%' + #ID + '%')
The LIKE syntax is compatible only with character-associated data types. INT types do not fall under this category.
Reference: http://msdn.microsoft.com/en-us/library/ms179859.aspx

Using LIKE within CASE in TSQL

I would like to select the records that contain the content of the #selctDescription parameter but only when #selctDescription is not empty.
I have the following, which does not work:
(t.[description] LIKE
(
CASE
WHEN #selctDescription = '' THEN t.[description]
ELSE ('%' #selctDescription '%')
END
)
)
Can anyone point me in the right direction?
SELECT *
FROM Table
WHERE
((#selctDescription IS NULL OR #selctDescription = '')
OR
(t.[description] LIKE '%' + #selctDescription +'%'))
#selctDescription = '' OR t.[description] LIKE '%' + #selctDescription + '%'
I think your code is equivalent to:
t.[description] LIKE '%' + #selctDescription + '%'
Your description on the other hand, suggests you want this:
#selctDescription <> ''
AND t.[description] LIKE '%' + #selctDescription + '%'
A couple of thoughts for you...
Where you have '%' #selctDescription '%', you just need + between the strings to concatenate them. Your code will then work as is.
'%' + #selctDescription + '%'
Also, it's useful to note that you don't even need the CASE statement, as when the parameter is blank, you get '%%', which will still match everything.
This is useful to know because at present you have table fields on both sides of the LIKE statement. This will really hurt the optimiser. If I were to use CASE I'd be more tempted to stick to...
t.[description] LIKE (CASE WHEN #selctDescription = '' THEN '%' ELSE ('%' + #selctDescription + '%') END)
This has the benefit that the result of the CASE statement can be performed once, as a scalar value, prior to execution of the query. As opposed to being recalculated ever row.
And that said, it then becomes functionally identical to...
t.[description] LIKE ('%' + #selctDescription + '%')
Based on your first line and comments, you need to make the following select:
Select * from table
where field <> ''
and field like '%' + #selctDescription + '%'
But you must put the correct table and field and #selctDesciption must be text (char, nchar, varchar, nvarchar, ...).