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
 General SQL Server Forums
 New to SQL Server Programming
 substring

Author  Topic 

a.ashabi
Posting Yak Master

117 Posts

Posted - 2009-12-03 : 16:46:33
Hi.I'd like to keep the first 6 digits of a feild.
I've wrote:

select SUBSTRING(product_oem, 1,len(product_oem)-6) as test from tbl_product

and gives me this error:
Invalid length parameter passed to the substring function.

I think its because some of the values of my field are less than 12 digits.

can u help me how can I say if the number of digits are 12 then
select the first 6 digits? or any other way which says just pick the first 6 character with no errors.
thanks in advanced

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-03 : 16:48:32
SELECT LEFT(product_oem, 6) AS test FROM tbl_product

or

select SUBSTRING(product_oem, 1, 6) as test from tbl_product

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

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-12-03 : 16:54:01
OR

SUBSTRING(product_oem+REPEAT(' ',6),1,6)

Never mind...long day


DECLARE @y varchar(6)
SELECT @y = '1234'
SELECT SUBSTRING(@y,1,6)




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-04 : 01:45:43
quote:
Originally posted by X002548

OR

SUBSTRING(product_oem+REPEAT(' ',6),1,6)

Never mind...long day


DECLARE @y varchar(6)
SELECT @y = '1234'
SELECT SUBSTRING(@y,1,6)




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam






REPEAT?
You forgot this is for SQL Server?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

a.ashabi
Posting Yak Master

117 Posts

Posted - 2009-12-07 : 15:08:05
thank u nso much for yr help
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-07 : 15:10:28
You're welcome.

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

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -