Select * from table where column = (any value from a comma delimited string) - sql

The user selects various words from a drop down list and these values get added into a comma delimited string. When passing the string to a stored procedure I want it to select * from a table where that word exists.
Table
id----word
1-----cat
2-----dog
3-----mouse
4-----dog
string that is passed into the stored procedure is cat, dog so returning columns 1, 2 and 4.
Is there a way of doing this in sql server?

Use IN:
SELECT *
FROM your_table
WHERE word IN ('cat', 'dog')

you first need to make a function SplitCSV :
CREATE FUNCTION [dbo].[SplitCSV] (#CSVString VARCHAR(8000), #Delimiter CHAR(1))
RETURNS #temptable TABLE (items VARCHAR(8000))
AS
BEGIN
DECLARE #pos INT;
DECLARE #slice VARCHAR(8000);
SELECT #pos = 1;
IF LEN(#CSVString) < 1 OR #CSVString IS NULL RETURN;
WHILE #pos!= 0
BEGIN
SET #pos = CHARINDEX(#Delimiter,#CSVString);
IF #pos != 0
SET #slice = LEFT(#CSVString, #pos - 1);
ELSE
SET #slice = #CSVString;
IF( LEN(#slice) > 0)
INSERT INTO #temptable(Items) VALUES (#slice);
SET #CSVString = RIGHT(#CSVString, LEN(#CSVString) - #pos);
IF LEN(#CSVString) = 0 BREAK;
END
RETURN
END
GO
then you can use it like :
SELECT *
FROM myTable
WHERE ID IN (
SELECT items FROM [dbo].[SplitCSV]('1,2,3,4,5', ',')
)

SELECT *
FROM Table
WHERE '%,' + Word + ',%' LIKE ',' + #your_csv_param + ','
Extra commas at the begin and end of parameter and column are to prevent search to match cat with catfish for example.

If you want select all animal except mouse , you can use NOT IN
SELECT * FROM
TABLE
WHERE Word Not IN('Mouse')
So you can avoid type many type of animal

CREATE FUNCTION
ALTER FUNCTION [dbo].[fn_Split](#text varchar(8000), #delimiter varchar(20) = ' ')
RETURNS #Strings TABLE
(
position int IDENTITY PRIMARY KEY,
value varchar(8000)
)
AS
BEGIN
DECLARE #index int
SET #index = -1
WHILE (LEN(#text) > 0)
BEGIN
SET #index = CHARINDEX(#delimiter , #text)
IF (#index = 0) AND (LEN(#text) > 0)
BEGIN
INSERT INTO #Strings VALUES (#text)
BREAK
END
IF (#index > 1)
BEGIN
INSERT INTO #Strings VALUES (LEFT(#text, #index - 1))
SET #text = RIGHT(#text, (LEN(#text) - #index))
END
ELSE
SET #text = RIGHT(#text, (LEN(#text) - #index))
END
RETURN
END
----
select * from yourtable where column in ( select value from fn_Split(#para1,',')

Related

Split string by two delimiters into two columns

I have a string value which has numeric values separated by comma and then by a pipe. I want to split them into a table with two columns. I could split the string by one delimiter but unfortunately couldn't find a way to split by two. Please help.
DECLARE #list NVARCHAR(MAX) = '1,101|2,202|3,303';
The result should be like below.
1 101
2 202
3 303
Thanks in advance.
If you're using SQL Server 2016 or Azure, you have access to the new SPLIT_STRING function. If not I recommend using Jeff Moden's DelimitedSplit8K function, which is widely regarded as the fastest, most efficient SQL based string splitter available...
DECLARE #list NVARCHAR(MAX) = '1,101|2,202|3,303';
SELECT
Col1 = LEFT(dsk.Item, sl.SplitLocation - 1),
Col2 = SUBSTRING(dsk.Item, sl.SplitLocation + 1, LEN(dsk.Item))
FROM
dbo.DelimitedSplit8K(#list, '|') dsk -- code for DelimitedSplit8K can be found here... http://www.sqlservercentral.com/articles/Tally+Table/72993/
CROSS APPLY ( VALUES (ISNULL(NULLIF(CHARINDEX(',', dsk.Item, 1), 0), 1)) ) sl (SplitLocation);
CREATE FUNCTION [dbo].[fn_Split_char](#text nvarchar(max), #delimiter varchar(20) = ' ')
RETURNS #Strings TABLE
(
position int IDENTITY PRIMARY KEY,
value nvarchar(max)
)
AS
BEGIN
DECLARE #index int
SET #index = -1
WHILE (LEN(#text) > 0)
BEGIN
SET #index = CHARINDEX(#delimiter , #text)
IF (#index = 0) AND (LEN(#text) > 0)
BEGIN
INSERT INTO #Strings VALUES (#text)
BREAK
END
IF (#index > 1)
BEGIN
INSERT INTO #Strings VALUES (LEFT(#text, #index - 1))
SET #text = RIGHT(#text, (LEN(#text) - #index))
END
ELSE
SET #text = RIGHT(#text, (LEN(#text) - #index))
END
RETURN
END
Select LEFT(value, Charindex(',', value) - 1) ,
RIGHT(value, Charindex(',', Reverse(value)) - 1) ,
* from [fn_Split_char] ('1,101|2,202|3,303', '|')
Use xml path and cross apply to create multiple rows for a single row based on the pipe separator and then use substring w.r.t the commas to derive two desired columns
Create table #temp(list nvarchar(max))
Insert into #temp values('1,101|2,202|3,303')
SELECT
Substring(Tbl.Col.value('./text()[1]','varchar(50)'),1,1)as col1,
Substring(Tbl.Col.value('./text()[1]','varchar(50)'),charindex(',',Tbl.Col.value('./text()[1]','varchar(50)'),1)+1,len(Tbl.Col.value('./text()[1]','varchar(50)')))
FROM
(Select cast('<a>'+ replace((SELECT list As [*] FOR XML PATH ('')), '|', '</a><a>') + '</a>' as xml)as t
from #temp) tl
Cross apply
tl.t.nodes('/a') AS Tbl(Col)
Try using this Table-valued Function, embed this SP to your main SP
ALTER FUNCTION [dbo].[delimiter]
(
#PARAM_IDS AS VARCHAR(MAX)
#PARAM_DELIMITER AS CHAR(1)
)
RETURNS
#NEW_TABLE TABLE
(
NUM INT NOT NULL IDENTITY,
ID INT NOT NULL
)
AS
BEGIN
DECLARE #NEXTSTRING AS NVARCHAR(MAX);
DECLARE #POS AS INT;
DECLARE #STRING AS NVARCHAR(MAX);
DECLARE #DELIMITER AS NVARCHAR(MAX);
SET #STRING = #PARAM_IDS;
SET #DELIMITER = #PARAM_DELIMITER;
SET #STRING = #STRING + #DELIMITER;
SET #POS = CHARINDEX(#DELIMITER,#STRING);
WHILE (#POS <> 0)
BEGIN
SET #NEXTSTRING = SUBSTRING(#STRING,1,#POS - 1);
INSERT #NEW_TABLE (ID) VALUES (#NEXTSTRING);
SET #STRING = SUBSTRING(#STRING,#POS+1,len(#STRING));
SET #POS = CHARINDEX(#DELIMITER,#STRING);
END
RETURN
END
then example of use
SET #DETAILS_COUNT = (SELECT COUNT(*) FROM delimiter(#PARAM_MS_UNIT_ID, #DELIMITER));

SQL - parametrized procedure with multiple parameters as array

I have very simple procedure:
CREATE PROCEDURE [Report]
#statusValue varchar(200) = '%'
AS
BEGIN
SELECT * FROM SomeTable
WHERE Something LIKE UPPER(#statusValue)
END
I'd like to provide user set multiple statusValue. Because there is 6 levels of statusValue in my table, I'd like to provide user to define required statusValue into procedure parameters - something like array.
I don't know, how it exactly works - I'm very new in this area - but I'm supposing to have procedure like this one:
EXEC Report #statusValue = 'statusValue1|statusValue2|statusValue3'
Do you happen to know, how can I adjust my procedure to have required output. Many thanks in advance.
Use following user defined function to return values from delimited string (say pipe):
CREATE FUNCTION [dbo].[stringlist_to_table]
(#list varchar(8000),
#delimiter nchar(1) = N',')
RETURNS #tbl TABLE (value varchar(8000)) AS
BEGIN
DECLARE #pos int,
#tmpstr varchar(8000),
#tmpval varchar(8000);
SET #tmpstr = #list;
SET #pos = charindex(#delimiter , #tmpstr);
WHILE #pos > 0
BEGIN
SET #tmpval = ltrim(rtrim(left(#tmpstr, #pos - 1)))
INSERT #tbl (value) VALUES(#tmpval)
SET #tmpstr = substring(#tmpstr, #pos + 1, len(#tmpstr))
SET #pos = charindex(#delimiter, #tmpstr)
END
INSERT #tbl(value) VALUES (ltrim(rtrim(#tmpstr)));
RETURN
END
Now use the following procedure to get the required output:
CREATE PROCEDURE [Report]
#statusValue varchar(200) = '%'
AS
BEGIN
DECLARE #iterator INT = 1;
DECLARE #total INT = 1;
DECLARE #keyword VARCHAR(100) = '';
SELECT ROW_NUMBER() OVER (ORDER BY value) SNo, value keyword
INTO #temp
FROM dbo.stringlist_to_table(#statusValue, '|')
SELECT *
INTO #output
FROM SomeTable
WHERE 1 = 0;
SELECT #total = MAX(SNo), #iterator = MIN(Sno)
FROM #temp
WHILE (#iterator <= #total)
BEGIN
SELECT #keyword = '%' + keyword + '%'
FROM #temp
WHERE SNo = #iterator;
INSERT INTO #output
SELECT *
FROM SomeTable
WHERE Something LIKE #keyword
SET #iterator = #iterator + 1;
END
SELECT *
FROM #output;
DROP TABLE #output, #temp;
END
You need the split function in this case. As there is no way to handle what you need. Another approach to add many variables. But in your case it will be enough to create split function and use it to parse your string. Please find the split function below:
CREATE FUNCTION [dbo].[ufnSplitInlineStringToParameters] (
#list NVARCHAR(MAX)
,#delim NCHAR(1) = ','
)
RETURNS TABLE
AS
RETURN
WITH csvTbl(START, STOP) AS (
SELECT START = CONVERT(BIGINT, 1)
,STOP = CHARINDEX(#delim, #list + CONVERT(NVARCHAR(MAX), #delim))
UNION ALL
SELECT START = STOP + 1
,STOP = CHARINDEX(#delim, #list + CONVERT(NVARCHAR(MAX), #delim), STOP + 1)
FROM csvTbl
WHERE STOP > 0
)
SELECT LTRIM(RTRIM(CONVERT(NVARCHAR(4000), SUBSTRING(#list, START, CASE
WHEN STOP > 0
THEN STOP - START
ELSE 0
END)))) AS VALUE
FROM csvTbl
WHERE STOP > 0
GO
One of the simplest way to achieve this is using a custom type. Sample snippet as follows:
CREATE TYPE dbo.StatusList
AS TABLE
(
statusValue varchar(200)
);
GO
CREATE PROCEDURE [Report]
#statusValue AS dbo.StatusList READONLY
AS
BEGIN
SELECT * FROM SomeTable
WHERE Something IN (SELECT * FROM #statusValue)
END
GO
--EDIT---
If you are using SSMS, you can execute the procedure as follows:
DECLARE #status dbo.StatusList
INSERT INTO #status VALUES('Pending')
EXEC [Report] #status

remove numeric values in data using sql

my data is :
Id Case ID#
1. QCCR1A105369, QCCR1A104986 ,QCCR1A103717
2. QCIM1A106902,4613410733 QCIM1A106902; 4613344523 QCIM1A105842; 4614004212 QCIM1A106580; 4614060189 QCIM1A106676
3. QCCR1D93616, QCCR1D92488, QCCR1D58461
4. QCCR1B40216 .... ,QCCR1B39080, QCCR1B40216, QCCR1B39745, QCCR1B38463 , QCCR1B38618, QCCR1B38619, QCCR1B38620, QCCR1B38621, QCCR1B38622, QCCR1B38465, QCCR1B38623
5. QCCR2A30221 QCCR2A30223 QCCR2A30222 QCIM2A30416
My output will be Id 1,3,4,5. I want only that rows, which have starting value QC not any numeric value. For ID 2 you can see there are some numeric values, please tell me how can I achieve it.
You could use a table valued function to split your value by a delimiter like this:
CREATE FUNCTION [dbo].[Split]
(
#ItemList NVARCHAR(MAX),
#delimiter CHAR(1)
)
RETURNS #ItemTable TABLE (Item VARCHAR(250))
AS
BEGIN
DECLARE #tempItemList NVARCHAR(MAX)
SET #tempItemList = #ItemList
DECLARE #i INT
DECLARE #Item NVARCHAR(4000)
SET #i = CHARINDEX(#delimiter, #tempItemList)
WHILE (LEN(#tempItemList) > 0)
BEGIN
IF #i = 0
SET #Item = #tempItemList
ELSE
SET #Item = LEFT(#tempItemList, #i - 1)
INSERT INTO #ItemTable(Item) VALUES(#Item)
IF #i = 0
SET #tempItemList = ''
ELSE
SET #tempItemList = RIGHT(#tempItemList, LEN(#tempItemList) - #i)
SET #i = CHARINDEX(#delimiter, #tempItemList)
END
RETURN
END
Then this query returns the expected result:
SELECT t.*
FROM dbo.TableName t
WHERE NOT EXISTS
(
SELECT 1 FROM dbo.Split(t.CaseID, ',') split
WHERE NOT LEFT(LTRIM(split.Item), 2) = 'QC'
OR ISNUMERIC(split.Item) = 1
)
Demo with your sample data. But in general you should avoid multiple values in one column. Instead store it in separate rows in a linked table. That's called database normalization.

User Defined Function to get longest word in a string in SQL Server

I've trying to create a UDF in SQL to return the longest word in a string. I've created the following but I cant get it to work properly. Any suggestions?
CREATE FUNCTION [dbo].[ufn_Longestword] (#input varchar(255))
RETURNS varchar(100)
AS
BEGIN
declare #pos int
declare #pos2 int
declare #wordpos int
declare #longestword varchar(100)
declare #Letter1 varchar (1)
declare #Letter2 varchar (1)
declare #twords table (
words varchar(100))
SET #pos = 1
WHILE #pos <= len(#input)
BEGIN
SET #Letter1 = substring(#input, #pos, 1)
IF #Letter1 = ' '
BEGIN
SET #pos2 = #pos
WHILE #pos2 <= len(#input)
BEGIN
SET #Letter2 = substring(#input, #pos2, 1)
if #letter2 = ' '
BEGIN
insert into #twords
select SUBSTRING(#input, #pos,#pos2 - #pos)
END
SET #pos2 = #pos2 + 1
END
END
SET #pos = #pos + 1
END
SET #longestword = (select top 1 words from #twords
ORDER BY len(words)desc)
delete from #twords
RETURN #longestword
END
I#m trying to get the different between the 2 spaces and insert that word into a temp table but it doesnt work.
Instead you can use this.
DECLARE #str VARCHAR(5000)='aaaa bbbbb cccccccc'
SELECT TOP 1 Split.a.value('.', 'VARCHAR(100)') as longest_Word
FROM (SELECT Cast ('<M>' + Replace(#str, ' ', '</M><M>') + '</M>' AS XML) AS Data) AS A
CROSS APPLY Data.nodes ('/M') AS Split(a)
ORDER BY Len(Split.a.value('.', 'VARCHAR(100)')) DESC
Result : cccccccc
i found this as solution :
click Here
CREATE FUNCTION FN_ex06(#str varchar(8000) )
RETURNS #T TABLE
( position int IDENTITY PRIMARY KEY,
value varchar(8000) ,
length smallint null
)
AS
BEGIN
DECLARE #i int
SET #i = -1
WHILE (LEN(#str) > 0)
BEGIN
SET #i = CHARINDEX(' ' , #str) /* here i used space as delimiter*/
IF (#i = 0) AND (LEN(#str) > 0)
BEGIN
INSERT INTO #T (value, length) VALUES (#str, LEN(#str))
BREAK
END
IF (#i > 1)
BEGIN
INSERT INTO #T (value, length) VALUES (LEFT(#str, #i - 1),
LEN(LEFT(#str, #i - 1)))
SET #str = RIGHT(#str, (LEN(#str) - #i))
END
ELSE
SET #str = RIGHT(#str, (LEN(#str) - #i))
END
RETURN
END
to run this function you treat it as table because return is a table
select max(value) from FN_ex06('karim pentester')
result is : pentester
of course you select all other columns in return table

Getting id values from a column and then get value from another table

I have a column value separated by comma as
GoalTag:All Tags,TaskTag:All Tags,GoalId:All,TaskId:All,MaxGoal:5,MaxTask:5
As you can see I have 6 values separated by comma, so when I do split the first value will be
GoalTag:All Tags
How I do this (get the values seperated by comma is) by calling a table valued function
Select * from dbo.CustomSplit((SELECT FilterNames FROM TblUserFilterView where UserId = 325 AND Entity = 'Dashboard'),',')
The definition for dbo.CustomSplit looks like
ALTER FUNCTION [dbo].[CustomSplit](#String varchar(8000), #Delimiter char(1))
returns #temptable TABLE (Items varchar(8000))
as
begin
declare #idx int
declare #slice varchar(8000)
select #idx = 1
if len(#String)<1 or #String is null return
while #idx!= 0
begin
set #idx = charindex(#Delimiter,#String)
if #idx!=0
set #slice = left(#String,#idx - 1)
else
set #slice = #String
if(len(#slice)>0)
insert into #temptable(Items) values(#slice)
set #String = right(#String,len(#String) - #idx)
if len(#String) = 0 break
end
return
end
Now what I need to do is, I need to get the value after the ":" i.e. "All Tags" it may be some id for some other records let's say it may be "142". I need to get this Id and then get the corresponding value from the table.
How can I do this?
Try this:
SELECT Substring(s.items, 1 + Charindex ( ':', s.items),
Len(s.items) - Charindex (':',
s.items))
FROM (SELECT *
FROM dbo.Customsplit((SELECT filternames
FROM tbluserfilterview
WHERE userid = 325
AND entity = 'Dashboard'), ',')) AS s
You may create another function:
CREATE FUNCTION [dbo].[Customsplit2](#string VARCHAR(8000),
#Delimiter CHAR(1))
returns VARCHAR(4000)
AS
BEGIN
DECLARE #result NVARCHAR(4000)
SELECT #result = Substring(#string, 1 + Charindex ( #Delimiter, #string),
Len(#string) - Charindex (#Delimiter,
#string)
)
RETURN #result
END
And use it like:
SELECT [dbo].Customsplit2(s.items, ':') AS Tag
FROM (SELECT *
FROM dbo.Customsplit((SELECT filternames
FROM tbluserfilterview
WHERE userid = 325
AND entity = 'Dashboard'), ',')) AS s