September 14, 2009 at 12:54 am
I want to get a date in the following format ex- 14-09-2009 should be returned in the format 14th September 2009. Can nay one tell me how?
September 14, 2009 at 1:13 am
Where do you want to convert the date format - SSRS or Database?
If the latter I strongly recommend against it. Date formatting should be done at the front end side (e.g. SSRS), especially, if the target format cannot be achieved using the standard CONVERT function.
In the scenario you described a concatenation of various CONVERT functions together with DATENAME function would be required.
September 14, 2009 at 1:42 am
In the front end can u tell me the logic how to do it
September 14, 2009 at 2:09 am
jsheikabdullah (9/14/2009)
In the front end can u tell me the logic how to do it
For what type of front end?
September 14, 2009 at 2:17 am
In vb.net
September 14, 2009 at 2:18 am
Hi,
try this
Declare @RESULT datetime
set @RESULT = getdate()
--
select (cast(day(@RESULT) as varchar)+
(case when (right(day(@RESULT),1) >= 4)
or(right(day(@RESULT),1)= 0)
or((left(day(@RESULT),1) = 1)and len(day(@RESULT))= 2) then 'th'
when right(day(@RESULT),1) = 1 then 'st'
when right(day(@RESULT),1) = 2 then 'nd'
when right(day(@RESULT),1) = 3 then 'rd'
end)+
space(1)+
datename(mm,@RESULT)+
space(1)+
cast(year(@RESULT) as varchar))
September 14, 2009 at 2:41 am
thanks it worked but shocking to know that the date format which is almost used in all forums for displaying is not supported by default in SQL.
September 14, 2009 at 3:41 am
Well the point is that SQL Server is the back-end and nicely formatting things is the job of the front-end. It's just not what SQL was designed for.
It's also beneficial in other ways to pass the data to the front end as the proper type (a date) rather than a string so you can re-sort it efficiently from the client end.
If you're using VB.net, it's pretty straight forward, example below:
Dim idate As Date
idate = "2009-01-01"
MsgBox(idate.ToString("dd MMMM yyyy"))
Adding the "th", "rd" etc isn't natively supported as far as I'm aware, but there are plenty of examples for building a vb.net function to do this on the web
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply