473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

May 24 '06 #1
1 3660
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

May 24 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Robin Tucker | last post by:
My code prints out 0 for the value of charindex ( 'Hello.', '.' ). Did I miss something? I think it should print out 6!
5
by: M Wells | last post by:
Hi All, Just wondering if it's any more efficient to use the following SQL statement to return a partial match from a column as opposed to the second statement. SELECT * FROM Table1 WHERE...
1
by: sysindex | last post by:
I am trying to find a way to dynamically retrieve the substring starting point of an nText field. My query looks something like SELECT ID,Substring(DOCTEXT,0,200) from mytable where DOCTEXT...
2
by: Little PussyCat | last post by:
Hello, I need to be able to replace only the first occurance of a space character in a column. Reason being is the data in the column I am trying to replace seems to have umpteen space...
3
by: Tim Soliday | last post by:
I have been looking at regular expressions lately, and I'm hoping that maybe someone could help me? I am trying to find the position of a substring, and then returning the substring including...
4
by: Daviso | last post by:
Hi. I hava an input like this <td><input name=numberA> </td> and a button with a javascript function in onclick event. I want to control that the first element of the value I have...
8
by: chudson007 | last post by:
I need help capturing information from a free text field. The 10 examples below contain examples of the information I am trying to capture. In each cell I am trying to capture the number between...
7
by: jknaty | last post by:
I'm trying to create a function that splits up a column by spaces, and I thought creating a function that finds the spaces with CHARINDEX and then SUBSTRING on those values would an approach. I...
1
by: jeremy | last post by:
Hello, I was hoping someone could help me with this SQL statement. The column 'options.optionDescrip' is a varchar field. Some values of the 'options.optionDescrip' contain commas, some do...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.