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
 SQL Server Development (2000)
 appendof string with 0

Author  Topic 

akpaga
Constraint Violating Yak Guru

331 Posts

Posted - 2008-10-08 : 17:46:11
i have field in a table that has data in the following format
1
34
567
2
45
298

what is need to do is append the field with 0's in the following manner in a select statement

if one digit ex: 0001
if two digits ex:0034
if three digits ex:0567

how can this be done?

thanks in advance

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-10-08 : 17:47:32
SELECT RIGHT('000' + Column1, 4)
FROM Table1

Depending upon your data type, you might need to convert/cast to varchar.

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

Subscribe to my blog
Go to Top of Page

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2008-10-08 : 17:52:09
[code]declare @temp table
(ID bigint IDENTITY(1,1) NOT NULL,
nums varchar(4)
);

INSERT INTO @TEMP (nums)
select 1
UNION ALL
select 34
UNION ALL
select 567
UNION ALL
select 2
UNION ALL
select 45
UNION ALL
select 298;

select nums from @TEMP;
select right('0000'+nums,4) as nums from @TEMP[/code]

RESULTS:
[code]
nums
----
1
34
567
2
45
298

(6 row(s) affected)

nums
----
0001
0034
0567
0002
0045
0298

(6 row(s) affected)[/code]
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-10-08 : 17:53:47
You got by five minutes.

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

Subscribe to my blog
Go to Top of Page

akpaga
Constraint Violating Yak Guru

331 Posts

Posted - 2008-10-08 : 18:02:03
Thanks ,it worked

quote:
Originally posted by tkizer

SELECT RIGHT('000' + Column1, 4)
FROM Table1

Depending upon your data type, you might need to convert/cast to varchar.

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

Subscribe to my blog


Go to Top of Page

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2008-10-08 : 18:23:35
quote:
Originally posted by tkizer

You got by five minutes.

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

Subscribe to my blog




slow forum.. what can i say
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-10-09 : 03:15:58
If you want to show formatted numbers in front end, use Format function there

Madhivanan

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

- Advertisement -