473,837 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help sorting time values

I have been trying and trying to properly sort records through a mySQL
query. I have hour, minute and AM/PM values stored separately in a
database as varchars. I'm trying to concat those, cast them to a time
value and sort them. I have found that it sorts some properly but other
it does not.

The following query:
CAST(CONCAT(hou r, ':', minute, ' ', ampm) AS TIME) AS myTime
ORDER BY myTime

Sorts some results like this:
10:30 PM
8:30 PM
9:30 PM

But other are perfect:
10:00 AM
11:00 AM
12:00 PM
1:00 PM
2:00 PM
3:00 PM
4:00 PM
5:00 PM

These exact results we part of the same recordset. Can anyone help me
with this? It would be greatly, greatly appreciated.

Thanks in advance!

Jul 23 '05 #1
3 2659
pa******@hotmai l.com wrote:
I have been trying and trying to properly sort records through a mySQL
query. I have hour, minute and AM/PM values stored separately in a
database as varchars. I'm trying to concat those, cast them to a time
value and sort them. I have found that it sorts some properly but other
it does not.

The following query:
CAST(CONCAT(hou r, ':', minute, ' ', ampm) AS TIME) AS myTime
ORDER BY myTime

Sorts some results like this:
10:30 PM
8:30 PM
9:30 PM

But other are perfect:
10:00 AM
11:00 AM
12:00 PM
1:00 PM
2:00 PM
3:00 PM
4:00 PM
5:00 PM

These exact results we part of the same recordset. Can anyone help me
with this? It would be greatly, greatly appreciated.

Thanks in advance!


Your problem is that either your data is being stored without
leading blanks or that your code is eliminating them. You can
determine if the data is stored with/without leading blanks
using a simple select that encloses the data in quotation marks.

If the output looks like "8" your problem is that data is being
stored without leading blanks.

If it looks like " 8" your code is wiping out the leading blanks.

HTH
Jerry
Jul 23 '05 #2
Thanks for the response Jerry. I'm a bit confused. Do I want the
spaces? Shouldn't the cast take care of those? Should I be trimming the
values?

Jul 23 '05 #3
pa******@hotmai l.com wrote:
I have been trying and trying to properly sort records through a mySQL
query. I have hour, minute and AM/PM values stored separately in a
database as varchars. I'm trying to concat those, cast them to a time
value and sort them. I have found that it sorts some properly but other
it does not.

The following query:
CAST(CONCAT(hou r, ':', minute, ' ', ampm) AS TIME) AS myTime
ORDER BY myTime

Sorts some results like this:
10:30 PM
8:30 PM
9:30 PM

But other are perfect:
10:00 AM
11:00 AM
12:00 PM
1:00 PM
2:00 PM
3:00 PM
4:00 PM
5:00 PM

These exact results we part of the same recordset. Can anyone help me
with this? It would be greatly, greatly appreciated.

Thanks in advance!

If you use the date_format routine for the generated time and specify is
like so it should work as well I think:

DATE_FORMAT(CAS T(CONCAT(hour, ':', minute, ' ', ampm) AS TIME), '%r') as
mytime ORDER BY myTime

This way it should generate date/time in the hh:mm:ss format followed by
AM or PM according to
http://dev.mysql.com/doc/mysql/en/da...functions.html

Jonathan
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1961
by: Tom Mortimer | last post by:
Hi, A quick question - can anyone tell me how to interpret negative time values in pstats.Stats.print_stats() output? Eg - 39052965 function calls (38035317 primitive calls) in -250.959 CPU seconds Ordered by: internal time
0
1219
by: Ron Hocking | last post by:
When I link a MySQL table containing a TIME column to Microsoft Access the value does not display. If I edit the value it updates correctly in mysql but if I refresh the Access datasheet view the value disappears and the column shows as blank. Can anyone tell me how to display time values in Access. I am using MySQL ODBC driver 3.51, MySQL 4, Access 2002
1
2528
by: gi75research | last post by:
What should be a very simple function is going terribly wrong, and I don't know why. StartTime and EndTime are table values (formatted like "01:00A" or "02:00P"); DaypartStart and DaypartEnd are string values I specify when calling the function (initially tried passing time values & the same problem occurs); In the If statement, I can compare the exact same time values, and the varCurrentMinute >= varDaypartStart equality will not work;...
3
1566
by: John Smith | last post by:
Hello all: New problem. I am currently storing some mostly numeric values as ids in a db but with the possiblity that there are revisions to each. For instance say we have a record such as 100, a revision may need to be added such as 100a. As a result this column in the db must be set to a string data type. However, when I do so the sort order gets whacked. We have some records in the hundreds and they show up after those in the...
4
11685
by: narayana | last post by:
hi gurus, I am kid at DB2 .problem is I am unable to insert values of time and date datatype fields into tables .default date datatype is taking 4bytes lenght so I am unable to insert date value into it .Is there any way or else I have to do any modifications to my table. plz give some hint.
2
7241
by: jediknight | last post by:
Hi, I have a listview which has columns of text and columns of numerical data. I need to be able to sort these columns into ascending/desending order whenever the user clicks on the column header. The text columns are sorted correctly but the numerical columns seem not to get sorted properly. For example I get the following result when I try to sort a numerical
1
1715
by: madhura | last post by:
Hi, I need to compare some time values stored in a file in the following format & get the lowest of them. FORMAT - 16:12.433(mins:seconds.milliseconds) Can someone help me with the required coding. Madhura
7
3740
by: pt36 | last post by:
Hi I have a php string like this: $string = "one two three four five" I have to sorting the values randomly every time the page are loaded. So, for example: first time "one two three four five" second time "three five one four two" third time
1
2048
by: trevorjhughes | last post by:
I am running an import of some date/time values that have come from a Cognos query. These values are in a CSV file and I want to import them into access. The CSV file would show the value thus: 01/12/2007 3:15PM However when I import into Access, Access creates import errors ad won't import the values. The import spec shows them as date/time. Would it be possible to import the values into a text field and convert them to a...
0
10563
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10621
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10267
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9392
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6998
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5668
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4038
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3123
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.