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 double digit day WITH YYYY-MM-DD??

Author  Topic 

phrankbooth
Posting Yak Master

162 Posts

Posted - 2013-05-20 : 14:34:15

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
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2013-05-20 : 14:46:01
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

162 Posts

Posted - 2013-05-20 : 14:53:31
thanks for the quick reply!

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

--PhB
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2013-05-20 : 14:59:26
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
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-05-20 : 15:49:44
[code]SELECT FORMAT(SYSDATETIME(), 'yyyy-MM-dd', 'en-us')[/code]
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-05-21 : 00:49:59
--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

22864 Posts

Posted - 2013-05-22 : 06:32:27
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
   

- Advertisement -