3

I would like to have a fix number of characters, let say 10 (xxxxxxxxxx) and input may be some varchar let say (abcd). Wanted output in that case is xxxxxxabcd.

Is there any elegant way to avoid looping for 10 - len(abcd)?

edit:

This questiom may be a duplicate if it's observed through accepted answer but not through a way of asking a question. Otherwise I would be able to find existing question and wouldn't create a duplicate.

dllhell
  • 1,987
  • 3
  • 34
  • 51
  • Does the duplicate help you or not? – Daniel Hilgarth Feb 20 '13 at 07:09
  • Answers under question - yes but how should I reach them? Question is asked on inapropriate way for my problem and answers are digged up trough questions. Implication is that a structure of question is very important to find an answer. Anyway, thanks again to @mellamokb the Wise for helping me. – dllhell Feb 20 '13 at 07:20
  • The question is formulated very clearly. You want to pad a string on the left. That's what the quesion asks. *Your* question is formulated in a way that only describes the problem in a convoluted way without using the proper terminology, so it's the fault of your question that you were unable to find the existing one. It's not the existing question being at fault here... – Daniel Hilgarth Feb 20 '13 at 07:22
  • I used my right to defend my opinion so I did, you are disagreeing and I don't see any space for further discussion. Techincally yes, you are right. – dllhell Feb 20 '13 at 07:25

2 Answers2

4

The trick is to prepend the fixed pattern with the input, and then grab the right-most n characters:

RIGHT('xxxxxxxxxx' + 'abcd', 10)
mellamokb
  • 56,094
  • 12
  • 110
  • 136
0

I've done something like this with SQL Server in the past. Hope it helps.

select SUBSTRING('000000', 1,6-LEN('ABCD')) + 'ABCD'
TEEKAY
  • 1,156
  • 1
  • 10
  • 25