 |
| 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
|
|
|
|

May 6th, 2009, 10:47 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2006
Posts: 112
Thanks: 12
Thanked 0 Times in 0 Posts
|
|
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
|
|

May 6th, 2009, 11:47 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
|
|
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:
|
|
|

May 6th, 2009, 03:07 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2006
Posts: 112
Thanks: 12
Thanked 0 Times in 0 Posts
|
|
I can add this to my store procedure where i do my select statement right ???
|
|

May 6th, 2009, 05:01 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
|
|
Doesn't need to be that complicated
Quote:
Originally Posted by RinoDM
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:
|
|
|

May 6th, 2009, 05:10 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2006
Posts: 112
Thanks: 12
Thanked 0 Times in 0 Posts
|
|
works great...
thanks
|
|

May 6th, 2009, 05:29 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
|
|
Thanks for the feedback, Rino.
__________________
--Jeff Moden
|
|

May 6th, 2009, 05:47 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
|
|
Jeff,
Much better. Was not familiar with DATENAME.
Also, I figured there had to be a much easier way.
Thanks,
Richard
|
|

May 6th, 2009, 06:42 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
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.
|
|

May 6th, 2009, 07:00 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
|
|
Quote:
Originally Posted by rstelma
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
|
|

May 6th, 2009, 07:05 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
|
|
Quote:
Originally Posted by Old Pedant
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
|
|
 |