Solved

Format date in SQL query like (Month Day, Year)

Posted on 2009-05-05
12
785 Views
Last Modified: 2012-05-06
I am sure I must be just missing something, but what is the simplest way to output a datetime field to look like this:

May 5, 2009

when currently the field value is equal to this:

2009-05-05 13:05:03.530

It doesn't have to appear in the database in that format, just in the output of select statement.

Thanks!
0
Comment
Question by:hyphenpipe
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 250 total points
ID: 24306821
select convert(varchar(10), getdate(), 101)
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24306828
from a table:

select convert(varchar(10), FIELDNAME, 101)
from tablename
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24306841
swap out 101 for 100...
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24306844
select convert(varchar(10), FIELDNAME, 100)
from tablename
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24306851
Also:
SELECT CONVERT(VARCHAR, GETDATE(), 107)

See reference:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24306913
this is all about datetime conversion game, you can see all available date time in SQL Server with very small script, have a look at my small article.

http://www.sqlhub.com/2009/04/list-of-all-available-datetime-format.html
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 6

Author Comment

by:hyphenpipe
ID: 24307646
I apologize for my error.  Although these are close (I am already doing something similar), I should have used a better example other than the month of May.

Is it possible to get the full month string as part of the date, i.e. December 7, 1941 rather than Dec 7 1941 without a case statement?
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24307691
Not too easily...however,  Iwould suggest using a dates table, as I use in this article

http://blogs.techrepublic.com.com/datacenter/?p=326
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24307711
Try like this:
DECLARE @date DATETIME

SET @date = GETDATE()-28;
 

SELECT DATENAME(month, @date) + ' ' + SUBSTRING(CONVERT(VARCHAR, @date, 107), 5, 8)

Open in new window

0
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 250 total points
ID: 24307717
Or use the original space from conversion in 107.
SELECT DATENAME(month, @date) + SUBSTRING(CONVERT(VARCHAR, @date, 107), 4, 8)

Open in new window

0
 
LVL 6

Author Closing Comment

by:hyphenpipe
ID: 31578110
I awarded 250 to chapmandew and 250 to mwvisa1.  I hope this is satisfactory.  Thanks for the help!
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24307998
hyphenpipe,

Glad to help.

Regards,
Kevin

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
combine data in sql 1 29
SQL - sum months, quarter, YTD 9 81
SQL Server Select on LT running Totals 6 54
Stored Procedure error 45 40
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…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

16 Experts available now in Live!

Get 1:1 Help Now