Insert comma separated string into temp table - sql - sql

I have a string which data are separated using comma(,) that i wanted to insert into temp table with id as auto increment example string like '12,34,46,767'

Create your own split function and use it
CREATE FUNCTION [dbo].[fnSplitString]
(
#string NVARCHAR(MAX),
#delimiter CHAR(1)
)
RETURNS #output TABLE(splitdata NVARCHAR(MAX)
)
BEGIN
DECLARE #start INT, #end INT
SELECT #start = 1, #end = CHARINDEX(#delimiter, #string)
WHILE #start < LEN(#string) + 1 BEGIN
IF #end = 0
SET #end = LEN(#string) + 1
INSERT INTO #output (splitdata)
VALUES(SUBSTRING(#string, #start, #end - #start))
SET #start = #end + 1
SET #end = CHARINDEX(#delimiter, #string, #start)
END
RETURN
END

Related

Store a CAST to a variable in SQL

A column in SQL had data in an image format. I used CAST and the data spits out in text, which is all great. The problem is the column has HTML tags. I'm trying to strip away HTML tags from a column that has been casted.
I've created a function to remove the HTML tags but now I need to call the function on that casted column.
/* Converting the image to text */
SELECT cast(cast(note as varbinary(max)) as varchar (max)) note
FROM NOTES
/HTML Function/
CREATE FUNCTION [dbo].[udf_StripHTML] (#HTMLText VARCHAR(MAX))
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE #Start INT
DECLARE #End INT
DECLARE #Length INT
SET #Start = CHARINDEX('<',#HTMLText) SET #End =
CHARINDEX('>',#HTMLText,CHARINDEX('<',#HTMLText))
SET #Length = (#End - #Start) + 1 WHILE #Start > 0
AND #End > 0
AND #Length > 0
BEGIN
SET #HTMLText = STUFF(#HTMLText,#Start,#Length,'')
SET #Start = CHARINDEX('<',#HTMLText) SET #End = CHARINDEX('>',#HTMLText,CHARINDEX('<',#HTMLText))
SET #Length = (#End - #Start) + 1
END
RETURN LTRIM(RTRIM(#HTMLText))
END
GO
THE PROBLEM CODE:
SELECT
[Col1]
,[Col2]
,[Col3]
,[Col4]
,dbo.udf_StripHTML(cast(cast(note as varbinary(max)) as varchar (max)) note)
FROM NOTES
How do I call the HTML function with the cast?
I think it's just a syntax error. This should work:
SELECT
[Col1]
,[Col2]
,[Col3]
,[Col4]
,[Note] = dbo.udf_StripHTML(cast(cast([note] as varbinary(max)) as varchar (max)))
FROM NOTES

How to execute a sp number of times on the basis of multiple output from another sp

I have a sp as shown below
ALTER PROCEDURE [dbo].[pPatAssessDel]
#IAllGUIDs nvarchar(max) ,
#IPAsPatID UNIQUEIDENTIFIER,
#IPAsOrgID UNIQUEIDENTIFIER,
#IPAsOrgGrpID UNIQUEIDENTIFIER
AS
declare #output TABLE(splitdata NVARCHAR(MAX) )
DECLARE #start INT, #end INT
SELECT #start = 1, #end = CHARINDEX(',', #IAllGUIDs)
WHILE #start < LEN(#IAllGUIDs) + 1 BEGIN
IF #end = 0
SET #end = LEN(#IAllGUIDs) + 1
INSERT INTO #output (splitdata)
VALUES(SUBSTRING(#IAllGUIDs, #start, #end - #start))
SET #start = #end + 1
SET #end = CHARINDEX(',', #IAllGUIDs, #start)
END
Declare #PAsID NVARCHAR(MAX)
Set #PAsID =(select splitdata From #output)
EXEC pDeleteTbl 'PatAssess',#IPAsOrgID,#IPAsOrgGrpID,#PAsID,#IPAsPatID
Delete From PatAssess where PAsRowGUID in (select splitdata From #output)
AND PAsPatID = #IPAsPatID
I need to execute
EXEC pDeleteTbl 'PatAssess',#IPAsOrgID,#IPAsOrgGrpID,#PAsID,#IPAsPatID
but the query
(select splitdata From #output) returning more than one value so how to execute the pDeleteTbl query ....thanks in advance
you have to use cursor for that
ALTER PROCEDURE [dbo].[pPatAssessDel]
#IAllGUIDs nvarchar(max) ,
#IPAsPatID UNIQUEIDENTIFIER,
#IPAsOrgID UNIQUEIDENTIFIER,
#IPAsOrgGrpID UNIQUEIDENTIFIER
AS
declare #output TABLE(splitdata NVARCHAR(MAX) )
DECLARE #start INT, #end INT
SELECT #start = 1, #end = CHARINDEX(',', #IAllGUIDs)
WHILE #start < LEN(#IAllGUIDs) + 1 BEGIN
IF #end = 0
SET #end = LEN(#IAllGUIDs) + 1
INSERT INTO #output (splitdata)
VALUES(SUBSTRING(#IAllGUIDs, #start, #end - #start))
SET #start = #end + 1
SET #end = CHARINDEX(',', #IAllGUIDs, #start)
END
declare #IPAsID nvarchar(max)
declare cur CURSOR LOCAL for
select splitdata From #output
open cur
fetch next from cur into #IPAsID
while ##FETCH_STATUS = 0 BEGIN
EXEC pDeleteTbl 'PatAssess',#IPAsOrgID,#IPAsOrgGrpID,#IPAsID,#IPAsPatID
fetch next from cur into #IPAsID
END
close cur
deallocate cur
Delete From PatAssess where PAsRowGUID in (select splitdata From #output)
AND PAsPatID = #IPAsPatID

SQL Script to remove anchor tags in sql server

I have a small requirement. I need to remove the anchor tags only in a text (but not remaining HTML tags) but the text should not be removed.
Only anchor tag should be removed.
I got the below script and modified it a bit. It is working with one small issue i.e </a> is not getting removed. Can you please help me with this?
Please tell me if you have better idea.
Eg
Click to
Click to should not get removed
Code:
CREATE FUNCTION [dbo].[udf_StripHTML]
(#HTMLText VARCHAR(MAX))
RETURNS
VARCHAR(MAX)
AS
BEGIN
DECLARE #Start INT
DECLARE #End INT
DECLARE #Length INT
SET #Start = CHARINDEX('<a',#HTMLText)
SET #End = CHARINDEX('>',#HTMLText,CHARINDEX('<',#HTMLText))
SET #Length = (#End - #Start) + 1
WHILE #Start > 0 AND #End > 0 AND #Length > 0
BEGIN
SET #HTMLText = STUFF(#HTMLText,#Start,#Length,'')
SET #Start = CHARINDEX('<a',#HTMLText)
SET #End = CHARINDEX('>',#HTMLText,CHARINDEX('<',#HTMLText))
SET #Length = (#End - #Start) + 1
END
RETURN LTRIM(RTRIM(#HTMLText))
END
GO
try this.
Fiddle Demo Here
declare #HTMLText varchar(max)='<a href=''hello.com''>click me</a>'
DECLARE #Start INT
DECLARE #End INT
DECLARE #Length INT
SET #Start = CHARINDEX('<',#HTMLText)
SET #End = CHARINDEX('>',#HTMLText,CHARINDEX('<',#HTMLText))
SET #Length = (#End - #Start) + 1
WHILE #Start > 0 AND #End > 0 AND #Length > 0
BEGIN
SET #HTMLText = STUFF(#HTMLText,#Start,#Length,'')
SET #Start = CHARINDEX('<',#HTMLText)
SET #End = CHARINDEX('>',#HTMLText,CHARINDEX('<',#HTMLText))
SET #Length = (#End - #Start) + 1
End
select LTRIM(RTRIM(#HTMLText))
EDIT:2 Only To Remove Anchor Tags and
Fiddle Demo
declare #HTMLText varchar(max)='<l href=''fvhjfshdfsd''>gsgdgfsdf</l><a href=''hello.com''>click me</a><b>aa</b>'
DECLARE #Start INT
DECLARE #End INT
DECLARE #Length INT
SET #Start = CHARINDEX('<a',#HTMLText)
SET #End =CHARINDEX('>',#HTMLText,CHARINDEX('<a',#HTMLText))
SET #Length = (#End - #Start) + 1
WHILE #Start > 0 AND #End > 0 AND #Length > 0
BEGIN
SET #HTMLText = STUFF(#HTMLText,#Start,#Length,'')
SET #Start = CHARINDEX('<a',#HTMLText)
SET #End = CHARINDEX('>',#HTMLText,CHARINDEX('<a',#HTMLText))
SET #Length = (#End - #Start) + 1
End
select REPLACE(LTRIM(RTRIM(#HTMLText)),'</a>','')

insert multiple rows from multiple tables - SQL

The input parameters are - 100, 'abc,def,ghi', '10,20,30'. They have to be inserted into a table as
menuid(int) cid(int) code(varchar)
----------------------------------
100 10 abc
100 20 def
100 30 ghi
I have used the function to split string from here - http://www.sqlservercentral.com/blogs/querying-microsoft-sql-server/2013/09/19/how-to-split-a-string-by-delimited-char-in-sql-server/
and here is my code
declare #menuid int = 100
declare #strcode varchar(max)='abc,def,ghi'
declare #strid varchar(max)='10,20,30'
declare #t1 table(
menuid int,
cid int,
code varchar(100)
)
;with t1 as(
select * from fnSplitString(#strid,',')
)
;with t2 as(
select * from fnSplitString(#strcode,',')
)
insert into #t1
...do not know how to proceed from here
I just altered the function used for split & continue to get solution for you ...
ALTER FUNCTION [dbo].[fnSplitString]
(
#string NVARCHAR(MAX),
#delimiter CHAR(1)
)
RETURNS #output TABLE(id int, splitdata NVARCHAR(MAX)
)
BEGIN
DECLARE #id INT,#start INT, #end INT
SELECT #start = 1, #end = CHARINDEX(#delimiter, #string) ,#id=1
WHILE #start < LEN(#string) + 1 BEGIN
IF #end = 0
SET #end = LEN(#string) + 1
INSERT INTO #output (id,splitdata)
VALUES(#id,SUBSTRING(#string, #start, #end - #start))
SET #start = #end + 1
SET #id = #id + 1
SET #end = CHARINDEX(#delimiter, #string, #start)
END
RETURN
END
Next Step
declare #menuid int = 100
declare #strcode varchar(max)='abc,def,ghi'
declare #strid varchar(max)='10,20,30'
declare #t1 table(
menuid int,
cid int,
code varchar(100)
)
insert into #t1
select #menuid,
s1.splitdata AS cid,s2.splitdata AS code
from fnSplitString(#strid,',') s1
INNER JOIN
(
select * from fnSplitString(#strcode,',')
)s2 on s1.id=s2.id
select * from #t1
You can update the function to also return a number for each splitted string like this:
ALTER FUNCTION [dbo].[fnSplitString]
(
#string NVARCHAR(MAX),
#delimiter CHAR(1)
)
RETURNS #output TABLE(splitdata NVARCHAR(MAX), currentIndex int
)
BEGIN
DECLARE #count int = 0
DECLARE #start INT, #end INT
SELECT #start = 1, #end = CHARINDEX(#delimiter, #string)
WHILE #start < LEN(#string) + 1 BEGIN
IF #end = 0
SET #end = LEN(#string) + 1
INSERT INTO #output (splitdata, currentIndex)
VALUES(SUBSTRING(#string, #start, #end - #start), #count)
SET #start = #end + 1
SET #count += 1
SET #end = CHARINDEX(#delimiter, #string, #start)
END
RETURN
END
Now you can join the result of the function in a simple query and insert this into your table.
declare #menuid int = 100
declare #strcode varchar(max)='abc,def,ghi'
declare #strid varchar(max)='10,20,30'
declare #t1 table(
menuid int,
cid int,
code varchar(100)
);
INSERT INTO #t1
SELECT #menuid menuId, codes.splitdata cid, ids.splitdata code FROM fnSplitString(#strid,',') codes LEFT OUTER JOIN
fnSplitString(#strcode,',') ids ON codes.currentIndex = ids.currentIndex

Split data from 2 symbols

have table
CREATE TABLE #tbl
(
id int identity(1,1),
obj_type int ,
obj_id nvarchar(50)
)
have data like : 153:0|114:0|147:0|148:0|152:0|155:0
want insert which data ise before " : " to obj_id , which data is next to " : " insert tu obj_type. it's must be like
id obj_type obj_id
1 0 153
2 0 114
3 0 147
4 0 148
5 0 152
6 0 155
How do it in stored procedure ? not function
declare #S varchar(100) = '153:0|114:0|147:0|148:0|152:0|155:0'
declare #xml xml
select #xml = '<item><value>'+replace(replace(#s, ':','</value><value>'), '|','</value></item><item><value>')+'</value></item>'
select N.value('value[1]', 'int') as obj_id,
N.value('value[2]', 'int') as obj_type
from #xml.nodes('item') as T(N)
SQL Fiddle
You can wait for some experts answer
till then you can give it one chance
insert into #tbl
SELECT LEFT(splitdata, CHARINDEX(':', splitdata) - 1) AS obj_id,
RIGHT(splitdata, CHARINDEX(':', REVERSE(splitdata)) - 1) AS obj_type from (select splitdatafrom fnSplitString(parameterName,'|')
now you can write stringsplit function like this
CREATE FUNCTION [dbo].[fnSplitString]
(
#string NVARCHAR(MAX),
#delimiter CHAR(1)
)
RETURNS #output TABLE(splitdata NVARCHAR(MAX)
)
BEGIN
DECLARE #start INT, #end INT
SELECT #start = 1, #end = CHARINDEX(#delimiter, #string)
WHILE #start < LEN(#string) + 1 BEGIN
IF #end = 0
SET #end = LEN(#string) + 1
INSERT INTO #output (splitdata)
VALUES(SUBSTRING(#string, #start, #end - #start))
SET #start = #end + 1
SET #end = CHARINDEX(#delimiter, #string, #start)
END
RETURN
END
Another Solution :
Create FUNCTION [dbo].[SplitString]
(
#List NVARCHAR(MAX),
#Delim VARCHAR(255)
)
RETURNS TABLE
AS
RETURN ( SELECT [Value] FROM
(
SELECT
[Value] = LTRIM(RTRIM(SUBSTRING(#List, [Number],
CHARINDEX(#Delim, #List + #Delim, [Number]) - [Number])))
FROM (SELECT Number = ROW_NUMBER() OVER (ORDER BY name)
FROM sys.all_objects) AS x
WHERE Number <= LEN(#List)
AND SUBSTRING(#Delim + #List, [Number], LEN(#Delim)) = #Delim
) AS y
);
taken form: T-SQL split string
And then select the values:
Declare
#Text varchar (100) = '153:0|114:0|147:0|148:0|152:0|155:0',
#Delim varchar(50) = ':0|'
select case when CHARINDEX(':0', Value) > 0 then Left(Value, Len(Value)-2) else Value End AS Result from dbo.SplitString(#Text, #Delim)
CREATE procedure [dbo].[Insert_procedure]
#inputString varchar(max)
AS
BEGIN
set #inputString ='2153:770|114:0|147:0|148:0|152:0|155:0' Declare #delimiter char(1) = '|' Declare #delimiter_Colon char(1) = ':'
DECLARE #chIndex int DECLARE #chIndex1 int DECLARE #item varchar(100)Declare #ReverseString varchar(max)
SELECT #ReverseString = Reverse(substring(reverse(#inputString), 1, 1))
IF(#ReverseString <> '|')
set #inputString = #inputString +'|'
WHILE CHARINDEX(#delimiter, #inputString, 0) <> 0
BEGIN
SET #chIndex = CHARINDEX(#delimiter, #inputString, 0)
SELECT #item = SUBSTRING(#inputString, 1, #chIndex - 1)
IF LEN(#item) > 0
BEGIN
set #chIndex1 = CHARINDEX(#delimiter_Colon, #item, 0)
Declare #obj_type int Declare #obj_id varchar(50)
SELECT #obj_id = SUBSTRING(#item, #chIndex1+1,len(#item)) SELECT #obj_type = SUBSTRING(#item,1,#chIndex1-1)
Insert into TEST(obj_type,obj_id) values (#obj_type,#obj_id)
END
SELECT #inputString = SUBSTRING(#inputString, #chIndex + 1, LEN(#inputString))
END
END