Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 6th, 2009, 10:47 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 112
Thanks: 12
Thanked 0 Times in 0 Posts
Default Display date format???

Hi to all...

In a select statement is there a way i can display the date format as

Wednesday May 6, 2009

Thanking you in advance

Rino
 
Old May 6th, 2009, 11:47 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Hi,

You can use this User Defined Function to get the results you desire.

CREATE FUNCTION getLongDate(@InputDate Datetime)
RETURNS Varchar(30)
AS
BEGIN

RETURN

CASE WHEN DATEPART(dw,@InputDate) = 1 Then 'Sunday' WHEN DATEPART(dw,@InputDate) = 2 Then 'Monday'
WHEN DATEPART(dw,@InputDate) = 3 Then 'Tuesday'
WHEN DATEPART(dw,@InputDate) = 4 Then 'Wednesday'
WHEN DATEPART(dw,@InputDate) = 5 Then 'Thursday'
WHEN DATEPART(dw,@InputDate) = 6 Then 'Friday'
WHEN DATEPART(dw,@InputDate) = 7 Then 'Saturday'
END + ' ' +
CASE
WHEN CONVERT(Varchar, DATEPART(mm, @InputDate)) = 1 Then 'January'
WHEN CONVERT(Varchar, DATEPART(mm, @InputDate)) = 2 Then 'February'
WHEN CONVERT(Varchar, DATEPART(mm, @InputDate)) = 3 Then 'March'
WHEN CONVERT(Varchar, DATEPART(mm, @InputDate)) = 4 Then 'April'
WHEN CONVERT(Varchar, DATEPART(mm, @InputDate)) = 5 Then 'May'
WHEN CONVERT(Varchar, DATEPART(mm, @InputDate)) = 6 Then 'June'
WHEN CONVERT(Varchar, DATEPART(mm, @InputDate)) = 7 Then 'July'
WHEN CONVERT(Varchar, DATEPART(mm, @InputDate)) = 8 Then 'August'
WHEN CONVERT(Varchar, DATEPART(mm, @InputDate)) = 9 Then 'September'
WHEN CONVERT(Varchar, DATEPART(mm, @InputDate)) = 10 Then 'October'
WHEN CONVERT(Varchar, DATEPART(mm, @InputDate)) = 11 Then 'November'
WHEN CONVERT(Varchar, DATEPART(mm, @InputDate)) = 12 Then 'December'
END + ' ' +
CONVERT(Varchar,DATEPART(dd, @InputDate), 0) + ', ' + CONVERT(Char(4), DATEPART(yy, @InputDate))

END

SELECT dbo.getLongDate([Date Column]) AS TheLongDate FROM [Some Table]
The Following User Says Thank You to rstelma For This Useful Post:
RinoDM (May 6th, 2009)
 
Old May 6th, 2009, 03:07 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 112
Thanks: 12
Thanked 0 Times in 0 Posts
Default

I can add this to my store procedure where i do my select statement right ???
 
Old May 6th, 2009, 05:01 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default Doesn't need to be that complicated

Quote:
Originally Posted by RinoDM View Post
I can add this to my store procedure where i do my select statement right ???
Yeah... but it doesn't need to be nearly that complicated....

Code:
 SELECT DATENAME(dw,GETDATE())+' '
      + DATENAME(mm,GETDATE())+' '
      + DATENAME(dd,GETDATE())+', '
      + DATENAME(yy,GETDATE())
__________________
--Jeff Moden
The Following User Says Thank You to Jeff Moden For This Useful Post:
RinoDM (May 6th, 2009)
 
Old May 6th, 2009, 05:10 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 112
Thanks: 12
Thanked 0 Times in 0 Posts
Default

works great...
thanks
 
Old May 6th, 2009, 05:29 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Thanks for the feedback, Rino.
__________________
--Jeff Moden
 
Old May 6th, 2009, 05:47 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Jeff,

Much better. Was not familiar with DATENAME.

Also, I figured there had to be a much easier way.

Thanks,
Richard
 
Old May 6th, 2009, 06:42 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

For future reference...

Even if you were forced into a CASE...WHEN, the form used by RSTELMA is not the optimum choice.

Much better than
Code:
CASE WHEN DATEPART(dw,@InputDate) = 1 Then 'Sunday' WHEN DATEPART(dw,@InputDate) = 2 Then 'Monday'
 WHEN DATEPART(dw,@InputDate) = 3 Then 'Tuesday'
 WHEN DATEPART(dw,@InputDate) = 4 Then 'Wednesday'
 WHEN DATEPART(dw,@InputDate) = 5 Then 'Thursday'
 WHEN DATEPART(dw,@InputDate) = 6 Then 'Friday'
 WHEN DATEPART(dw,@InputDate) = 7 Then 'Saturday'
END
would be this:
Code:
CASE DATEPART(dw,@InputDate) 
        WHEN 1 THEN 'Sunday'
        WHEN 2 THEN 'Monday'
        WHEN 3 THEN 'Tuesday'
        WHEN 4 THEN 'Wednesday'
        WHEN 5 THEN 'Thursday'
        WHEN 6 THEN 'Friday'
        WHEN 7 THEN 'Saturday'
END
Do *NOT* repeat the expression when it doesn't change throughout the CASE. Use this alternate form, instead. It will look pretty familiar to anybody who has used VB or even switch( ) in many other languages.
 
Old May 6th, 2009, 07:00 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Quote:
Originally Posted by rstelma View Post
Jeff,

Much better. Was not familiar with DATENAME.

Also, I figured there had to be a much easier way.

Thanks,
Richard
Absolutely my pleasure, Richard. Thank you for the feedback.
__________________
--Jeff Moden
 
Old May 6th, 2009, 07:05 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Quote:
Originally Posted by Old Pedant View Post
For future reference...

Even if you were forced into a CASE...WHEN, the form used by RSTELMA is not the optimum choice.
I agree although I've never tested it for actual performance. It would be interesting to test on a million row table.
__________________
--Jeff Moden





Similar Threads
Thread Thread Starter Forum Replies Last Post
date format differs, need to force format somehow patricolsson ASP.NET 2.0 Basics 1 December 3rd, 2009 12:53 AM
Convert date to JDEdwards date format snufse ASP.NET 2.0 Basics 1 March 24th, 2009 08:54 AM
Convert British format date to American format? fyr PHP How-To 0 December 19th, 2007 03:17 PM
How to give Date format while entering date Subuana Beginning VB 6 4 March 17th, 2006 07:25 AM
date format differs, need to force format somehow patricolsson HTML Code Clinic 2 January 12th, 2006 05:55 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.