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)
 Removing leading zeros, should not return 0

Author  Topic 

zeeshan13
Constraint Violating Yak Guru

347 Posts

Posted - 2009-10-07 : 08:25:23
Hi All,

I have a table called 'Table1' with the following field.

ProductID (varchar 50 length)

The records are like this. There is no particular length.


ProductID
0023456
87600
0000410
000
00000
0
12444

In my SELECT query I want to remove the leading zero's only. But the SELECT should never return a 0, instead a blank ''.

So for the above the SELECT should return the following.


ProductID_Modified
23456
87600
410



12444



How to achieve that in a SELECT query. For some specific reasons I do not want a CASE statement please.

Thanks for all the help.

Zee

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-10-07 : 08:55:24
select replace('#'+convert(varchar(10),convert(int,ProductID)),'#0',' ')


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-10-07 : 08:59:36
[code]select coalesce(convert(varchar(10), nullif(convert(int, ProductID), 0)), '')[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-10-07 : 09:07:56
OK - my solution isn't working proper!
I'm on the way with a "no java script" browser in the moment so I cannot delete my post.
Maybe later.

Sorry


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-10-07 : 10:04:56
quote:
Originally posted by zeeshan13

Hi All,

I have a table called 'Table1' with the following field.

ProductID (varchar 50 length)

The records are like this. There is no particular length.


ProductID
0023456
87600
0000410
000
00000
0
12444

In my SELECT query I want to remove the leading zero's only. But the SELECT should never return a 0, instead a blank ''.

So for the above the SELECT should return the following.


ProductID_Modified
23456
87600
410



12444



How to achieve that in a SELECT query. For some specific reasons I do not want a CASE statement please.

Thanks for all the help.

Zee




Where do you want to show the data?

Madhivanan

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

zeeshan13
Constraint Violating Yak Guru

347 Posts

Posted - 2009-10-07 : 10:12:58
I want to show an use the data at the back end within SQL Server. I do not use front end application.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-10-07 : 10:26:36
Ok. Another method

select SUBSTRING(productid,patindex('%[1-9]%',productid+'1'),LEN(productid)) from your_table

Madhivanan

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

- Advertisement -