Thursday, 19 September 2013

Subtract two CHARINDEX values

Subtract two CHARINDEX values

This works fine..
SELECT CHARINDEX(CHAR(10), AFIELD)[first]
,CHARINDEX(CHAR(10), AFIELD,CHARINDEX(CHAR(10), AFIELD)+1)[second]
FROM SOMETABLE
But when I try to subtract the two, the results are empty..
I have followed the information posted here and at -
http://phelabaum.com/archive/tag/charindex/
SELECT SUBSTRING(AFIELD,
CHARINDEX(CHAR(10),AFIELD)+1,
CHARINDEX(CHAR(10),AFIELD,CHARINDEX(CHAR(10),AFIELD)+1) -
CHARINDEX(CHAR(10),AFIELD)) as [first_length]
FROM SOMETABLE
When I run this, I get: the message:
Msg 537, Level 16, State 3, Line 1 Invalid length parameter passed to the
LEFT or SUBSTRING function.
The field is CHAR(10) or Line Feed delimited.

No comments:

Post a Comment