1

I am having a little difficulty with the below code - it return the value I am after, but I need the value 3 digits long - i.e. it returns '1' but I need '001' - any help would be gratefully received

select convert(varchar(3),(select count(ptMatter) + 1 from lamatter where  
 convert(varchar(10), getdate(), 103)=convert(varchar(10), dateadd, 103)))
HaskellElephant
  • 9,819
  • 4
  • 38
  • 67
James Parish
  • 327
  • 1
  • 6
  • 15
  • 4
    Are you trying to repeat the integer three times (question title), or add leading zeros (question body)? Your question is contradictory. – Delan Azabani Mar 01 '11 at 12:46
  • 1
    possible duplicate of [Most efficient T-SQL way to pad a varchar on the left to a certain length?](http://stackoverflow.com/questions/121864/most-efficient-t-sql-way-to-pad-a-varchar-on-the-left-to-a-certain-length) – Gabe Mar 01 '11 at 12:47

3 Answers3

4
Select Right('000000' + convert(varchar(3), Result),3)
From yourTable

for your exact query:

Select Right('000000' + convert(varchar(3), (select count(ptMatter) + 1 from lamatter where convert(varchar(10), getdate(), 103)=convert(varchar(10), dateadd, 103)) ),3)
codingbadger
  • 42,678
  • 13
  • 95
  • 110
0

Look at your database documentation if the lpad function exists. For example, this is the mysql version : http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_lpad

edit: sorry, didn't saw the tsql tag... Like said in another answer, the right function is the way to go, like explained here : Most efficient T-SQL way to pad a varchar on the left to a certain length?

Community
  • 1
  • 1
krtek
  • 26,334
  • 5
  • 56
  • 84
0

What about something like this:

DECLARE @i int;
SELECT @i = 1;
SELECT REPLACE(STR(@i, 3), ' ', '0')
Frank Kalis
  • 1,322
  • 9
  • 8