Main menu

Microsoft SQL string padding technique

Published by dnovikov on Tue, 11/29/2011 - 12:08

Microsoft SQL has no functions for padding strings. This functionality can be only simulated. E.g., let's try to pad month number with leading zeroes:


SET @DT = '2003-02-02 10:31 PM'

SELECT RIGHT('0' + CAST(DATEPART(month, @DT) AS nvarchar), 2)

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.