Solved

Date formatting

Posted on 2009-07-15
7
339 Views
Last Modified: 2012-05-07
I have a date field @Date that currently dislays '07/01/2009'.  What statement can I use in my query to format it it like '7/01/2009'?........m/dd/yyyy?

0
Comment
Question by:mattkovo
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 50 total points
ID: 24862882
CONVERT(varchar, datecolumn, 101)
0
 
LVL 7

Expert Comment

by:Randy Wilson
ID: 24863194
There is no simple T-SQL way to do what you want, the CONVERT will not give you m/dd/yyyy.  
The code below will give you m/d/yyyy.    I have this is a function, because you always seem to need it.  You could edit it to do m/dd/yyyy

CREATE FUNCTION [dbo].[fn_ShortDate]
      (@Date smalldatetime)

RETURNS varchar(12)

AS
BEGIN
DECLARE @String varchar(12)
SET @String = CAST(DATEPART(M,@Date) AS VARCHAR(2)) + '/' + CAST(DATEPART(D,@Date) AS VARCHAR(2)) +
'/' + CAST(DATEPART(YY,@Date) AS CHAR(4))

RETURN(@String)
0
 
LVL 7

Expert Comment

by:Randy Wilson
ID: 24863202
Oops, need and END at the end of above code
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24865503
>>What statement can I use in my query to format it it like '7/01/2009'?........m/dd/yyyy?<<
This is a presentation not a data problem.  You should be using .NET for that.  

0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24875683
use DATE_FORMAT(date,format)

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
SET @Date = DATE_FORMAT(@Date,'%c/%d/%Y')

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24880763
NerdsOfTech,

I am not sure if you noticed, but the author is using MS SQL Server.
0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24881467
Oops thanks ac
SET @Date = CONVERT(VARCHAR(10), @DATE, 111) AS [YYYY/MM/DD]

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question