[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

How to format a DATETIME field in yyyy-mm-dd without dropping any leading zeros?

How can I format a DATETIME field in yyyy-mm-dd without dropping any leading zeros?
0
rocketTendon
Asked:
rocketTendon
  • 2
  • 2
1 Solution
 
rafranciscoCommented:
Try this:

SELECT CONVERT(VARCHAR(10), YourDate, 111)
0
 
rafranciscoCommented:
Another way:

SELECT CAST(YEAR(YourDate) AS VARCHAR(4)) + '-' + RIGHT('00' + CAST(MONTH(YourDate) AS VARCHAR(2)), 2) + '-'  + RIGHT('00' + CAST(DAY(YourDate) AS VARCHAR(2)), 2)
0
 
rocketTendonAuthor Commented:
Thanks...

Used the following:

replace(CONVERT(VARCHAR(10), [Sale Start], 111),'/','-')
0
 
Anthony PerkinsCommented:
I suspect you will find the following more efficient (no need for the REPLACE):
Select CONVERT(VARCHAR(10), [Sale Start], 120)
0
 
rocketTendonAuthor Commented:
Will do.. thanks acp.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now