im working on FTS and has been stuck in a problem.With respect to . How do you concatenate strings inside of a CONTAINS in SQL Server 2008?
im trying to use Contains function of FTS
and i need to pass search string either as concatenated string or return string from a scalar function. but both options are not available in FTS.
it does not allow to concatenate search string inside "Contains" Clause
select top 10 * from dbo.staging_table with (nolock)
where contains(text,N'"pakistan"'+'" Lahore"')
select top 10 * from dbo.staging_table with (nolock)
where contains(text,(select from dbo.getcityList()))
second option is again unavailable as it is expecting a string argument.
Im using this query in a view so i cant declare any variable :(
Please help me in resolving this issue
You can use table-valued function instead of a view.
CREATE FUNCTION dbo.fTestFTS (#str NVARCHAR(4000))
RETURNS TABLE
RETURN (
select top 10 *
from dbo.staging_table
where contains(*, #str)
)
SELECT * FROM dbo.fTestFTS(N'"pakistan"' + ' &' + '" Lahore"')
Note the ampersand in search string.
Related
I have an application that takes a comma separated string for multiple IDs to be used in the 'IN' clause of a SQL query.
SELECT * FROM TABLE WHERE [TABLENAME].[COLUMNNAME]
IN
((SELECT '''' + REPLACE('PARAM(0, Enter ID/IDS. Separate multiple ids by
comma., String)', char(44), ''',''') + ''''))
I have tested that PARAM gets the string entered e.g. 'ID1, ID2' but SELECT/REPLACE does not execute. The statement becomes,
SELECT * FROM TABLE WHERE [TABLENAME].[COLUMNNAME]
IN
((SELECT '''' + REPLACE('ID1,ID2', char(44), ''',''') + ''''))
I am trying to achieve,
SELECT * FROM TABLE WHERE [TABLENAME].[COLUMNNAME]
IN ('ID1', 'ID2')
The query does not return any results/errors. I am confident the corresponding records are in the database I am working with. Not sure how to fix this.
You can't do it like this. The IN operator expects a list of parameters separated by comma, but you supply it with a single parameter that happens to contain a comma delimited string:
If you are working on SQL Server version 2016 or higher, you can use the built in string_split to convert the delimited string into a table.
SELECT *
FROM TABLE
WHERE [TABLENAME].[COLUMNNAME]
IN STRING_SPLIT(#CommaDelimitedString, ',')
For older versions, there are multiple user defined functions you can choose from, my personal favorite is Jeff Moden's DelimitedSplit8K. For more options, read Aaron Bertrand's Split strings the right way – or the next best way.
I'm trying to extract xml metadata from from SQL Server's Report Server Database. My current query is listed below:
SELECT
a.Name as 'ReportName'
,a.ReportXML
,Parameter = x.value('(Parameter/Name)[1]','VARCHAR(250)')
FROM (SELECT C.Name,c.itemID,CONVERT(XML,CONVERT(VARCHAR(MAX),C.Parameter)) AS reportXML
FROM ReportServer.dbo.Catalog C
WHERE C.Content is not null
and c.Type = 2 -- Report only
) a
cross apply reportXML.nodes('/Parameters') r (x)
WHERE 1=1
and name ='ReportName'
My objective is to return all parameters associated with a report. The x.value method will only return 1 value at most. (It currently returns the first parameter of the report because 1 is hard coded in the string literal.) I know there are 5 parameters for the report I'm looking at.
Is there another function with similar syntax that will allow me to return all the values? Or is there a wildcard that I can use in place of the number? I've tried multiple functions on msdn with no luck.
Your query is using nodes('/Parameters'). This will return a resultset with one row for each Parameters element - but there is only one probably.
With Parameter = x.value('(Parameter/Name)[1]','VARCHAR(250)') you are reading the first name of the first Parameter. If there are more Parameter elements nested within Parameters you will read only the first...
Without an example of your actual XML it is not easy to answer but:
Try to add one nesting level by adding this below your cross apply
outer apply r.x.nodes('Parameter') AS p (y)
Then change the column to
,Parameter = y.value('Name[1]','VARCHAR(250)')
This should read the first Name element of each Parameter element
If you need further help, please poste an example of your XML.
Btw: I do not understand this:
CONVERT(XML,CONVERT(VARCHAR(MAX),C.Parameter)
What is the initial type of C.Parameter, that you have to cast it to VARCHAR(MAX) and then to XML? If you are still using the deprecated TEXT, NTEXT or IMAGE you should consider to change this!
I've read countless posts on this topic but I can't seem to get any of the recommendations to apply to my particular situation (which isn't different than others...)
I have an SSRS report. Dataset 1 is using a stored procedure and in the where clause I have
and (#param is null or alias.column in
(select Item from dbo.ufnSplit(#param,',')))
I borrowed the dbo.ufnSplit function from this post here: https://stackoverflow.com/a/512300/22194
FUNCTION [dbo].[ufnSplit]
(#RepParam nvarchar(max), #Delim char(1)= ',')
RETURNS #Values TABLE (Item nvarchar(max))AS
--based on John Sansoms StackOverflow answer:
--https://stackoverflow.com/a/512300/22194
BEGIN
DECLARE #chrind INT
DECLARE #Piece nvarchar(100)
SELECT #chrind = 1
WHILE #chrind > 0
BEGIN
SELECT #chrind = CHARINDEX(#Delim,#RepParam)
IF #chrind > 0
SELECT #Piece = LEFT(#RepParam,#chrind - 1)
ELSE
SELECT #Piece = #RepParam
INSERT #Values(Item) VALUES(#Piece)
SELECT #RepParam = RIGHT(#RepParam,LEN(#RepParam) - #chrind)
IF LEN(#RepParam) = 0 BREAK
END
RETURN
END
In dataset 2 I am getting the values that I want to pass to dataset 1
select distinct list from table
My parameter for #param is configured to look at dataset 2 for available values
My issue is that if I select a single value from my parameter dropdown for #param, the report works. If I select multiple values from the dropdown, I only return data for the first value selected.
My values in dataset 2 do not contain any ,'s
Did I miss anything for fail to provide enough information? I'm open to criticism, feedback, do's and don'ts for this, I've struggled with this issue for some time, and by no means a SQL expert :)
Cheers,
MD
Update So SQL Profiler is showing me this:
exec sp... #param=N'value1,value2 ,value3 '
Questions are:
1. Shouldn't every value be wrapped in single quotes?
2. What's with the N before the list?
3. Guessing the trailing spaces need to be trimmed out
When you select multiple values from a parameter dropdown list they are stored in an array. In order to convert that to a string that you can pass to SQL you can use the Join function. Go to your dataset properties and then to the Parameters tab. Replace the Parameter Value with this expression:
=Join(Parameters!param.Value, ",")
It should look like this:
Now your split function will get one comma separated string like it's supposed to. I would also suggest having the split function trim off spaces from the values after it has separated them.
So I figured it out and wanted to post my results here in hopes it helps someone else.
Bad data. One trailing space was blowing up my entire result set, and I didn't notice it until I ran through several scenarios (choosing many combinations of parameters)
My result set had trailing spaces - once I did an rtrim on it I didn't have to do any fancy join/split's in SSRS.
I have a requriment where user will provide many Ids(in Hundres/thousands) in a Text area in vb.net app, I need to use these IDs in T-Sql(Sql Server) to get the data. I dont want to save these Ids in any database table. Just want to pass using a paramater (type of varchar(max)) and use in the procedure.
actually, only read access is permitted for the vb application users.It is Sql-2005 database.Id field is atleaset 12 to 15 characters length.The user will copy/paste data from other source may be CSV or Excel file.
any idea how can i achive this.
any help is appreciated.
Thanks
If you do not want to use Table Valued Parameters, as suggested elsewhere, and you don't want to store the ID's in a temporary table, you can do the following.
Assuming your ID's are integer values, that are separated by commas, in the parameter string, you can use the LIKE operator in your SQL-statement's WHERE filter:
Say you have a parameter, #IDs of type varchar(max). You want to get only those records from MyTable where the ID-column contains a value that has been typed into the #IDs-parameter. Then your query should look something like this:
SELECT * FROM MyTable
WHERE ',' + #IDs + ',' LIKE '%,' + CAST(ID AS varchar) + ',%'
Notice how I prepend and append an extra comma to the #IDs parameter. This is to ensure that the LIKE operator will still work as expected for the first and last ID in the string. Make sure to take the nescessary precautions against SQL injection, for example by validating that users are only allowed to input integer digits and commas.
Try using CHARINDEX:
DECLARE #selectedIDs varchar(max) = '1,2,3'
Set #selectedIDs = ',' + #selectedIds + ',' --string should always start and end with ','
SELECT * FROM YourTable
WHERE CHARINDEX(',' + CAST(IDColumn as VARCHAR(10)) + ',', #selectedIds) > 0
I'm new to SQL Server and I would like to ask a query
Suppose I have a function which returns four values like e.g.
set #RetValue=#PendingFlag + #Status + #BTFound + #ValueDate and RETURN #RetValue.
Now suppose im invoking this function from another function like suppose:
DECLARE #d VARCHAR(max)
SET#d=dbo.ValidateBTtype(#BType,#ReadBT,#Type,#Id,#ClientId,#Customerno,#InterestDate,#ApplicationType)
In #d il have all the four values
But what I want to do is I want the returned values and I want to store it separately in local variables like local variable #PendingFlag=returned value (#Pending Flag) local variable #Status=returned value (#Status) and etc..
Please help me how to store the returned values separately in the second function by invoking the first function
You could return a table from the function, where the table has one row, and four columns, one for each of the return values.
See here for details on table valued functions.
See here 'How to return multiple values from a sql server function'. Or google 'sql server returning multiple values from function' and take your pick.
Cheers -
possible approaches here:
in your dbo.ValidateBTtype function to split the four strings (or values) with comma and then split the #d string in to 4 parts using comma as delimiter
like so:
#RetValue= #PendingFlag + ',' + #Status + ',' + #BTFound + ','+ #ValueDate
...or create table valued function instead..
You need to look into SQL Concepts .
Use SQlClient Class for making an SQL query . Look into DataSet class for it is usually used to store "multiple return values"
http://www.dotnetperls.com/sqlclient