SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need to show HH and "AM" or "PM'


Need to show HH and "AM" or "PM'

Author
Message
latingntlman
latingntlman
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1050 Visits: 521
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
LutzM
LutzM
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23575 Visits: 13559
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
Lowell
Lowell
SSC Guru
SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)

Group: General Forum Members
Points: 71332 Visits: 40930
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
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
diamondgm
diamondgm
Mr or Mrs. 500
Mr or Mrs. 500 (575 reputation)Mr or Mrs. 500 (575 reputation)Mr or Mrs. 500 (575 reputation)Mr or Mrs. 500 (575 reputation)Mr or Mrs. 500 (575 reputation)Mr or Mrs. 500 (575 reputation)Mr or Mrs. 500 (575 reputation)Mr or Mrs. 500 (575 reputation)

Group: General Forum Members
Points: 575 Visits: 950
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.
latingntlman
latingntlman
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1050 Visits: 521
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
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)

Group: General Forum Members
Points: 94393 Visits: 38956
Could you post the full query you are using? There may be a better way do accomplish the task.

Cool
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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search