Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Need to show HH and "AM" or "PM' Expand / Collapse
Author
Message
Posted Tuesday, November 3, 2009 11:44 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, August 25, 2014 2:32 PM
Points: 206, Visits: 451
I've got a datetime field for call date but I need to group my data by the hour of that call date and display it grouped and show that date time value as "HH AM" or "HH PM".

Is there such a syntax or code avail somewhere that I can use?

thx,

John
Post #813126
Posted Tuesday, November 3, 2009 11:52 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Sunday, September 7, 2014 11:27 PM
Points: 7,164, Visits: 13,257
Would you mind sharing some sample data together with your expected result?

Please see the first link in my signature on how to post sample data.

There are several ways to do it, e.g. using dateadd() and datediff() to "normalize" your data on an hourly base.

One comment regarding the AM/PM format:
usually, date formatting should be done at the front end level. What's the reason for doing it at the database side?




Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Post #813132
Posted Tuesday, November 3, 2009 12:43 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:20 AM
Points: 12,916, Visits: 32,080
here's one way to do it, i'm sure you'll get other examples to choose from:
select CONVERT(varchar,datepart(hour,getdate()) % 12 ) + ' ' + CASE WHEN datepart(hour,getdate()) > =12 THEN 'PM' ELSE 'AM' END
--preceeding zero for HH?
select RIGHT('00' + CONVERT(varchar,datepart(hour,getdate()) % 12 ),2) + ' ' + CASE WHEN datepart(hour,getdate()) > =12 THEN 'PM' ELSE 'AM' END

--results: 2 PM
-- 02 PM



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #813158
Posted Wednesday, November 4, 2009 12:00 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 12:40 PM
Points: 141, Visits: 854
Another interesting way (don't know if I would call it a "good" way):
SELECT LEFT(RIGHT(CONVERT(CHAR(19),GETDATE(),100),7),2) + RIGHT(RIGHT(CONVERT(CHAR(19),GETDATE(),100),7),2)
-- BASED ON:
Select LTRIM(RIGHT(CONVERT(CHAR(19),GETDATE(),100),7))

edit:
I say "don't know if I would call it a "good" way" because I don't know if it will perform well. I'm sure other will suggest more efficient ways.
Post #813440
Posted Wednesday, November 4, 2009 11:22 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, August 25, 2014 2:32 PM
Points: 206, Visits: 451
I ended up using the code below and it works for what I'm trying to do. Thanks for your input and I've saved it as well.

Case When [Hour] > 12 Then convert(varchar(2),[Hour]-12) + ' PM' When [Hour] = 12 Then convert(varchar(2),[Hour]) + ' PM' When [Hour] < 12 Then convert(varchar(2),[Hour]) + ' AM' End,

In a subquery I'm pulling the field [Hour] as:

[Hour] = datepart(hh,a.CallDateEnded),


Regards,

John
Post #813754
Posted Wednesday, November 4, 2009 11:31 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:05 PM
Points: 23,397, Visits: 32,239
Could you post the full query you are using? There may be a better way do accomplish the task.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #813758
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse