tSQL Time Format & Order

How do you order by time in tSQL?

SELECT *
FROM BROADCASTS_TBL
ORDER BY time(broadcast_time)

broadcast_time field values examples:

4:30 PM EST
11:00 AM EDT
5:30 PM CST

Please provide an example of how this can be ordered in tSQL.

Thanks for the help!

Terry
LVL 1
splendorxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sigmaconCommented:
What's the data type of these columns? If it's not datetime or smalldatetime, do:

SELECT *
FROM BROADCASTS_TBL
ORDER BY cast(broadcast_time as datetime)
0
sigmaconCommented:
That's going to break with the time zone statements ... I have to give that more thought.
0
Saqib KhanSenior DeveloperCommented:
Maybe this
SELECT *
FROM BROADCASTS_TBL
ORDER BY Convert(dateTime, SubString(broadcast_time, 1, LEN(broadcast_time)-3))
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

sigmaconCommented:
In order to get the (US) time zone format to be applied, you may need to do something like this:

order by
    case upper(ltrim(right(rtrim(broadcast_time), 4)))
        when 'AKST' then cast(left(broadcast_time, charindex('AKST', broadcast_time) - 1) as datetime) - 9.0 / 24
        when 'AST' then cast(left(broadcast_time, charindex('AST', broadcast_time) - 1) as datetime) - 4.0 / 24
        when 'CST' then cast(left(broadcast_time, charindex('CST', broadcast_time) - 1) as datetime) - 6.0 / 24
        when 'EST' then cast(left(broadcast_time, charindex('EST', broadcast_time) - 1) as datetime) - 5.0 / 24
        when 'HST' then cast(left(broadcast_time, charindex('HST', broadcast_time) - 1) as datetime) - 10.0 / 24
        when 'MST' then cast(left(broadcast_time, charindex('MST', broadcast_time) - 1) as datetime) - 7.0 / 24
        when 'PST' then cast(left(broadcast_time, charindex('PST', broadcast_time) - 1) as datetime) - 8.0 / 24
        when 'AKDT' then cast(left(broadcast_time, charindex('AKDT', broadcast_time) - 1) as datetime) - 8.0 / 24
        when 'ADT' then cast(left(broadcast_time, charindex('ADT', broadcast_time) - 1) as datetime) - 3.0 / 24
        when 'CDT' then cast(left(broadcast_time, charindex('CDT', broadcast_time) - 1) as datetime) - 5.0 / 24
        when 'EDT' then cast(left(broadcast_time, charindex('EDT', broadcast_time) - 1) as datetime) - 4.0 / 24
        when 'PDT' then cast(left(broadcast_time, charindex('PDT', broadcast_time) - 1) as datetime) - 7.0 / 24
        when 'MDT' then cast(left(broadcast_time, charindex('MDT', broadcast_time) - 1) as datetime) - 6.0 / 24
    end orderCol

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ShogunWadeCommented:
maybee i am missing the point but surely

SELECT * FROM BROADCASTS_TBL
 order by broadcast_time

assuming broadcast_time  is a datetime datatype.
0
sigmaconCommented:
Shogun, that's why I asked whether the column type is datetime ... ;-) ... but we haven't gotten an answer. Based on the data posted, I believe it's some character type, because datetime does not store time zone information as shown in the sample output - ASFAIK
0
ShogunWadeCommented:
sorry sig,   didnt read your reply properly.  (i'll slap my wrist ;) )
0
splendorxAuthor Commented:
Sorry, the datatype is: nvarchar.
0
sigmaconCommented:
Well, splendorx, did any of our answers help so far?
0
splendorxAuthor Commented:
Thanks for everyone's input.  sigmacon thanks for a great solution!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.