Unix Technical Forum

Substring and Charindex Script Not Working

This is a discussion on Substring and Charindex Script Not Working within the SQL Server forums, part of the Microsoft SQL Server category; --> This script doesn't work. Why? UPDATE [460zz_Relma_Comment_Parse] SET [Comments 1] = LTRIM(SUBSTRING([Comments], 1, convert(bigint, CHARINDEX(',', Comments)-1))) WHERE NOT([COMMENTS] IS ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 07:29 PM
db55
 
Posts: n/a
Default Substring and Charindex Script Not Working

This script doesn't work. Why?

UPDATE [460zz_Relma_Comment_Parse]
SET [Comments 1] = LTRIM(SUBSTRING([Comments], 1,
convert(bigint, CHARINDEX(',', Comments)-1)))
WHERE NOT([COMMENTS] IS NULL) AND LEN([Comments]) > 8

Basically, I'm trying to move everything before a comma into [Comments
1] and everything after the comma into [Comments 2]. The comments 2
script works. See below.

UPDATE [460zz_Relma_Comment_Parse]
SET [Comments 2] = LTRIM(SUBSTRING([Comments], convert(bigint,
charindex(',', [Comments])+1), Len([Comments]) - charindex(',',
[Comments])))
WHERE NOT([COMMENTS] IS NULL) AND LEN([Comments]) > 8

This script [Comments 2] runs perfectly but the first one [Comments 1]
above gives me the following error.

Msg 536, Level 16, State 5, Line 1
Invalid length parameter passed to the SUBSTRING function.
The statement has been terminated.

They are basically the same script except I'm adding +1 to one script
and minusing one -1 from the other.

Thanks,

db55

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 07:29 PM
Gert-Jan Strik
 
Posts: n/a
Default Re: Substring and Charindex Script Not Working

db55,

You probably have rows where there is a Comment (of at least 8
characters) that has no ',' in it, as in the following example:

SELECT LTRIM(SUBSTRING(Comments, 1, convert(bigint, CHARINDEX(',',
Comments)-1)))
FROM (
SELECT 'this is just an example' AS Comments
) AS "460zz_Relma_Comment_Parse"
WHERE NOT(COMMENTS IS NULL) AND LEN(Comments) > 8
-- AND Comments LIKE '%,%'

Server: Msg 536, Level 16, State 3, Line 1
Invalid length parameter passed to the substring function.

HTH,
Gert-Jan



db55 wrote:
>
> This script doesn't work. Why?
>
> UPDATE [460zz_Relma_Comment_Parse]
> SET [Comments 1] = LTRIM(SUBSTRING([Comments], 1,
> convert(bigint, CHARINDEX(',', Comments)-1)))
> WHERE NOT([COMMENTS] IS NULL) AND LEN([Comments]) > 8
>
> Basically, I'm trying to move everything before a comma into [Comments
> 1] and everything after the comma into [Comments 2]. The comments 2
> script works. See below.
>
> UPDATE [460zz_Relma_Comment_Parse]
> SET [Comments 2] = LTRIM(SUBSTRING([Comments], convert(bigint,
> charindex(',', [Comments])+1), Len([Comments]) - charindex(',',
> [Comments])))
> WHERE NOT([COMMENTS] IS NULL) AND LEN([Comments]) > 8
>
> This script [Comments 2] runs perfectly but the first one [Comments 1]
> above gives me the following error.
>
> Msg 536, Level 16, State 5, Line 1
> Invalid length parameter passed to the SUBSTRING function.
> The statement has been terminated.
>
> They are basically the same script except I'm adding +1 to one script
> and minusing one -1 from the other.
>
> Thanks,
>
> db55

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 12:35 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com