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 2000 Forums
 Transact-SQL (2000)
 Leading Zero's Function...?

Author  Topic 

dnf999
Constraint Violating Yak Guru

253 Posts

Posted - 2006-12-21 : 09:01:21
Hi I have values in a table which I want as a set Length (7 digits).

i.e

Value Value I want
------ ------------
78 0000078
980 0000980
3874 0003874
93049 0093049

Is there a function which I can use to do this, without writing a loop to add leading '0's...?

Thanks in Advance

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2006-12-21 : 09:07:36
SELECT
RIGHT('0000000' + LTRIM(RTRIM(STR(VALUE, 7))), 7)
FROM
YOURTABLE


Duane.
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-12-21 : 09:09:00
[code]select right('0000000' + cast(Value as varchar(7)),7)[/code]


Ditch, seems like you updated your boat!

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-12-21 : 09:13:40

1 If you use front end application, do format function there
eg in VB
Format(col,"0000000")

2 Select col,Replace(Str(col, 7), ' ', '0') from table


Madhivanan

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

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-12-21 : 10:12:36
What if Value column is not integer?

select Value, right('0000000' + Value, 7) from <yourcolumnnamehere>


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -