Solved

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

Posted on 2009-05-05
12
786 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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

867 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

17 Experts available now in Live!

Get 1:1 Help Now