I want to select some data in sql express by IN clause and LIKE but no row returned.
my code is here :
SELECT PointId, PointTitleId, PointTitleName, PointTitleContentText
FROM PointsTitlesData
WHERE (PointTitleContentText LIKE '%' + #SearchingClause + '%')
AND (PointId IN ('2','3'))
above code don't return any data but when i use only LIKE code return data.
SELECT PointId, PointTitleId, PointTitleName, PointTitleContentText
FROM PointsTitlesData
WHERE (PointTitleContentText LIKE '%' + #SearchingClause + '%')
how can i use both IN and LIKE?
Thanks.
I tried to recreate your scenario with the script below
Declare #SearchingClause nvarchar(20);
Create table PointsTitlesData (Pointid int, PointTitleContentText nvarchar(20));
insert into PointsTitlesData (Pointid, PointTitleContentText)
values ( 1, 'aaamypoint1aaa'), (2, 'bbbmypoint2bbb'),(3,'cccmypoint3ccc');
Set #SearchingClause = 'mypoint2';
SELECT PointId, PointTitleContentText
FROM PointsTitlesData
WHERE (PointTitleContentText LIKE '%' + #SearchingClause + '%')
AND (PointId IN ('2','3'))
-- above code don't return any data but when i use only LIKE code return data.
SELECT PointId, PointTitleContentText
FROM PointsTitlesData
WHERE (PointTitleContentText LIKE '%' + #SearchingClause + '%')
I got a result for both queries so it looks like you have a data issue.
Have you tried
SELECT PointId from PointsTitlesData where ((PointId = 2) or (PointId = 3))
to check the data is there.
Regards
Jude
probably drop the quotes:
'2','3' should be 2,3
Related
In postgresql (9.6), given a variable length user input of the type 'alice chaplin' or 'alice' or 'alice chaplin meyer' but also 'lic chapl', I would like to search for records that contain 'alice' in column firstname OR column lastname (AND contain 'chaplin' in firstname OR lastname (AND contain 'meyer' in firstname OR lastname)), etc.
I had decided to use ILIKE %searchterm% for the matching, so the query would presumably be along the lines of:
... where
((lastname ILIKE '%' || SEARCHTERM1 || '%') OR (firstname ILIKE '%' || SEARCHTERM1 || '%'))
AND ((lastname ILIKE '%' || SEARCHTERM2 || '%') OR (firstname ILIKE '%' || SEARCHTERM2 || '%'))
AND etc.
After lots of attempts and searching, nothing comes up that resolves this... As a last resort I'll write a very procedural pgplsql function that loops over a split search string, intersecting the ILIKE results, but there has to be some more idiomatic SQL way of resolving such a run of the mill problem.
You can use string_to_array to convert an input string into an array of words. You can then use unnest to convert the array into a (virtual) table, and operate on the words to add '%' before and after. And finally, you can use the ALL comparison using ILIKE ALL (SELECT ...). This ALL will actually be AND-ing the results, as desired.
WITH q AS
(
SELECT 'Alice Chaplin Meyer'::text AS q
)
, words AS
(
SELECT
'%' || word || '%' AS wordish
FROM
q
JOIN LATERAL unnest(string_to_array(q, ' ')) AS a(word) ON true
)
SELECT
*
FROM
t
WHERE
concat_ws(' ', first_name, last_name) ILIKE ALL(SELECT wordish FROM words)
You can check it all at http://rextester.com/LNB38296
References:
string_to_array and unnest
Using ALL
NOTE: This can probably be simplified, but I've prefered a step-by-step approach.
I can't seem to find a solution to this problem. Following is my query:
Declare #MY_STUDENT_ID Varchar(100)
Select #MY_STUDENT_ID = COALESCE(#MY_STUDENT_ID + ',', '') + Convert(varchar, STUDENT_ID) From Some_TABLE Where FISCAL_YEAR = '2014'
SELECT * FROM Table_Students WHERE STUDENT_ID IN (#MY_STUDENT_ID)
Basically first query runs and give me all student IDs as a string concatenated with , for e.g. 1,2,3
And then this value is passed into second query but second query is giving this error which I have posted in title. No idea what to do so any help will be appreciated.
Type of STUDENT_ID field is int.
There is absolutely no need to mess about with comma delimited lists here.
Just use the sub query directly
SELECT *
FROM Table_Students
WHERE STUDENT_ID IN (SELECT StudentId
From Some_TABLE Where FISCAL_YEAR = '2014')
Your approach does not work as it ends up generating something with semantics of
SELECT *
FROM Table_Students
WHERE STUDENT_ID IN ('1,2,3')
Which is not the same as
SELECT *
FROM Table_Students
WHERE STUDENT_ID IN (1,2,3)
As it just is a single string parameter with contents that happen to resemble an in list, rather than 3 int parameters.
You could do so using dynamic SQL, but in this scenario, Martin SMith's answer seems to be better. Should you, however, wish to use dynamic SQL, this would be the way to do so (untested pseudo-code):
Declare #MY_STUDENT_ID varchar(100);
DECLARE #sql nvasrchar(max);
Select #MY_STUDENT_ID = COALESCE(#MY_STUDENT_ID + ',', '') + Convert(varchar, STUDENT_ID) From Some_TABLE Where FISCAL_YEAR = '2014'
SELECT #sql = 'SELECT * FROM Table_Students WHERE STUDENT_ID IN (' + #MY_STUDENT_ID + ')';
EXEC sp_executesql #sql;
I have a small problem and I know a few solutions for it, but I don't know the best way (or less dirty spaghetti way) to go.
I have a string variable, and I need to use it in a like expression.
So:
declare #a varchar(100) = 'my string to use as a join from'
select *
from table
where
column like '%' + #a + '%'
But I don't want any rows from table that contains the #a variable, I want any rows from table that are contained in the #a variable, so:
select *
from table
where
#a like '%' + column + '%'
Result:
'my string'
'as a join from'
But now I need to remove the matched rows from the #a variable. How can I do that?
(edit)
expected result:
#a = ' to use '
'my string'
'as a join from'
You can alter the value of #a with each matched row in the select:
select #a = replace(#a, MyColumn, '')
from MyTable
where #a like '%' + MyColumn + '%'
Demo: http://www.sqlfiddle.com/#!3/187d6/5 (result: #a = ' to use ')
If you want to get a result set as well, use a temp table to store matching rows:
select MyColumn
into #Temp
from MyTable
where #a like '%' + MyColumn + '%'
-- #a = ' to use '
select #a = replace(#a, MyColumn, '') from #Temp
-- returns result set of words that matched
select * from #Temp
drop table #Temp
Demo: http://www.sqlfiddle.com/#!3/187d6/13
I have to select all fields which contain a specific text and ended with ", ', ) or blank space characters or this text is placed in the end of string.
So, I need something like that:
select *
from MyTable
Where Column1 like '%' + #text + '["'') ]%'
This query works fine until text is not placed in the end of Column1.
You could use
Where Column1 + '"' like '%' + #text + '["'') ]%'
Why not try OR?
Where Column1 like '%' + #text + '["'') ]%'
or
Column1 like '%' + #text + '["'') ]'
Or do I misunderstand the requirement? Your question is a little hard to follow.
SELECT *
FROM MyTable
WHERE Column1 LIKE '%' + #text + '%["'') ]'
OR Column1 LIKE '%' + #text
This should work better and faster:
SELECT * FROM MyTable
WHERE
( (RIGHT(Column1,1) IN (' ', '"', '''', ')'))
AND
(SUBSTRING(Column1, LENGTH(#text)-1, LENGTH(#text))=#text)
)
OR ( RIGHT(Column1, LENGTH(#text))=#text )
Duplicate of: TSQL varchar string manipulation
I'm building a dynamic SQL statement out of parameters from a reporting services report. Reporting services passes MutiValue Parameters in a basic CSV format. For example a list of states may be represented as follows: AL,CA,NY,TN,VA
In a SQL statement this is OK:
WHERE customerState In (#StateList)
However, the dynamic variant isn't OK:
SET #WhereClause1 = #WhereClause1 + 'AND customerState IN (' + #StateList + ') '
This is because it translates to (invalid SQL):
AND customerState IN (AL,CA,NY,TN,VA)
To process it needs something like this:
AND customerState IN ('AL','CA','NY','TN','VA')
Is there some cool expression I can use to insert the single quotes into my dynamic SQL?
REPLACE didn't work for me when used with IN for some reason. I ended up using CHARINDEX
WHERE CHARINDEX( ',' + customerState + ',', ',' + #StateList + ',' ) > 0
For anyone attempting to use Dynamic SQL with a multi-valued parameter in the where clause AND use it to run an SSRS report, this is how I got around it...
create table #temp
(id, FName varchar(100), LName varchar(100))
declare #sqlQuery (nvarchar(max))
set #sqlQuery =
'insert into #temp
select
id,
FName,
LName
from dbo.table'
exec (#sqlQuery)
select
FName, LName
from #temp
where #temp.id in (#id) -- #id being an SSRS parameter!
drop table #temp
Granted, the problem with this query is that the dynamic SQL will select everything from dbo.table, and then the select from #temp is where the filter kicks in, so if there's a large amount of data - it's probably not so great. But... I got frustrated trying to get REPLACE to work, or any other solutions others had posted.
This takes care of the middle:
SET #StateList = REPLACE(#StateList, ',', ''',''')
Then quote the edges:
SET #WhereClause1 = #WhereClause1 + 'AND customerState IN (''' + #StateList + ''') '