Solved

Date formatting

Posted on 2009-07-15
7
310 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:wrwilson
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:wrwilson
ID: 24863202
Oops, need and END at the end of above code
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now