D
Dan Holmes
I am getting an error i don't understand. I would like for someone else to try and repeat it if possible. I am on
Sql2005sp2.
The goal is to take a string and split it based on a delimiter. The following is the split function. You may recognize
the form. I found it from this NG but i don't remember where. tblCalendar is my source of numbers so don't let that
throw you.
CREATE FUNCTION [dbo].[split](@text VARCHAR(MAX), @delimiter VARCHAR(20) = ' ')
RETURNS TABLE
AS
RETURN
(
SELECT ROW_NUMBER() OVER (ORDER BY Id)-1 idx,
SUBSTRING(@text, Id, CAST(CHARINDEX(@delimiter, @text + @delimiter, Id) - CAST(Id AS BIGINT) AS INT)) value
FROM tblCalendar
WHERE Id <= CONVERT(INT, LEN(@text)) AND SUBSTRING(@delimiter + @text, Id, 1) = @delimiter
)
This exercises the above function:
SELECT * FROM dbo.split(
'341|07/25/2008 12:00:00 AM|T,T|752811,752821|07/25/2008 09:00:00 AM,07/25/2008 01:00:00 PM|07/25/2008 09:30:00
AM,07/25/2008 01:30:00 PM|0,0|,,|1'
, '|')
The following causes this error:
Msg 245, Level 16, State 1, Line 11
Conversion failed when converting the varchar value 'æˆrž]Q...........' to data type int.
declare @VehicleID INT
DECLARE vehicle_field_cursor CURSOR
FOR
SELECT Value
FROM dbo.split(
'341|07/25/2008 12:00:00 AM|T,T|752811,752821|07/25/2008 09:00:00 AM,07/25/2008 01:00:00 PM|07/25/2008 09:30:00
AM,07/25/2008 01:30:00 PM|0,0|,,|1'
, '|')
OPEN vehicle_field_cursor
FETCH NEXT FROM vehicle_field_cursor INTO @VehicleID
print @VehicleID
CLOSE vehicle_field_cursor
Deallocate vehicle_field_cursor
I have another version of split that is very procedural that has the same signature as this one and it runs quite well.
I have had to revert to it in my code because of this error.
I would greatly appreciate it if someone would take the time to see if this error happens for them as well. And if
possible find some explanation and/or remedy.
thanks
dan
Sql2005sp2.
The goal is to take a string and split it based on a delimiter. The following is the split function. You may recognize
the form. I found it from this NG but i don't remember where. tblCalendar is my source of numbers so don't let that
throw you.
CREATE FUNCTION [dbo].[split](@text VARCHAR(MAX), @delimiter VARCHAR(20) = ' ')
RETURNS TABLE
AS
RETURN
(
SELECT ROW_NUMBER() OVER (ORDER BY Id)-1 idx,
SUBSTRING(@text, Id, CAST(CHARINDEX(@delimiter, @text + @delimiter, Id) - CAST(Id AS BIGINT) AS INT)) value
FROM tblCalendar
WHERE Id <= CONVERT(INT, LEN(@text)) AND SUBSTRING(@delimiter + @text, Id, 1) = @delimiter
)
This exercises the above function:
SELECT * FROM dbo.split(
'341|07/25/2008 12:00:00 AM|T,T|752811,752821|07/25/2008 09:00:00 AM,07/25/2008 01:00:00 PM|07/25/2008 09:30:00
AM,07/25/2008 01:30:00 PM|0,0|,,|1'
, '|')
The following causes this error:
Msg 245, Level 16, State 1, Line 11
Conversion failed when converting the varchar value 'æˆrž]Q...........' to data type int.
declare @VehicleID INT
DECLARE vehicle_field_cursor CURSOR
FOR
SELECT Value
FROM dbo.split(
'341|07/25/2008 12:00:00 AM|T,T|752811,752821|07/25/2008 09:00:00 AM,07/25/2008 01:00:00 PM|07/25/2008 09:30:00
AM,07/25/2008 01:30:00 PM|0,0|,,|1'
, '|')
OPEN vehicle_field_cursor
FETCH NEXT FROM vehicle_field_cursor INTO @VehicleID
print @VehicleID
CLOSE vehicle_field_cursor
Deallocate vehicle_field_cursor
I have another version of split that is very procedural that has the same signature as this one and it runs quite well.
I have had to revert to it in my code because of this error.
I would greatly appreciate it if someone would take the time to see if this error happens for them as well. And if
possible find some explanation and/or remedy.
thanks
dan