5

Is there a SQL format to remove leading zeros from the date?

Like if the date is 01/12/2015 to present it as 1/12/2015, and 01/01/2016 should be shown as 1/1/2016 etc

The entire date normally contains dd/MM/yyyy HH:mm:ss. I need to remove those redundant leading zeroes without changing the rest of information.

Currently I use query containing something like this:

convert(varchar, dateadd(hh, " + 2 + " , o.start_time), 103)) + ' '  
left(convert(varchar, dateadd(hh, " + 2 + " , o.start_time), 108), 110)  

I'm working with SQL Server 2008

5
  • 2
    What data type are you using to store that date? Commented Dec 30, 2015 at 18:01
  • 2
    if you are using this data in an application or report, it is better to do this formatting on the front-end and keep the data in its native format when reading from the database. As soon as you format it on the way out, it is just character data and can no longer be sorted or filtered as a date. Commented Dec 30, 2015 at 18:02
  • 1
    Have you tried "FORMAT" function... tried on sqlfiddle and this example worked and removed leading zeroes - " SELECT FORMAT ( dateadd(dd, -225, getdate()), 'd/m/y', 'en-US' )"
    – vmachan
    Commented Dec 30, 2015 at 18:08
  • I'm retrieving this data from DB to some local hashMap to compare with data retrieved from the web page. The web page presents the data in shortened way, the DB with pre-coming zeros. I prefer converting the Date after quiring the data from DB to hashMap and not to convert data received from the web page to dd/MM/yyyy format
    – Prophet
    Commented Dec 30, 2015 at 18:08
  • @vmachan: FORMAT is only available in SQL Server 2012 as a new feature - not available in SQL Server 2008 which is used here
    – marc_s
    Commented Dec 30, 2015 at 20:25

3 Answers 3

14

Try this, format is a much cleaner solution:

declare @date datetime = '01/01/2016'    
SELECT FORMAT(@date,'M/d/yyyy')

result: 1/1/2016

1
4

Not sure why you want to do this. Here is one way.

  • Use DAY and MONTH inbuilt date functions to extract day and month from date.
  • Both the function's return type is INT which will remove the unwanted leading zero
  • Then concatenate the values back to form the date

Try something like this

declare @date datetime = '01/01/2016'

select cast(day(@date) as varchar(2))+'/'+cast(month(@date) as varchar(2))+'/'+cast(year(@date) as varchar(4))

Result : 1/1/2016

Note: Always prefer to store date in date datatype

0
2

One way is to use datename() for the day and year:

select cast(month(o.start_time) as varchar(255)) + '/' + datename(day, o.start_time) + '/' + datename(year, o.start_time)

An alternative method uses replace():

select replace(replace(replace('@month/@day/@year', '@month', month(o.start_time)
                              ), '@day', day(o.start_time)
                      ), '@year', year(o.start_time)
              )

Personally, I see no use for this. I always put days in YYYY-MM-DD format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.