473,394 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

date/time in sql

112 100+
This is my select statement:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.   a2.conditionindex, a1.timestamp, 
  3.   a3.alarmdefinitionname, a3.alarmdefinitionseverity, 
  4.   a2.tripsubjectname,a2.tripcontextname,  a2.tripvalue,
  5.   a5.conditionname, a5.conditiontype 
  6. FROM alarmnotification AS a1, 
  7.      alarmnotificationdetail AS a2, 
  8.      alarmdefinition AS a3, 
  9.      alarmcondition AS a5 
  10. WHERE a1.notificationid=a2.notificationid 
  11. AND a1.definitionid=a3.databaseidentifier 
  12. AND a2.conditionid=a5.databaseidentifier 
  13. ORDER BY a1.timestamp, a3.alarmdefinitionseverity";
right now, time stamps is displayed: month/day/year hour/min/sec am/pm

MY QUESTIONS IS:
I want my timestamp to only display hour/min/sec

how do I change my select statement.

thank you
Jan 27 '09 #1
12 2276
Atli
5,058 Expert 4TB
Hi.

Try the DATE_FORMAT function.

Should allow you to convert any date/time type field into whatever format you want.
Jan 27 '09 #2
ndedhia1
112 100+
should I use the date_format in the create table for example,

CREATE TEXT TABLE ALARMNOTIFICATION(
TIMESTAMP DATE_FORMAT('HH:MM:SS'),
Jan 28 '09 #3
mwasif
802 Expert 512MB
No, it will be used at the time of SELECTing data. Did you look at the link provided by Atli?
Jan 28 '09 #4
ndedhia1
112 100+
I used it in the select statement like this:
"SELECT a2.conditionindex, DATE_FORMAT(a1.timestamp, '%r'),...
but it only changed the header of the table and not the contents inside the table.

thanks
Jan 28 '09 #5
Atli
5,058 Expert 4TB
This wouldn't change the actual data stored in your table.
It would change the way the data is returned by the SELECT statement.

That is, if these were the actual values inside your table:
Expand|Select|Wrap|Line Numbers
  1. mysql> SELECT Timestamp FROM myTable;
  2. +---------------------+
  3. | Timestamp           |
  4. +---------------------+
  5. | 2009-01-28 08:45:00 | 
  6. | 2009-01-28 13:45:00 | 
  7. | 2009-01-28 03:45:00 | 
  8. | 2009-01-28 20:45:00 | 
  9. +---------------------+
And you execute this query:
Expand|Select|Wrap|Line Numbers
  1. SELECT DATE_FORMAT(timestamp, '%r') AS 'Time'
  2. FROM myTable
  3.  
You would get:
Expand|Select|Wrap|Line Numbers
  1. mysql> SELECT DATE_FORMAT(timestamp, '%r') AS 'Time'
  2.      > FROM myTable;
  3. +-------------+
  4. | Time        |
  5. +-------------+
  6. | 08:45:00 AM | 
  7. | 01:45:00 PM | 
  8. | 03:45:00 AM | 
  9. | 08:45:00 PM | 
  10. +-------------+
Jan 28 '09 #6
ndedhia1
112 100+
another questions:
if in my create table, I want to do it so that there is only hh:mm:ss for a field..do i do it something like this:

CREATE TEXT TABLE ALARMNOTIFICATION(
TIMESTAMP DATETIME(hh:mm:ss),
Jan 28 '09 #7
Atli
5,058 Expert 4TB
No, you would use the TIME data type.

The Reference Manual contains a list of all available data types, as well as everything else you may want to know about the correct usage of MySQL syntax.
Jan 28 '09 #8
ndedhia1
112 100+
What i ment when i said that the data is changed is that when I run:
"SELECT a2.conditionindex, DATE_FORMAT(a1.timestamp, '%r'),...
the file that I am using to print this select statement comes back empty with only the header line being there with the titles of the fields. All of the data is not showing up
Jan 28 '09 #9
Atli
5,058 Expert 4TB
Ok. Then there is either no data fitting your query, or your code is not displaying it properly.

Either way, we would need to see the code to be able to help.
It's damn hard to debug code you can't see :)
Jan 28 '09 #10
ndedhia1
112 100+
There is definitely Data that fits the query, because if comment out if statements that deal with the timestamp, the rest of the fields are populated. This code that i am sending you is the original that displays the output in an excel file but with the timestamp, displays the date, and hour and min. Everything is pretty much correct but I want the timestamp to only display the hr, min and seconds. No date.
Here is parts of the code:

The select statement:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     a2.conditionindex, 
  3.     a1.timestamp, 
  4.     a3.alarmdefinitionname, 
  5.     a3.alarmdefinitionseverity, 
  6.     a2.tripsubjectname, 
  7.     a2.tripcontextname, 
  8.     a2.tripvalue, 
  9.     a5.conditionname, 
  10.     a5.conditiontype 
  11. FROM alarmnotification AS a1, 
  12.      alarmnotificationdetail AS a2, 
  13.      alarmdefinition AS a3, 
  14.      alarmcondition AS a5 
  15. WHERE a1.notificationid=a2.notificationid 
  16.   AND a1.definitionid=a3.databaseidentifier 
  17.   AND a2.conditionid=a5.databaseidentifier 
  18. ORDER BY a1.timestamp, a3.alarmdefinitionseverity
the Create table code:
Expand|Select|Wrap|Line Numbers
  1. CREATE TEXT TABLE ALARMNOTIFICATION(
  2. LOGGINGVERSION DECIMAL(4,2),
  3. LOGGINGSTYLE CHAR(1),
  4. NOTIFICATIONID INTEGER,
  5. MILLISECONDS BIGINT,
  6. TIMESTAMP TIMESTAMP,
  7. ACTIVATIONID INTEGER,
  8. DEFINITIONID INTEGER,
  9. DEFINITIONSEVERITY INTEGER,
  10. CONSTRAINT PK_ALARMNOTIFICATION PRIMARY KEY(NOTIFICATIONID)
  11. );
the php code to desplay the tables:
Expand|Select|Wrap|Line Numbers
  1. while($row=mysql_fetch_array($result)){
  2.  
  3.         fwrite($debug_handle, $row['conditionindex'] . ",");
  4.         fwrite($debug_handle, $row['timestamp'] . ",");
  5.         fwrite($debug_handle, $row['alarmdefinitionname'] . ",");
  6.         fwrite($debug_handle, $row['alarmdefinitionseverity'] . ",");
  7.         fwrite($debug_handle, $row['tripsubjectname'] . ",");
  8.         fwrite($debug_handle, $row['tripcontextname'] . ",");
  9.         fwrite($debug_handle, $row['tripvalue'] . ",");
  10.         fwrite($debug_handle, $row['conditionname'] . ",");
  11.         fwrite($debug_handle, $row['conditiontype'] . ",");
  12.         fwrite($debug_handle, "\r\n");
  13.  
  14.         if ((substr($row['timestamp'],11,12)>=$timeFrame_min) && (substr($row['timestamp'],11,12)<$timeFrame_max)) {
  15.             fwrite($handle, "<tr>");
  16.             fwrite($handle, "<td>" . $row['timestamp'] . "</td>");
  17.             fwrite($handle, "<td>" . $row['alarmdefinitionname'] . "</td>");
  18.             if ($row['alarmdefinitionseverity'] == 1)
  19.                 fwrite($handle, "<td> High </td>");
  20.             elseif ($row['alarmdefinitionseverity'] == 2)
  21.                 fwrite($handle, "<td> Medium </td>");
  22.             elseif ($row['alarmdefinitionseverity'] == 3)
  23.                 fwrite($handle, "<td> Low </td>");
  24.             fwrite($handle, "<td>" . $row['tripsubjectname'] . "</td>");
  25.             fwrite($handle, "<td>" . $row['tripcontextname'] . "</td>");
  26.             if ($row['tripvalue'] == 1)
  27.                 fwrite($handle, "<td> DOWN </td>");
  28.             else
  29.                 fwrite($handle, "<td>" . $row['tripvalue'] . "</td>");
  30.             fwrite($handle, "<td>" . $row['conditionname'] . "</td>");
  31.             if ($row['conditiontype'] == 1)
  32.                 fwrite($handle, "<td> Instrumentor </td>");
  33.             elseif ($row['conditiontype'] == 2)
  34.                 fwrite($handle, "<td> Process Watcher </td>");
  35.             else
  36.                 fwrite($handle, "<td> ERROR#NOTENOUGHINFO# </td>");
  37.             fwrite($handle, "</tr>");
  38.         }
  39.     }
the Java code that gets the time:
Expand|Select|Wrap|Line Numbers
  1. pw.print(c.get(c.YEAR) + "-" + (c.get(c.MONTH)+1) + "-" + c.get(c.DAY_OF_MONTH) + " " + c.get(c.HOUR_OF_DAY) + ":" + c.get(c.MINUTE) + ":" + c.get(c.SECOND) + ",");
hope that helps a bit.

thanks
Jan 29 '09 #11
Atli
5,058 Expert 4TB
Ok, so I'm assuming the output you need to change is in line #16 of your PHP code?

If so, there are two ways I see.

First, apply the DATE_FORMAT function to your SELECT query, like we have been discussing.
You would have to reflect this change in your if statement on line #14 to. It is expecting the the standard datetime format, so it probably won't work after the format.

Or, you could use the PHP date function in line #16 to alter the timestamp before you print it. This would not require you to actually alter the query, so your timestamp would remain intact if it has to be used elsewhere.
Note that you will have to run the timestamp through strtotime to get the Unix timestamp before you run it through the date function.

Like:
Expand|Select|Wrap|Line Numbers
  1. echo date("H:i:s", strtotime($row['timestamp']));
P.S.
Where does that Java code fit in?
You know, PHP can do that to:
Expand|Select|Wrap|Line Numbers
  1. echo date("Y-m-d H:i:s", time());
Jan 29 '09 #12
ndedhia1
112 100+
Thanks for the help,
That made the correct conversion.

thank you
Jan 29 '09 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: androtech | last post by:
Hello, I'm looking for a function that returns a date range for a specified week number of the year. I'm not able to find functions like this anywhere. Any pointers/help would be much...
15
by: Khurram | last post by:
I have a problem while inserting time value in the datetime Field. I want to Insert only time value in this format (08:15:39) into the SQL Date time Field. I tried to many ways, I can extract...
13
by: maflatoun | last post by:
Hi, I have the following function to convert UTC time to Local time. It works perfect for GMT- (Minus) time zones however it provides incorrect results for GMT+(Plus) time zones? // Format to...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
6
by: Luvin lunch | last post by:
Hi, I'm new to access and am very wary of dates as I have limited experience in their manipulation and I know if they're not done properly things can turn ugly quickly. I would like to use a...
3
by: Jim in Arizona | last post by:
I have a gridview that's being populated from an access db query. The problem I'm having is that the date/time fields in access that are populating the gridview are showing both date and time, when...
3
by: colleen1980 | last post by:
Hi: Data in my table is in that format. How to i separate date with time. 11/9/2006 10:10:46 AM Thank You.
6
by: Geoff Cox | last post by:
Hello, at the moment I can add the combined date and time into MySQL using php $dt1 = date("Y-m-d H:i:s"); is it possible to add the date and time separately? I thought it might be
0
yasirmturk
by: yasirmturk | last post by:
Standard Date and Time Functions The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any...
4
by: ahmurad | last post by:
Dear Brothers, I am struggling the following four Date-Time type values which were inputted into MYSQL database in different tables. As MYSQL Default Time Format: YYYY-MM-DD HH:MM:SS, So I used...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.