-1

I have the following value:

Jun **6** 2012 12:00:00:000AM

What I want is:

Jun **06** 2012 12:00:00:000AM

0 should only be prepended if the value of day is less than 9.

How do I do this?

Jay Sullivan
  • 17,332
  • 11
  • 62
  • 86

2 Answers2

4

If you're using SQL Server you can do:

DECLARE @dt datetime = '1/1/2012'
SELECT CONVERT(VARCHAR(26), @dt, 107) + ' ' + CONVERT(VARCHAR(12), @dt, 114)
Curtis
  • 101,612
  • 66
  • 270
  • 352
0

In any database, you can do something like:

(case when substring(val, 5, 1) = ' '
      then substring(val, 1, 4)||'0'+substring(val, 5, 1000)
      else val
 end)

This checks for the 5th character to see if it is a space. If so, then a zero needs to be added. I'm assuming the "**" are for emphasis an not in the string.

The problem is that the various string operators -- substring and concatenation -- tend to vary by database.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786