Error with function when exporting with SQL Export Wizard - sql

I'm trying to export some entries from a database to a .txt file. I'm only exporting certain columns and this works fine with:
SELECT
Category1, Category2, Category3, Category4
FROM dbo.tbl1
WHERE Category3 = 'JP-4'
AND Category4 > 4
However I also need to remove all html tags within the text when exporting so I've got this function to do so:
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
And this in my export query:
SELECT dbo.udf_StripHTML([Category3])
Any suggestions what the below issue is?
0x80040E14 Description: "Cannot find either column "dbo" or the user-defined function or aggregate "dbo.udf_StripHTML", or the name is ambiguous.".
(SQL Server Import and Export Wizard)

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

Insert comma separated string into temp table - 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

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