I have a column in a database set as a DATETIME datatype, when I select it, I want to return it as:
mm/dd/yyyy hh:mm am or pm.
How in the world can I do this? I looked at the function CONVERT() and it doesnt seem to have this format as a valid type. This is causing me to lose my hair, in MySQL it is just so much easier. .
At any rate, currently when I select the value without any convert() it returns as:
June 1 2007 12:23AM
Which is close, but I want it as:
06/01/2007 12:23AM
Thanks!
Hi,
Normally you would be formatting your date on the UI or Report side, which means you must let your program display it correctly or let your reporting engine format your date as you want to.
But if you really want to change the format of your date, you can do this by changing the type to string by using the CONVERT function. The closest that I can come up with is this format:
mm/dd/yyyy
checki it here (code 101):
http://msdn2.microsoft.com/en-us/library/ms187928.aspx
the syntax would be like this in SQL
SELECT CONVERT('your date', nvarchar(MAX), 101)
Maybe you can mix it up with code 108 so that you can concatenate the time with it.
cheers,
Paul June A. Domag
|||The reason I am formatting it in SQL is this is inside a trigger written in pure SQL which emails from the DB.
Yeah CONVERT() using type 101 was the closest I could get as well, but it only displays date, no time. I need both date and time in the format I specified above.
This is absolutley stupid that they did it this way, they should take a lession from MySQL which allows you to format a DATETIME in any fashion via strings such as %m/%d/%Y etc, etc.
Anybody have other ideas?
|||Hi,
Why not just combine the codes 101 and 108 and maybe manually parse it using substring?
You can create a scalar function to make it reusable.
Code Snippet
DECLARE @.dt VARCHAR(MAX)
SELECT @.dt = CONVERT(nvarchar(MAX), GETDATE(), 101) + ' ' + CONVERT(nvarchar(MAX), GETDATE(), 108)
-- After this just use Substring to satisfy your formatting
note: varchar(max) is only available in SQL2005. specify the lenght if your using SQL2000
cheers,
Paul June A. Domag
|||As Paul indicated, formating is normally left to the client application. I suspect the developer you are working with either does not know how to properly format for display in his/her application, or is too lazy and is passing the responsibility off to the database.
This expression should provide the date in the form you desire. Replace the [ @.MyDate ] with your column or date value. You can easily create your own function that will do this for you so that you can re-use this expression.
DECLARE @.MyDate datetime
SET @.MyDate = '2007/07/21 11:35:45.255PM'
SELECT MyDate =
convert( varchar(10), @.MyDate, 101) +
stuff( right( convert( varchar(26), @.MyDate, 109 ), 15 ), 7, 7, ' ' )
MyDate
-
07/21/2007 11:35 PM
Arnie Rowland ,
You are the man, that worked like a charm. I guess my problem with the convert function is that there is no predefined format of:
mm/dd/yyyy hh:mm am/pm
I would assume this is very very popular, so I am confused as to why it is not implemented. As far as this benig done on the front end, this has to be done at the DB level, since we send emails out via the database.
|||I have to say that I wouldn't want to send an email from a trigger that required any kind of special formatting. Perhaps an alert to a sysadmin, but if I was going to send correspondence like that, I would put my information in a queue of some sort and have a tool to send the email.
the CONVERT thing is a mess because it doesn't give you enough formats, unlike a proper data presentation layer would. I have (in the past) used datePart to build up a date formatter of my own, or you could probably do it with the CLR quite nicely. But SQL Server should be used to manage and manipulate data, not format it (as a broad rule of course. We all do it from time to time to appease a user/manager/programmer etc, so don't think I am saying it is horrible, it just isn't as ideal as using a programming tool made to do such things.)
No comments:
Post a Comment