Mysql time stamp queries - mysql

I have a column that uses time stamp. My question is I am a bit confused about how to make queries against it,how would I say
Where $time is after X date
Are queries made in local time or CUT?
When I just try to do where andthe post date /time I get an error because of the space and if I quote it I think it takes it as a string : /
What format do I use for the date in the WHERE clauss !?!?

You can use the normal logical comparisons, for example:
SELECT * FROM table WHERE date >= '2010-01-31'
SELECT * FROM table WHERE date >= '2010-01-31 12:01:01'
Time is generally in your current local time, but you can run the query "SELECT CURTIME()" to check. Also, make sure you have the year-month-date in the right order... that can cause issues.
The manual has more details:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

Assuming you talk about TIMESTAMP column type (not Unix timestamp) the format is either 'YYYY-MM-DD HH:MM:SS' (quoted) or YYYYMMDDHHMMSS
Atually, in the first case you can use any spearators you wish (or none), only numbers are taken into ccount

Related

Time stamp calculation on SQL queries

I'm trying to display the total travel time that each customer in data time format (00:00:00)
But I'm confused to whether I should just do
SEC_TO_TIME(SUM(endtime - starttime))
or
SEC_TO_TIME(SUM(TIME_TO_SEC(endtime - starttime)))
or
SEC_TO_TIME(SUM(TIME_TO_SEC(endtime) - TIME_TO_SEC(starttime))
if endtime and startime is in datetime format
which one should be the right way to do it. I'm getting a different result for first and the second one
1st:
convert endtime and starttime into timestamp using UNIX_TIMESTAMP
2nd:
subtract them
note: the result will be in seconds
(UNIX_TIMESTAMP(endtime) - UNIX_TIMESTAMP(starttime))
to determine how much time did the costumer consume
3rd:
convert the result into time
SEC_TO_TIME( (UNIX_TIMESTAMP(endtime) - UNIX_TIMESTAMP(starttime)) )
so your final query should be like this
SELECT SEC_TO_TIME( (UNIX_TIMESTAMP(endtime) - UNIX_TIMESTAMP(starttime)) ) FROM travel
Of your three options, I would recommend:
SEC_TO_TIME(SUM(TIME_TO_SEC(endtime)
- TIME_TO_SEC(starttime))
First, I think this is clearest on what you want to do:
Convert the date/times to seconds
Add up the seconds
Convert back to a time
Second, MySQL treats date/time values as numbers in a numeric context. This can produce strange results because 2018-09-13 is turned into 20,180,913, and that is not usually what you want.

MySQL timestamp format and datediff

Hi I'm writing queries for MySQL, and now my database has a column containing the timestamp in this format: 7/14/2015 7:57:49 AM, but I need to use the DATEDIFF function, so how can I convert the timestamp into the format like: 2015-7-14 (or 2015-07-14, I'm not sure which one is correct; just the date)?
This should convert your string to just the date in a date format, then you can use DATEDIFF on the date fields in question:
SELECT STR_TO_DATE(LEFT(t,LOCATE(' ',t) - 1), '%m/%d/%Y') FROM my_table;
The LEFT function will take the substring to the left of the space, which is just your date, then STR_TO_DATE will convert that substring to a date the system can use.
(Not knowing your field and table names, I used t and my_table.)
You don't need to. The way MySQL displays timestamps has nothing to do with the way they're stored internally; as long as it's TYPE TIMESTAMP or some compatible type, the DATEDIFF() function will know what to do with it.
TIMESTAMPs are actually stored as a really huge integer representing (I think) milliseconds from Midnight UTC, January 1st, 1970. The display format is determined by a system global variable, and has nothing to do with the actual value.
Converting from a string to a DATETIME or TIMESTAMP is actually also fairly straightforward using the STR_TO_DATE() function; in your case the format string would be something like
STR_TO_DATE('%c/%e/%Y %l:%i:%s %p', datecol)
although you might have to experiment a bit to make it work reliably.

MySql YYYY-MM-DDTHH:MM:SS to time

I have a table with a varchar column DateFrom that has this format:
2014-02-22T08:08:00
I want an sql that prints 08:08 and one that prints 22-02-2014 but i can't seem to get the time function to work.
What i'm trying to do is get all entries in DateFrom and print them as just time (HH:MM)
and the same with date.
Altough I think string functions are a better option in this case (like #hakre answered) and less cpu expensive, you can also achieve this goal using the STR_TO_DATE, DATE and TIME function.
SELECT
DATE(STR_TO_DATE('2014-02-22T08:08:00', '%Y-%m-%dT%H:%i:%s')),
TIME(STR_TO_DATE('2014-02-22T08:08:00', '%Y-%m-%dT%H:%i:%s'))
If you're not looking for date/time but for string functions, they are available here:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html
One string function that I think is useful for your substring operation is SUBSTRING. You can tell per the varchar column that you want a sub-string starting from a position for a certain length with it:
SUBSTRING(DateFrom FROM 1 FOR 8) AS DateName -- "2014-02-22"
SUBSTRING(DateFrom FROM 10 FOR 5) AS TimeName -- "08:08"
Use other string functions to concatenate parts in the order you need it.
Alternatively you can cast your varchar string in that format to a datetime type and then format as needed:
CAST(DateFrom AS datetime)
See the Mysql manual for more information about casting types and the date-time functions that are available:
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html
SQL Example:
SET #DateFrom = '2014-02-22T08:08:00';
SELECT DATE(CAST(#DateFrom AS datetime)); -- '2014-02-22'
SELECT TIME(CAST(#DateFrom AS datetime)); -- '08:08:00'

why this MySQL SELECT doesn't include the right dates?

The main problem is, that I have stored in database datetime , not the date (what I need). Ok never mind.
I have thousands of reports stored each day.
I need to LEFT by 10 my datetime_view (to cut the time) and everything's fine. Except this. I'm trying to figure out why do I have to put in the condition + one day from the future? Otherwise it won't search what I want.
SELECT
LEFT(datetime_view,10),
count(type)
FROM reports
WHERE
type IN (1,2,5)
AND
datetime_view>='2012-10-28'
AND
datetime_view<='2012-11-04'
group by LEFT(datetime_view,10);
You can see I must search from the future. Why??
It gives me an output from 28.10 to 3.11 ....
don't use string operations on date/time values. MySQL has a huge set of functions for date/time manipulation. Try
GROUP BY DATE(datetime_view)
instead, which will extract only the date portion of the datetime field. Your string operation is not y10k compliant. Using the date() function is.
As for your plus one day, consider how the comparisons are done: A plain date value, when used in date/time comparisons, has an implicit 00:00:00 time value attached to it, e.g. all dates have a time of "midnight".
i think it's better to use DATE(datetime_view) to cut the time instead of LEFT(datetime_view,10), also on the where condition:
DATE(datetime_view) <= '2012-11-03'

MS Access SELECT and WHERE

I am having trouble getting records out of a database for a certain date. This this the SQL command I have at the moment:
SELECT * FROM ict1 WHERE date='26/03/1992'
It should return one record from my database. "date" is a column and one of the records has '26/03/1992' as the value. I have tested the db connection and I can use:
SELECT * from ict1
So I know it's not that. It's probably just the SQL syntax being a lot different, I'm used to MySQL :#)
Should probably mention that I'm using .NET with an OleDbConnection.
Thanks.
Usually dates need to be formatted as for access like the following
Select * from ict1 where date= #03/26/1992#
The # denotes a date in access.
Be very careful with dates formatted as '10/03/1992' as it could mean 10th of march or 3rd of october depending on where you are.
Also #1992/03/26# also works
You may want to use a date comparison function instead of date = #xxxx/xx/xx#. Date comparisons do not yield expected results because of formatting and data type issues. In SQL Server your date might be stored as a datetime or a date data type. You need to make sure you are comparing things in the same type and format.
DateDiff ("d", #1992/03/26#, date) = 0
Use the date as YYYY/MM/DD format:
SELECT * FROM ict1 WHERE date='1992/03/26'
SELECT * FROM ict1 WHERE date=#1992/03/26#