locked
Is it possible to format date without leading zeros? RRS feed

  • Question

  • Using CONVERT with style 101 causes date to look like "04/07/1010"

    Is there a way to make it look like "4/7/1010"?

    Wednesday, April 7, 2010 5:43 PM

Answers

  • declare @dt datetime
    set @dt='04/07/2010'
    
    select replace(left(convert(varchar, @dt, 101),5), '0', '')+right(convert(varchar,@dt,101), 5)

    http://www.t-sql.ru
    • Marked as answer by JoeSchmoe115 Wednesday, April 7, 2010 5:51 PM
    Wednesday, April 7, 2010 5:49 PM

All replies

  • declare @dt datetime
    set @dt='04/07/2010'
    
    select replace(left(convert(varchar, @dt, 101),5), '0', '')+right(convert(varchar,@dt,101), 5)

    http://www.t-sql.ru
    • Marked as answer by JoeSchmoe115 Wednesday, April 7, 2010 5:51 PM
    Wednesday, April 7, 2010 5:49 PM
  • declare
     @dt datetime
    
    set
     @dt='04/07/2010'
    
    
    select
     replace
    (left
    (convert
    (varchar
    , @dt, 101),5), '0'
    , ''
    )+right
    (convert
    (varchar
    ,@dt,101), 5)
    

    http://www.t-sql.ru

    Yeah, this will work but I hoped there is some style I missed
    Wednesday, April 7, 2010 5:52 PM
  • Sorry,

     

    declare @dt datetime
    set @dt='04/20/2010'
    
    select replace(left(convert(varchar, @dt, 101),2), '0', '')+
    replace(right(convert(varchar,@dt,101), 8),'/0','/')

    http://www.t-sql.ru
    Wednesday, April 7, 2010 5:55 PM
  • You could also do this:

    DECLARE @dt datetime
    SET @dt='20100407'
    SELECT LTRIM(STR(MONTH(@dt)))+'/'+LTRIM(STR(DAY(@dt)))+'/'+STR(YEAR(@dt),4)

     


    --Brad (My Blog)
    • Proposed as answer by Naomi N Wednesday, April 7, 2010 6:06 PM
    Wednesday, April 7, 2010 6:00 PM
  • I think replace would not work here. You may want to use Brad's suggestion instead.
    Premature optimization is the root of all evil in programming. (c) by Donald Knuth

    Naomi Nosonovsky, Sr. Programmer-Analyst

    My blog
    Wednesday, April 7, 2010 6:06 PM
  • SELECT ltrim(MONTH(@dt))+'/'+ltrim(day(@dt))+'/'+ltrim(year(@dt))
    Wednesday, April 7, 2010 6:25 PM
  • Obviously this is an old question, but you can also do the following:
    convert(varchar,DATEPART(MM, GETDATE()))+'/'+convert(varchar,DATEPART(DD, getdate()))+'/'+convert(varchar,DATEPART(YYYY, GETDATE()))

    replace

    getDate()

    with your date field. This will yield the date in a varChar() string without the leading zeroes.

    Tuesday, July 26, 2016 6:59 PM