SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to get double digit day WITH YYYY-MM-DD??
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

phrankbooth
Posting Yak Master

USA
156 Posts

Posted - 05/20/2013 :  14:34:15  Show Profile  Reply with Quote

This:
SELECT SUBSTRING(convert(varchar, getdate(), 126),0,10)

Gives me:
2013-05-2

But I need:
2013-05-02

I had thought the DD would have automatically done that.


--PhB

MIK_2008
Flowing Fount of Yak Knowledge

Pakistan
1052 Posts

Posted - 05/20/2013 :  14:46:01  Show Profile  Reply with Quote
Are you looking for only date portion???

SELECT Convert(date,getdate())



P.S. I would suggest to handle formatting on the front end of the application (if you need it in any application). Else stick with the standard date type with in SQL.


Cheers
MIK
Go to Top of Page

phrankbooth
Posting Yak Master

USA
156 Posts

Posted - 05/20/2013 :  14:53:31  Show Profile  Reply with Quote
thanks for the quick reply!

There's no front end. These are internal calls.

--PhB
Go to Top of Page

jimf
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 05/20/2013 :  14:59:26  Show Profile  Reply with Quote
Start your substring at 1, not 0, and always declare the lengths of your varchars!
SELECT SUBSTRING(convert(varchar(10), getdate(), 126),1,10)

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

4613 Posts

Posted - 05/20/2013 :  15:49:44  Show Profile  Reply with Quote
SELECT FORMAT(SYSDATETIME(), 'yyyy-MM-dd', 'en-us')
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2202 Posts

Posted - 05/21/2013 :  00:49:59  Show Profile  Reply with Quote
--see this format.....( 2013-05-02)
DECLARE @date DATE = getdate()-19
SELECT CONVERT(VARCHAR(10), @date, 120) AS [YYYY-MM-DD]
SELECT REPLACE(CONVERT(VARCHAR(10), @date, 111), '/', '-') AS [YYYY-MM-DD]

--
Chandu
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22744 Posts

Posted - 05/22/2013 :  06:32:27  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
The FORMAT function is available from verrsion 2012 onwards. However if you use front end application to show this format, use format function there

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000