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
 diplay with leading zeros as select statement

Author  Topic 

j4ydh
Starting Member

18 Posts

Posted - 2010-07-29 : 04:32:41
Hi

I am trying to create a select statement that takes the auto increment (ID field as int primary key) and is formated for display as a number with leading zeros and appending string info.

So increments of 100:
100
200
300 etc..

need to be displayed as follows:

a_000000000100.jpg
a_000000000200.jpg
a_000000000300.jpg

.... a_000000001400.jpg etc..

I am working on sql server management studio (2008) and everything is erroring and my eyes are starting to get bloodshot!

Any help would be appreciated

J




Devart
Posting Yak Master

102 Posts

Posted - 2010-07-29 : 04:50:56
Hello,

You can try this:

SELECT
'a_'+replicate('0',12-datalength(cast(<your_field_name> as varchar(12))))+
cast(<your_field_name> as varchar(12))+'.jpg'
FROM
<your_table_name>;

Best regards,

Devart Team
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-07-29 : 04:52:12
select
'a_' + right('000000000000' + convert(varchar(12),ID),12) + '.jpg'
from your_table


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

j4ydh
Starting Member

18 Posts

Posted - 2010-07-29 : 05:11:46
Hi,

I was hoping that I had made a school boy error, but unfortunately I wasn't even close!
Thank you Devart and webfred... spot on

Cheers
J
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-07-29 : 05:19:10
welcome


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

- Advertisement -