Transact-sql code does not work. HELP!

Hi all, I am new to transact-sql and hoped that someone here might be able to help. I have a db with a field called "part" ... part contains text in the format:

xxxxx-xx-xxxxx

(the number of x's before or after each hyphen vary) such that I could have xxx-xxxxx-xxx as the part.

Someone wrote this transact-sql to take my current "part" field and move it to three different part fields ... a,b,c

If xxxxx-xx-xxxxx was the part in the original db, then a would contain xxxxx .. b contains xx ... c contains xxxxx

Make sense? Here is the code. It doesn't work and I can't seem to figure out why! It looks like it is just trying to find the hyphens and cut out the text from that... but it fails with an error that says "invalid length parameter passed to the substring function". Note, if I take off the "-1" from each line below... then the function works but produces the wrong data.

char(45) = "-" (hyphen)

UPDATE [dbo].[parts] SET
a = SUBSTRING(part,1,CHARINDEX(CHAR(45),part)-1),
b = SUBSTRING(part,CHARINDEX(CHAR(45),part)+1,CHARINDEX(CHAR(45),RIGHT(part,LEN(part)-CHARINDEX(CHAR(45),part)))-1),
c = SUBSTRING(part,CHARINDEX(CHAR(45),RIGHT(part,LEN(part)-CHARINDEX(CHAR(45),part)))+CHARINDEX(CHAR(45),part)+1,LEN(part))
GO

Does anyone know what I can do to fix this or is it much more difficult to do than what it seems?

Thanks!

[1465 byte] By [Butterflyangel02] at [2007-12-22]
# 1

Hi

It looks like your update expects to always have 2 hyphens in each value of the "part" column. The error is coming from those values

To find those not matching the pattern , run

select * from parts where part not like '%-%-%'

Your script willl work for

UPDATE [dbo].[parts] SET
a = SUBSTRING(part,1,CHARINDEX(CHAR(45),part)-1),
b = SUBSTRING(part,CHARINDEX(CHAR(45),part)+1,CHARINDEX(CHAR(45),RIGHT(part,LEN(part)-CHARINDEX(CHAR(45),part)))-1),
c = SUBSTRING(part,CHARINDEX(CHAR(45),RIGHT(part,LEN(part)-CHARINDEX(CHAR(45),part)))+CHARINDEX(CHAR(45),part)+1,LEN(part))

WHERE part like '%-%-%'

NB.

NB2006 at 2007-8-30 > top of Msdn Tech,SQL Server,Transact-SQL...
# 2

DECLARE @hyphen char(45)

SET @hyphen = "-"

UPDATE [dbo].[parts] SET
a = SUBSTRING(part,1,CHARINDEX(@hyphen,part)-1),
b = SUBSTRING(part,CHARINDEX(@hyphen,part)+1,CHARINDEX(@hyphen,RIGHT(part,LEN(part)-CHARINDEX(@hyphen,part)))-1),
c = SUBSTRING(part,CHARINDEX(@hyphen,RIGHT(part,LEN(part)-CHARINDEX(@hyphen,part)))+CHARINDEX(@hyphen,part)+1,LEN(part))
GO

Aego at 2007-8-30 > top of Msdn Tech,SQL Server,Transact-SQL...
# 3

Hi, Thanks for your input.

I tried your idea above which produces a running script... but all I get now is NULL in the a,b,c fields.

Thoughts?

Butterflyangel02 at 2007-8-30 > top of Msdn Tech,SQL Server,Transact-SQL...
# 4

I got yours to work also, except that all I get is NULL in a,b,c

I found the 670 records I have that don't have 2 hyphens and excluded them with the WHERE part like '%-%-%' statement.

Is there some way to printout what a b c are as it goes thru the records?

Thanks!!

Butterflyangel02 at 2007-8-30 > top of Msdn Tech,SQL Server,Transact-SQL...
# 5

Try this query.

Update [dbo].[parts]
Set A=ParseName(Replace(part, '-', '.'), 3),
B=ParseName(Replace(part, '-', '.'), 2),
C=ParseName(Replace(part, '-', '.'), 1)
Where len(part) - len(Replace(part, '-', '')) = 2

mastros at 2007-8-30 > top of Msdn Tech,SQL Server,Transact-SQL...
# 6

More info>

For example, I created table called Test.

part = char(20)

a = char(20)

I then ran the scripts above.

part contains KKKK-KK-KKKK

a should have contained KKKK but was null after the script ran.

Butterflyangel02 at 2007-8-30 > top of Msdn Tech,SQL Server,Transact-SQL...
# 7

Here's an example. You can copy/paste this in to Query Analyzer to see how it works.

Declare @Temp
Table (part varchar(100), a varchar(20), b varchar(20), c varchar(20))

insert into @Temp(Part) Values('xxxxxxx-ddd-rrrrr')
insert into @Temp(Part) Values('aaaa-b-cccccccccccc')
insert into @Temp(Part) Values('eeee-w-fffffffff')


Update @Temp
Set A=ParseName(Replace(part, '-', '.'), 3),
B=ParseName(Replace(part, '-', '.'), 2),
C=ParseName(Replace(part, '-', '.'), 1)
Where len(part) - len(Replace(part, '-', '')) = 2

Select * From @Temp

mastros at 2007-8-30 > top of Msdn Tech,SQL Server,Transact-SQL...
# 8
Are you speaking language other than T-SQL?. You keeping talking with some thing T-SQL won't understand. Such like :

part = char(20)

People speak T-SQL here, please tune in. Here is the test of the code I give you before:

DECLARE @Part char(12)
DECLARE @hyphen char(1)

SET @part ='XXX-XX-XXXXXX'
SET @hyphen = '-'

--UPDATE [dbo].[parts] SET
SELECT
a = SUBSTRING(@part,1,CHARINDEX(@hyphen,@part)-1),
b = SUBSTRING(@part,CHARINDEX(@hyphen,@part)+1,CHARINDEX(@hyphen,RIGHT(@part,LEN(@part)-CHARINDEX(@hyphen,@part)))-1),
c = SUBSTRING(@part,CHARINDEX(@hyphen,RIGHT(@part,LEN(@part)-CHARINDEX(@hyphen,@part)))+CHARINDEX(@hyphen,@part)+1,LEN(@part))
GO

It does not put null in a, b and c. As long as column a, b and c in the table have right size, it should work. If they are all null, then the part is null. Print out part to make sure it is not null.

Aego at 2007-8-30 > top of Msdn Tech,SQL Server,Transact-SQL...
# 9

Thanks for your help. This worked!!

Brian

Butterflyangel02 at 2007-8-30 > top of Msdn Tech,SQL Server,Transact-SQL...

SQL Server

Site Classified