Im newbie for sql. My code:
$start_date='2017-01-01';
$end_date='2016-01-31';
$this->db->where('date_note BETWEEN "'. date('Y-m-d', strtotime($start_date)). '" and "'. date('Y-m-d', strtotime($end_date)).'"');
But in my database is like 2016-03-11 14:21:36
How I can get this record from database with different format date? Thanks
The MySQL function DATE_FORMAT() resolves fully your problem as it allows you to get the time you saved within your database in the format of your choice.
There is no problem in your SQL. It is okay to compare DATE with DATATIME(as in your database).
Here is a demo:
mysql> SELECT '2016-03-11 14:21:36' BETWEEN '2016-03-10' AND '2016-03-12';
+-------------------------------------------------------------+
| '2016-03-11 14:21:36' BETWEEN '2016-03-10' AND '2016-03-12' |
+-------------------------------------------------------------+
| 1 |
+-------------------------------------------------------------+
1 row in set (0.00 sec)
Related
I have a column dateofbirth_string which is a VARCHAR containing dates in format dd/mm/yyyy. I would like to convert these values into format DATE (yyyy-mm-dd) and place in field dateofbirth_date in the same row.
Note, some values in dateofbirth_string may be in a bad format e.g. 10/02/15 or 100215. For these values, they can be ignored and I will enter manually.
Thanks in advance for your help!
STR_TO_DATE is your goal.
SELECT STR_TO_DATE('22/04/2020', '%d/%c/%Y');
Sample
MariaDB [(none)]> SELECT STR_TO_DATE('22/04/2020', '%d/%c/%Y');
+---------------------------------------+
| STR_TO_DATE('22/04/2020', '%d/%c/%Y') |
+---------------------------------------+
| 2020-04-22 |
+---------------------------------------+
1 row in set (0.02 sec)
MariaDB [(none)]>
The actor table in the salika sample schema defines column last_update as a timestamp. I want to render that column using JSON_ARRAY in ISO8601 format. - First shouldn't that be the default rendering for JSON_ARRAY.
From reading the documentation and comments on this website and others it appears that the answer is to use FROM_UNXTIME with an output mask that generates ISO8601 Format.
Unfortunately FROM_UNIXTIME() appears to always return NULL on my database
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2018-10-03 17:15:03 |
+---------------------+
1 row in set (0.00 sec)
mysql> select from_unixTime(current_timestamp())
-> ;
+------------------------------------+
| from_unixTime(current_timestamp()) |
+------------------------------------+
| NULL |
+------------------------------------+
1 row in set (0.00 sec)
mysql>
I suspect this may be caused by the fact that I have not installed the timezone configuration files.. However when I try that I get...
mysql -u root -p****** sys <timezone_posix.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1146 (42S02) at line 1: Table 'sys.time_zone' doesn't exist
Sure I've missed something obvious here....
Just try this:
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(CURRENT_TIMESTAMP()));
As mysql doc says, function FROM_UNIXTIME(unix_timestamp[,format]) only accept parameter as a UNIX_TIMESTAMP, but not a TIMESTAMP.
Luckily, there's a function UNIX_TIMESTAMP(date) transforming various types such as DATE, TIMESTAMP and so on into UNIX_TIMESTAMP. So call UNIX_TIMESTAMP(date) first, and then `FROM_UNIXTIME(
This worked for me in the end...
select json_array("actor_id","first_name","last_name",DATE_FORMAT(convert_tz("last_update", ##session.time_zone, '+00:00'),'%Y-%m-%dT%T%fZ')) "json" from "sakila"."actor"
/
which gives
[
1,
"PENELOPE",
"GUINESS",
"2006-02-15T12:34:33000000Z"
],
[
2,
"NICK",
"WAHLBERG",
"2006-02-15T12:34:33000000Z"
]
I have two different development environment which is MySQL 5.6 & Maria DB 10.0
I use WHERE query like time>="userInputBeginTime" to limit the minimum date of data.
Unfortunately, the front-side provide wrong date information(in my case, the expected input is date string formatted like "yyyy-MM-dd", but the real input was "Invalid Date" which due to javascript error)
And then i found a difference between MySQL & Maria DB
In MySQL:
SELECT NOW() > "Invalid Date" test;
+-----------+
| test |
+-----------+
| 0 |
+-----------+
In Maria DB:
SELECT NOW() > "Invalid Date" test;
+-----------+
| test |
+-----------+
| 1 |
+-----------+
This difference leads directly to the different result that MySQL returned nothing and Maria DB returned everything.
I think may be they use different methods to compare
My temporary solution is using SQL like this
SELECT * FROM table_name
WHERE
NOT ISNULL(CONVERT("userInputBeginTime",datetime))
AND time >= "userInputBeginTime"
to force Maria DB return nothing when invalid input was detected.
I would like to know if there is a better solution?
Best Regards
You can try using STR_TO_DATE to parse the string as a date in the format you expect. This function would return NULL if it cannot parse the date in the format specified. Your bad JavaScript input would fall into this category and should produce NULL:
SELECT *
FROM table_name
WHERE STR_TO_DATE(userInput, '%Y-%m-%d') IS NOT NULL AND
<other conditions here>
STR_TO_DATE in MariaDB
STR_TO_DATE in MySQL
quick question. In the DATETIME column in a MYSQL table, the format is, YYYY-MM-DD. So If i put insert today's date, as 2014-01-09, MYSQL will understand... But will it understand if i insert data like this?
YYYY/MM/DD or 2014/01/09
From the manual:
MySQL permits a “relaxed” format for values specified as strings, in which any punctuation character may be used as the delimiter between date parts or time parts.
So it would seem the answer is, yes.
mysql> select date('2016/06/16');
+--------------------+
| date('2016/06/16') |
+--------------------+
| 2016-06-16 |
+--------------------+
1 row in set (0.01 sec)
I have a JavaScript timer on a PHP page. It returns a number, representing seconds, that I want to insert into a database column of type Time. How can I do that?
This code is the best I could find:
$query = "INSERT INTO #__quiz_r_student_question (c_stu_quiz_id,
c_question_id,
c_score,
c_attempts,
is_correct,
time_to_solve
)" .
"\n VALUES('".$stu_quiz_id."',
'".$quest_id."',
'".$c_quest_score."',
'".($c_quest_cur_attempt + 1)."',
'".$is_correct."',
DATEADD(ms, ".$time_to_solve."* 1000, 0)
)";
This answer was originally posted by the asker as an edit to the question.
I solved my problem with PHP, not an SQL query. I used this code to change my seconds value into a readable time ready for database insertion:
$time = gmdate('H:i:s',$time_to_solve)
You can use a SEC_TO_TIME function. Take a look at this script -
CREATE TABLE table_time(
column1 TIME DEFAULT NULL
);
INSERT INTO table_time VALUES(SEC_TO_TIME(3600));
SELECT * FROM table_time;
+----------+
| column1 |
+----------+
| 01:00:00 |
+----------+