Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Converting Military Time to Standard time


Converting Military Time to Standard time

Author
Message
Marv2011
Marv2011
Valued Member
Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)

Group: General Forum Members
Points: 57 Visits: 183
Hi,

I need to convert a datetime field from military time to standard time. I also need to do a datepart to get
just the hour from the time.

I tried using this but I am still getting a military time format.
convert(varchar, datefield, 8)

The results are : 15:30.00, 16:00:00.

I bascially need to get the hour stripped out so my results are like this: 9,10,11,12,1,2,3,4 instead of
9,10,11,12,13,14,

any suggestions
Thanks
Jack Corbett
  Jack Corbett
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11008 Visits: 14858
What do you want to do for midnight? Do you want a single 0 for the hour? If you want a 0 for midnight then this would work:

SELECT 
   CASE
      WHEN DATEPART(Hour, CURRENT_TIMESTAMP) > 12 THEN DATEPART(hour, CURRENT_TIMESTAMP) - 12
      ELSE DATEPART(Hour, CURRENT_TIMESTAMP)
   END AS the_time





Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Evil Kraig F
Evil Kraig F
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5681 Visits: 7660
What is the source datatype that you're extracting the information from? DATETIME?


- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Marv2011
Marv2011
Valued Member
Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)

Group: General Forum Members
Points: 57 Visits: 183
Yes, the field is a datetime field. 2009-07-15 17:30:00.000
sachnam
sachnam
SSC-Enthusiastic
SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)

Group: General Forum Members
Points: 159 Visits: 500
Marv2011 (5/6/2011)
Yes, the field is a datetime field. 2009-07-15 17:30:00.000


Try out this ..

create table #temp_table
( a datetime)
go
insert into #temp_table (a) values (getdate())
insert into #temp_table (a) values (getdate()+.10)
insert into #temp_table (a) values (getdate()+.09)
go
select Case when datepart(hour, a)>12 then datepart(hour,a) -12
else datepart(hour,a)
End as 'Standard Hour'
from #temp_table
go
drop table #temp_table

Regards,
Sachin
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