all groups > sql server programming > september 2006 >
You're in the

sql server programming

group:

Displaying time as 1:20 PM


Displaying time as 1:20 PM jonefer
9/9/2006 7:48:01 PM
sql server programming:
I managed to get my char text into a format that can now be sorted:
cast(convert(char(12),TimeOfAppt,8) as datetime) as Time

What i'm really trying to do is display it as:
1:20 PM

this must be easy?
Re: Displaying time as 1:20 PM Arnie Rowland
9/9/2006 8:04:34 PM
Here is one option:

SELECT stuff( ( right( convert( varchar(30), getdate(), 100 ), 7 )), 6, =
0, ' ' )


--=20
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.=20
Most experience comes from bad judgment.=20
- Anonymous


[quoted text, click to view]
Re: Displaying time as 1:20 PM jonefer
9/9/2006 8:50:02 PM
That doesn't sort as time - it sorts as string

[quoted text, click to view]
Re: Displaying time as 1:20 PM Stu
9/9/2006 9:18:37 PM
Your display doesn't have to match your sorting criteria; however, in
most cases, it's a good idea to allow your application (not your
database) to handle your formatting. However, if you are using a very
thin application layer, you could do something like (using Arnie's
example):

SELECT stuff( ( right( convert( varchar(30), datecolumn, 100 ), 7 )),
6, 0, ' ' )
FROM Table
ORDER BY datecolumn

Stu

SELECT
[quoted text, click to view]
Re: Displaying time as 1:20 PM jonefer
9/9/2006 10:07:01 PM
yep, that's what I ended up doing - however, I was using a 'SELECT DISTINCT'
which conflicts, so I ended up making a view that was grouped. Seems kind of
a-round-about way just to show 1:20 PM that can be sorted.

--- there must be an easier way to show the time in a way that can be sorted.

[quoted text, click to view]
Re: Displaying time as 1:20 PM Arnie Rowland
9/9/2006 11:39:20 PM
I was responding to your post where you wrote: "i'm really trying to do is
display it"

If you are concerned about ORDER BY behavior, that is something quite
different from DISPLAY behavior.

--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous


[quoted text, click to view]

Re: Displaying time as 1:20 PM Tibor Karaszi
9/10/2006 12:00:00 AM
None of the datatypes in SQL Server has any implied formatting. The client application formats the
value for you. If you want to define some particular format when the data leave SQL Server, you will
have to convert to a string, where things like ordering can be a problem.

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/


[quoted text, click to view]
Re: Displaying time as 1:20 PM Erland Sommarskog
9/10/2006 12:00:00 AM
jonefer (jonefer@discussions.microsoft.com) writes:
[quoted text, click to view]

Sure, display it in 24 hour format, and make sure there is a leading space
before 10 o'clock.

But the best is of course to leave format to the client. The client can
apply the regional settings and if the users prefers, he can see the
time displayed as 13:20.

If you really insist on formatting the time in SQL Server, 24-hour is the
only reasonable option. AM/PM can just lead to confusion for people who
are not accustomed to that notation. (Hey, even for people who are it,
ic can go wrong. I've seen more than one slide when I've been to conferences
in the US where it has said a.m. when it should have said p.m. or vice
versa. That would never happen here!)
--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: Displaying time as 1:20 PM Steve Kass
9/10/2006 8:49:02 PM
As others have suggested, time is time and strings are strings,
and you can't have a "time" that "shows" in a specific format -
it would be a string not a time.

One way to handle this is to be explicit about the fact that
time and strings are different, and perhaps this is what you
ultimately did:

select
datedisplay,
other_columns
from (
select distinct
datecolumn,
<messy string expression> as datedisplay,
other_columns
from T
) as T_plus_datedisplay
order by datecolumn

It's not really roundabout if you think of it as outputting
something different than what you order by.

-- Steve Kass
-- Drew University
-- http://www.stevekass.com


[quoted text, click to view]
AddThis Social Bookmark Button