1

How to convert from 02/03/2012 to 2/3/2012 format in SQL Server

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Bernard Nongpoh
  • 1,028
  • 11
  • 20
  • 1
    just to be clear are you trying to remove the leading 0's from the day and month portion of the date? – Greg Dec 20 '12 at 05:47

4 Answers4

2

http://www.sql-server-helper.com/tips/date-formats.aspx

I think, this will be helpful...

EDIT: (Same Question)

In SQL Server, how to convert a date to a string in format M/D/YYYY? (no leading zeros, not MM/DD/YYYY)

Community
  • 1
  • 1
Adeel Ahmed
  • 1,591
  • 8
  • 10
1

if you're running SQL Server 2012, you can do this:

SELECT FORMAT ( @your_date_value, 'd', 'en-US' )

See http://msdn.microsoft.com/en-us/library/hh213505.aspx

shrisha
  • 445
  • 2
  • 7
1

And for your question answer

SELECT Convert(varchar, GETDATE(),1)  
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
A.Goutam
  • 3,422
  • 9
  • 42
  • 90
  • At least in sql 2005 this won't drop the leading 0 off the day and month which is what is being asked for – Greg Dec 20 '12 at 05:34
  • Hi Greg I think this may help http://www.sql-server-helper.com/tips/date-formats.aspx for user – A.Goutam Dec 20 '12 at 05:36
  • And i think this question is from SQL SERVER 2008 if i am right – A.Goutam Dec 20 '12 at 05:38
  • correct, it is adking about 2008 (I only had 2005 in front of me). Even in 2008 I think format 1 (and 101 and 111 that you suggested previously) will still give you leading zeros on day and month. The question is asking how to remove those zeros – Greg Dec 20 '12 at 05:47
0

This is going to get your exact results..

Declare @test datetime 

Set @test = '02/03/2012'

Print  Convert(VARCHAR(10) , @test,101 )

Print  REPLACE(LEFT(Convert(VARCHAR(10) , @test,101 ),5),0,'') + RIGHT(Convert(VARCHAR(10) , @test,101),5)

RESULT:

02/03/2012
2/3/2012
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user1583384
  • 230
  • 1
  • 2
  • 5
  • Welcome to StackOverflow: if you post code, XML or data samples, **please** highlight those lines in the text editor and click on the "code samples" button ( `{ }` ) on the editor toolbar to nicely format and syntax highlight it! – marc_s Dec 20 '12 at 06:52