I have a datetime 0000-00-00 00:00:00 column in my table,
Need to keep the minute and seconds when updating ____-__-__ __:MM:SS
but also change the date and hour to current time.
How can I achieve this on MySQL side?
Edit (Sample):
Current Date field value: 2016-06-27 15:13:07
We Update this table at 2016-07-28 12:31:18
Desired Date field value: 2016-07-28 12:13:07
As you can see the updated date still has correct 2016-07-28 12: but the :13:07 (minutes and seconds) are Selected from the date before update and replaced by current time's minutes and seconds
You can make use of MySQL CONCAT() function. Your update query would looks something like this,
UPDATE table_name
SET
date_column = CONCAT('2016-06-29 15:',MINUTE(date_column),':',SECOND(date_column))
WHERE
column_id = 1
CONCAT() returns the string that results from concatenating the given arguments.
So in your case we would update date and hour by adding first argument as 2016-06-29 15:, and then use the same minute and second from the same column. And concatenate all the arguments to make the new value as you need.
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_concat
Related
I have a table created in phpMyAdmin and it contains two fields: start_time and text_modified. It looks like this
so the start_time might be null.
When I'm filling the data in phpmyadmin I can choose the date and time that should be represented as this timestamp:
After doing so I expect to store a timestamp value in this field instead of date time. But when I do a query SELECT start_time from table I see there this:
So I assumed that it is just the php my admin that shows me automatically all dates as a date time value instead of timestamps. But now when I do a query: SELECT FROM_UNIXTIME(start_time) FROM table I'm getting those results:
and instead I want normal dates here. What is going wrong here?
In a timestamp you can insert datetime values, that are internally stored as integers (the seconds since 1970-01-01 as you probably know). When you select them, they are displayed as date and time.
So far so good.
When you have values like 0000-00-00 00:00:00 you probably inserted NULL values or invalid dates or dates out of range for the integer value. Using FROM_UNIXTIME() doesn't make sense here, since this function calculates a date and time value from an integer value. This integer value of the timestamp column is like I said only used internally. Therefore you get NULL values for valid dates and 1970-01-01 for invalid dates since those were presumably treated as 0 and 0 seconds since 1970-01-01 00:00:00 is, surprise, 1970-01-01 00:00:00.
The timestamp of the nodes in my node table have year as 201 instead of 2014 which are stored in unixtime. Is there a way in which I can update only the year of all the nodes concerned?
MySQL timestamp field allows a range of values between -2147483648 and 2147483648. (A detailed explanation can be found here.) The Unix time equivalent of "201-01-01 00:00:00" is
-55824249600. So, MySQL timestamp field cannot save a date from year 201.
If my above assumption is wrong and a date from year 201 exists in a timestamp field on your database, I think the best solution is;
Calculate the interval between 201-01-01 00:00:00 and 2014-01-01 00:00:00 in seconds:
1388534400 - (-55824249600) = 57212784000
Add this value to all "created" fields in your "node" table with the following SQL query:
UPDATE node SET created = created + 57244320000;
In a SQL Server 2008 database I have a Fiscal Year table where the end_date for each fiscal period is set up wrong. The period start_date is the first day of a calendar month at midnight in the format smalldatetime (2015-01-01 00:00:00). The period end_date is supposed to be the last second of the last day of the start_date's month (2015-01-31 23:59:59). The data type for both fields is smalldatetime.
The following gives me the desired date and time that I would like to put in the end_date field:
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,start_date)+1,0))
But it adds on milliseconds to the end of the time: 2015-01-31 23:59:59.000
When I try converting or casting that as smalldatetime to omit the milliseconds it sets the time to midnight of the last day of the start_date's month: 2015-01-31 00:00:00. This also happens if I just update the end_date field with the code in the select statement above.
How can I update the end_date with the correct format and value (2015-01-31 23:59:59)?
smalldatetime does not store seconds, it's always 00. That's why there's an automatic conversion when you subtract one second.
If you want to store seconds, you have to convert the columns to a different data type.
I didn't realize that smalldatetime doesn't store seconds. KekuSemau's answer wasn't quite the right answer, but it did make me realize I wasn't trying to do the right thing, given the datatype of the column I had to update (No, I couldn't change the datatype of the column).
All I had to do was adjust the query to subtract a minute versus a second, which the smalldatetime field will work with:
cast(DATEADD(MINUTE,-1,DATEADD(mm, DATEDIFF(m,0,start_date)+1,0)) as smalldatetime)
So I ended up updating the end_date column with 2015-01-31 23:50:00, as required.
I have to make a SELECT from a table where the field with the time is in format: y-m-d h-m-s but I have to output the values that are entered for the current day between 00:00:01 and 23:59:59. My query looks like that:
SELECT user, insert_time FROM t_users
WHERE insert_time BETWEEN '2014-06-30 00-00-01' AND '2014-06-30 23-59-59'
The problem is that I don't have to hardcode the date... only the hours interval. And to make it better to understand the current problem I will make a wrong input what is needed to be done: 'TODAY 00-00-01' AND 'TODAY 23-59-59' which of course don't work but if there is a way to make a query that will output the today's added values, I will be grateful.
You can do this kind of calculation with PHP:
http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/
or MysQL solution:
http://blog.ubiq.co/difference-between-two-dates/
Also for today entries I think you can use:
SELECTid FROM my_table
WHERE
timestamp < date_format(date_add(CURRENT_TIMESTAMP(), interval 1 day),'%Y%m%d000000')
AND
timestamp >= date_format(CURRENT_TIMESTAMP(),'%Y%m%d000000')
I have a database table that has fields as such :
TIME(Datetime) Update_ID
2013-11-25 05:00:14 XC3
2013-11-25 06:00:13 XC4
2013-11-25 06:00:19 XC5
2013-12-25 23:00:14 XC6
2013-12-25 24:00:00 XC7
So assuming i want to find a trend on the updates to know which period of the day has the a particular number of updates, what i initially think of is doing something like this :
SELECT COUNT(TIME) FROM table WHERE TIME between '06:00:00' and '12:00:00'
But this doesn't work because i think since the date is not added with the time, a default value for date is added(some date around 1970). If, i add the beginning and enddate in my query, i am afraid it won't give me the results i need.
Use
WHERE HOUR(TIME)...GROUP BY DAY(TIME)
in case you have more than 1 day
You are correct, the problem is that when you do not specify the date, a default one is added.
You can use the EXTRACT function to extract the time from a date, like this:
SELECT COUNT(*)
FROM mytable
WHERE EXTRACT(HOUR_SECOND from TIME) between 60000 and 120000
Note that the time portion in the condition is specified in a different format - i.e. as numbers, without colons and quotes.
Demo on SqlFiddle.