Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to get this date?

Author  Topic 

tantcu
Yak Posting Veteran

58 Posts

Posted - 2012-06-29 : 14:36:35
I tried to get this date: 2009-06-01

I used the code : CONVERT(CHAR(8), DATEADD(YY, -3, getdate()),112)

The result is 20090629.

Is there anyone know how to fix?

Thanks

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-06-29 : 14:45:08
This type of formatting shouldn't be done in SQL Server. It should be done in your application.

To answer your question though, use char(10) with style 120: http://msdn.microsoft.com/en-us/library/ms187928.aspx

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

sshelper
Posting Yak Master

216 Posts

Posted - 2012-06-30 : 03:25:51
Here's a couple of links that shows how to format a DATETIME value to the different date formats:

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

SQL Server Helper
http://www.sql-server-helper.com/tips/tip-of-the-day.aspx
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-01 : 01:02:03
quote:
Originally posted by tantcu

I tried to get this date: 2009-06-01

I used the code : CONVERT(CHAR(8), DATEADD(YY, -3, getdate()),112)

The result is 20090629.

Is there anyone know how to fix?

Thanks


is database field type date or varchar?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-07-02 : 04:09:24
Start with this


select dateadd(year, -3, getdate()-day(getdate())+1)

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -