- How to convert a Tsql scalar function into a table function?
- create function dbo.ufn_StripHTML
- ( @Input varchar(max),
- @Delimiter char(1)
- )
- returns varchar(max)
- as
- begin
- declare @Output varchar(max)
- select @Input = replace(replace(@input, '<', @Delimiter), '>', @Delimiter)
- select @Output = isnull(@Output, '') + s
- from ( select row_number() over (order by n.id asc) [i],
- substring(@Delimiter + @Input + @Delimiter, n.id + 1, charindex(@Delimiter, @Delimiter + @Input + @Delimiter, n.id + 1) - n.id - 1) [s]
- from [evolv_cs].[dbo].[progress_note] n
- where n.id = charindex(@Delimiter, @Delimiter + @Input + @Delimiter, n.id) and
- n.id <= len(@Delimiter + @Input)
- ) d
- where i % 2 = 1
- return @Output
- end
- Msg 156, Level 15, State 1, Line 1
- Incorrect syntax near the keyword 'identity'.
- select [progress_note].[note_text], [progress_note].[event_log_id]
- INTO #TEMP_PN
- from [evolv_cs].[dbo].[progress_note]
- group by [progress_note].[event_log_id], [progress_note].[note_text]
- CREATE TYPE dbo.MyTableType AS TABLE
- (
- col1 int identity(1,1) NOT NULL,
- col2 varchar(max) NULL
- )
- GO
- CREATE TABLE [dbo].[MyTable] (
- [col1] [int] identity(1,1) NOT NULL,
- [col2] [varchar](max) NULL
- )
- GO
- create PROC usp_AddRowsToMyTable @MyTableParam MyTableType READONLY, @Delimiter varchar(30)
- as
- INSERT INTO MyTable([col2])
- SELECT [col2]
- FROM @MyTableParam
- --update MyTable
- --set col2 = replace(replace(MyTable.col2, '<', @Delimiter), '>', @Delimiter)
- select s, i, t
- from(
- select MyTableInput.col1 [i],
- replace(replace(MyTable.col2, '<', @Delimiter), '>', @Delimiter) as t,
- substring(@Delimiter + MyTableInput.col2 + @Delimiter, MyTable.col1 + 1,
- charindex(@Delimiter, @Delimiter + MyTableInput.col2 + @Delimiter, MyTable.col1 + 1) - MyTable.col1 - 1) [s]
- from MyTable
- inner join MyTable as MyTableInput on MyTable.col1 = MyTableInput.col1
- where MyTable.col1 = CHARINDEX(@Delimiter, @Delimiter + MyTableInput.col2 + @Delimiter, MyTable.col1)
- and MyTable.col1 <= LEN(@Delimiter + MyTableInput.col2)
- ) d
- DECLARE @MyTable MyTableType
- INSERT INTO @MyTable(col2)
- VALUES ('<h><dsf>2000<h><dsf>'),
- ('<sd><dsf>2001'),
- ('2002<vnv><dsf>'),
- ('<gsd><dsf>2003<h><dsf>'),
- ('<eefs><dsf><h><dsf>2004<dfgd><dsf>')
- EXEC dbo.usp_AddRowsToMyTable @MyTableParam = @MyTable, @Delimiter = '|'
- create function dbo.ufn_StripHTML
- ( @Input varchar(max),
- @Delimiter char(1)
- )
- returns @retYourNewTable table
- (
- id int primary key clustered not null,
- yoursecond column varchar(100) null,
- ....
- )
- as
- ....