473,763 Members | 6,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sort by time (hh:mm:am/pm - format) in text string

Hi,

I have a group of records that I need to sort by time field. The time
field is a text string (should've been a date/time - I know -doh!) I
was wondering if anyone knew how to write a query to sort in Ascending
order with the earliest appearing first.

The times are stored as follows:

10:30pm
11:30pm

etc.

Many, many thanks to anyone who can help with this one. Cheers.
Jul 17 '05 #1
5 12242
00steve wrote:
Hi,

I have a group of records that I need to sort by time field. The time
field is a text string (should've been a date/time - I know -doh!) I
was wondering if anyone knew how to write a query to sort in Ascending
order with the earliest appearing first.

The times are stored as follows:

10:30pm
11:30pm


Try something built from this MySQL query :

select time_format(tim e, '%I:%i%p') as the_time from timetest order by
the_time asc;

----------------------- Sample output ----------------------------
mysql> select * from timetest;
+---------+
| time |
+---------+
| 10:15pm |
| 12:14pm |
| 12:15am |
| 9:30am |
+---------+
4 rows in set (0.00 sec)

mysql> select time_format(tim e, '%I:%i%p') as the_time from timetest
order by the_time asc;
+----------+
| the_time |
+----------+
| 09:30AM |
| 10:15AM |
| 12:14PM |
| 12:15PM |
+----------+
4 rows in set (0.00 sec)
----------------------------- ends --------------------------------

Hope this helps,

Regards,

Andy

Jul 17 '05 #2
Andy Barfield wrote:

Hmmmm - someone goofed - sorry :(
Jul 17 '05 #3
"00steve" <29****@tay.ac. uk> wrote in message
news:df******** *************** ***@posting.goo gle.com...
Hi,

I have a group of records that I need to sort by time field. The time
field is a text string (should've been a date/time - I know -doh!) I
was wondering if anyone knew how to write a query to sort in Ascending
order with the earliest appearing first.

The times are stored as follows:

10:30pm
11:30pm

etc.

Many, many thanks to anyone who can help with this one. Cheers.


Something like this should work:

SELECT * FROM classes ORDER BY CAST(start_time AS DATETIME)

Jul 17 '05 #4
00steve wrote:
I have a group of records that I need to sort by time field. The time
field is a text string (should've been a date/time - I know -doh!) I
was wondering if anyone knew how to write a query to sort in Ascending
order with the earliest appearing first.

The times are stored as follows:

10:30pm
11:30pm


If the records are in a database, then make the database work for you
as described by Andy in previous article. If you just have a bunch of
dates, then the mktime() function can help you convert that into UNIX
time. Put those in an array, and then sort that according to whatever
principle you want. Then using date(), you can display the times in
more human readable form.

/Marcin
Jul 17 '05 #5
Marcin Dobrucki wrote:
00steve wrote:
The times are stored as follows:

10:30pm
11:30pm

If the records are in a database, then make the database work for you
as described by Andy in previous article. If you just have a bunch of
dates, then the mktime() function can help you convert that into UNIX
time. Put those in an array, and then sort that according to whatever
principle you want. Then using date(), you can display the times in
more human readable form.

/Marcin


I was caught by the am/pm marker - which the TIME_FORMAT function
ignores - I could do it in PHP with no probems, but this one had me
pulling what little hair I have out by the roots! How big is the
database? What would be the work involved in converting it - could you
simply alter the table and add a date/time field to the database and
then run a utility that would populate it from the original time field?

Just some ideas - good luck steve!

Jul 17 '05 #6

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

Similar topics

2
3653
by: 00steve | last post by:
Hi, I have a group of records that I need to sort by time field. The time field is a text string (should've been a date/time - I know -doh!) I was wondering if anyone knew how to write a query to sort in Ascending order with the earliest appearing first. The times are stored as follows: 10:30pm
3
3521
by: Barney | last post by:
in my db i have the time as seconds, 3600, 1800, etc. how can i convert that to the actual time and display it as 1:00 AM, etc? thx
4
39044
by: Rich | last post by:
Now.ToShortTimeString returns 9:13 PM. I would like to get this in miliatry time with seconds included 21:13:45 (or something like that - just military time with seconds). How can this be done? Thanks, Rich
1
2765
by: Jason Chan | last post by:
DateTime mydate = new DateTime(2006,1,1,0,0,0); string testStr = mydate.ToString("hh:mm:ss"); //return 12:00:00 mydate = new DateTime(2006,1,1,1,0,0) testStr = mydate.ToString("hh:mm:ss"); //return 01:00:00 I want "00:00:00" instead of "12:00:00", what is going wrong?
5
19187
nirmalsingh
by: nirmalsingh | last post by:
i am getting date format in string as 30-11-2006 05:59:44 PM . i want to convert in the format of yyyy-mm-dd hh:mm:ss to sore it in mysql database. Help me with sample coding plz..
1
3171
by: unknown66 | last post by:
hello, I have a time value in unix time that I would like to convert into a time format of hh:mm:ss:ms. For example, I have this number, 1172234138451, that I would like to be in the hh:mm:ss:ms format. I have tried timedelta as well as time.ctime. Does anyone have any suggestions? Thanks.
1
2686
seshu
by: seshu | last post by:
Hi every body to morning my cousine has show his application and also his live db in that to sav the length of all the voice files he has saved in time format ie he took the datatype of time now i want to generate a an excell sheet of that table but here i want the time to be like mm:ss but not hh:mm:ss examle the length of song sweet dreams is 01:12:35 now i want this to be shown as 72:35 how is there any procedure to convert directly in...
1
2167
seshu
by: seshu | last post by:
Hi every body to morning my cousine has show his application and also his live db in that to sav the length of all the voice files he has saved in time format ie he took the datatype of time now i want to generate a an excell sheet of that table but here i want the time to be like mm:ss but not hh:mm:ss examle the length of song sweet dreams is 01:12:35 now i want this to be shown as 72:35 how is there any procedure to convert directly in...
1
3388
by: chindanoor | last post by:
Here is my code... class TimeConverter { public static void main(String ar) { int sec=3650; int min=0; int hour=0; while(sec>=60)
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10144
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9997
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
9937
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
6642
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
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
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
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
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.