SQL search issue - sql

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

Related

WHERE clause based on part of a VARCHAR column

I have a query where I need to search the numerical part of a string in SQL Server.
In the number column above needs to be searchable as a variable in the query.
Wildcards does not work:
SELECT PK_Story
FROM Story
WHERE ProductId = #productParam
AND Number LIKE '%' + #numberParam + '%';
because this would also return 132 and 232 for example.
So how can I search for a specific number after the '-'. As you can see I can't do charindex because of the variable prefix length.
What about LIKE '%-' + #numberParam?
You can use substring and charindex combination to get the result.
SELECT PK_Story
FROM Story
WHERE ProductId = #productParam
AND #numberParam like
'%' + case when charindex('-', Number) > 0
then substring(Number, charindex('-', Number) +1, len(Number)) + '%'
else Number
end + '%'
what about this
declare #My_Number as varchar(50)='8'
SELECT PK_Story
FROM Story
WHERE ProductId = #productParam
AND substring(Number, charindex('-', Number) +1, len(Number)) like
#My_Number +'%'
Or, if want equal
SELECT PK_Story
FROM Story
WHERE ProductId = #productParam
AND substring(Number, charindex('-', Number) +1, len(Number)) =
#My_Numbe

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

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

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), '%')

SQL involving columns with comma delimited strings and comparison

I am updating the requirement..Basically A string can have 2 or 3 or 4 decimal places decimal places instead of the two decimal places as show below.
Suppose I have a record with a comma delimited strings in a column name "Version"
Say we have a record (A) containing having these values in its "Version" column
Version=10.1.2.4, 10.4.3.4,11.6.0, (the column has comma delimited strings)
I am passing an sql parameter called #VersionCheck
IF #VersionCheck = 11.6.0 and Version does not have any value like 11.5.6 (Note the 11 matches with the 11 in 11.6.0)
I want to return the record
So here are some examples all comparing with the parameter #VersionCheck= 11.6.0
Version=10.1.2,10.4.3.45 return this record
Version=10.1.2,10.4.3,10.4.4.5,11.6.0 return this record (if we have a match 11.6.0 and there are no strings starting with 11.something that is not equal to 11.6.0(parameter passed) we return this record)
Version= 10.1.2,10.4.3,11.6.0.2 do not return this record (to check, take the string before the first decimal point, in this case is 11, however 11.6.0.2 and 11.6.0 is not the same so it should not match)
How do I write the sql query to have this logic in the where clause?
If the parameter passed is #VersionCheck = 10.6.8 I do not want it to match if the strings in the comma delimited column has say 10.5.4 Basically take the characters before the first decimal and if they are equal (10=10), the rest of its values have to be the same else it should not match.
This is my solution below in sql server 2008 syntax
Note that #SU_Version is my parameter
and SuVersion.fieldValue is the column which has the comma delited string values like 10.1.2,10.3.4 etc
WHERE (CASE WHEN '%#SU_Version%' <> '' AND suVersion.fieldValue LIKE LTRIM('#SU_Version')
THEN suVersion.fieldValue
ELSE '%'
END like CASE WHEN '%#SU_Version%' <> ''
THEN '%#SU_Version%'
ELSE '%'
END
AND
(CASE WHEN LTRIM('#SU_Version') <> '' AND suVersion.fieldValue LIKE SUBSTRING(LTRIM('#SU_Version'),1, CHARINDEX( '.',LTRIM('#SU_Version'))-1)
THEN suVersion.fieldValue
ELSE'%'
END not like CASE WHEN LTRIM('#SU_Version')<> ''
THEN SUBSTRING(LTRIM('#SU_Version'),1, CHARINDEX( '.',LTRIM('#SU_Version'))-1) + '%'
ELSE '%'
END
AND
CASE WHEN LTRIM('#SU_Version') <> '' AND suVersion.fieldValue LIKE SUBSTRING(LTRIM('#SU_Version'),1, CHARINDEX( '.',LTRIM('#SU_Version'))-1)
THEN suVersion.fieldValue
ELSE'%'
END not like CASE WHEN LTRIM('#SU_Version')<> ''
THEN SUBSTRING(LTRIM('#SU_Version'),1, CHARINDEX( '.',LTRIM('#SU_Version'))-1) + '%'
ELSE '%'
END)
)
Since your design basically thwarts any attempts to use an index on the Version column anyway, this will give you rows where there is an exact match
...
WHERE ',' + Version + ',' LIKE '%,' + #VersionCheck + ',%'
For the funky requirements you have, try this (this assumes all version strings will contain exactly two decimal places):
DECLARE #v TABLE(Version VARCHAR(MAX));
INSERT #v SELECT ('10.1.2,10.4.3')
UNION ALL SELECT ('10.1.2,10.4.3,11.6.0')
UNION ALL SELECT ('10.1.2,10.4.3,11.5.2');
DECLARE #SU_Version VARCHAR(32);
SET #SU_Version = '11.6.0';
DECLARE
#p1 VARCHAR(10),
#p2 VARCHAR(10);
SELECT
#p1 = PARSENAME(#SU_Version, 3),
#p2 = PARSENAME(#SU_Version, 2);
SELECT Version = SUBSTRING(Version, 2, LEN(Version)-2) FROM
(
SELECT Version = ',' + Version + ',' FROM #v
) AS v WHERE
(
Version LIKE '%,' + #SU_Version + ',%'
OR
Version NOT LIKE '%,' + #p1 + '.%,%'
AND NOT
(
Version LIKE '%,' + #p1 + '.%,%'
AND
Version NOT LIKE '%,' + #p1 + '.' + #p2 + '.%,%'
)
);

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, ...).