Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Need help on the query.

Author  Topic 

NguyenL71
Posting Yak Master

228 Posts

Posted - 2009-01-14 : 18:52:38
I keep getting the error messages and cannot figure out why or someone has a better way to rewrite this query.
Thank you in advance.

Error:
.Net SqlClient Data Provider: Msg 536, Level 16, State 3, Line 1
Invalid length parameter passed to the substring function.

IF OBJECT_ID('Tempdb.dbo.##Staging', 'u') IS NOT NULL
DROP TABLE ##Staging
GO
CREATE TABLE ##Staging
(
Idx INT NULL,
Addr3 CHAR(20) NULL
)
go

insert ##Staging values (18614, 'SN BERNRDNO CA')
insert ##Staging values (33031, 'VICTORVILLE CA')
insert ##Staging values (18687, 'SAN DIEGO CA')
insert ##Staging values (96681, 'SAN DIEGO CA')
insert ##Staging values (68994, 'CARMICHAEL CA')
insert ##Staging values (163692,'SN BERNRDNO CA')
insert ##Staging values (178462, 'ESCONDIDO CA')
insert ##Staging values (189431, 'CHULA VISTA, CA')
insert ##Staging values (6846, 'BANNING , CA')
insert ##Staging values (11318, 'EL CAJON ,CA ')
go

SELECT *
FROM ##Staging
Go

-- Testing, not working...
SELECT idx, Addr3, SUBSTRING(Addr3, 1, (LEN(Addr3)- 2) )
FROM ##Staging
--ORDER BY idx ASC
go

-- Rule: return only city name.
-- Desire result.
Idx Addr3
----------- --------------------
18614 SN BERNRDNO
33031 VICTORVILLE
18687 SAN DIEGO
96681 SAN DIEGO
68994 CARMICHAEL
163692 SN BERNRDNO
178462 ESCONDIDO
189431 CHULA VISTA
6846 BANNING
11318 EL CAJON

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-14 : 18:56:42
Your example is not returning an error. Please provide sample data that will return the error you are getting. I'm sure it has to do with LEN(Addr3) - 2 being less than 0 for NULL or empty data, such as: insert ##Staging values (22222, '')

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

NguyenL71
Posting Yak Master

228 Posts

Posted - 2009-01-14 : 19:03:34
quote:
Originally posted by tkizer

Your example is not returning an error. Please provide sample data that will return the error you are getting. I'm sure it has to do with LEN(Addr3) - 2 being less than 0 for NULL or empty data, such as: insert ##Staging values (22222, '')

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog


Go to Top of Page

NguyenL71
Posting Yak Master

228 Posts

Posted - 2009-01-14 : 19:04:39
quote:
Originally posted by tkizer

Your example is not returning an error. Please provide sample data that will return the error you are getting. I'm sure it has to do with LEN(Addr3) - 2 being less than 0 for NULL or empty data, such as: insert ##Staging values (22222, '')

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog




Do you have a better way to rewrite this query. Any suggestions would greatly appreciate.
Go to Top of Page

Skorch
Constraint Violating Yak Guru

300 Posts

Posted - 2009-01-14 : 19:08:13
Very ugly but it works:
SELECT idx, Addr3, rtrim(ltrim(substring(rtrim(ltrim(replace(Addr3, ',',''))),1,(len(rtrim(ltrim(replace(Addr3, ',',''))))-2))))
FROM ##Staging
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-14 : 19:08:39
Does the Addr3 column allow NULLs? What type of data is returned by this:

SELECT Addr3
FROM YourTable
WHERE LEN(Addr3) - 2 < 0

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

NguyenL71
Posting Yak Master

228 Posts

Posted - 2009-01-14 : 19:17:59
quote:
Originally posted by tkizer

Does the Addr3 column allow NULLs? What type of data is returned by this:

SELECT Addr3
FROM YourTable
WHERE LEN(Addr3) - 2 < 0

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog




It return result below. I know why I got the error. Thanks.

Addr3 id
-------------------- -----------
18687
18614
33032
117183
152158
150745
116289
O 134117
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-14 : 19:21:43
You're welcome. To handle NULL data, you can use COALESCE/ISNULL functions. To handle empty data, use a CASE statement.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -