I want to convert a YYYY-MM-DD column to unix timestamp. Similar to this question: MySQL - Convert MM/DD/YY to Unix timestamp
MySQL does not seem to transform a value of '2012-05-10' to the correct unixtime. Here is the suggested function that fails:
SELECT UNIX_TIMESTAMP(CAST(dateid AS DATE)) AS unixtime
What version of MySQL you are using? As tested by MySQL 5.5.27 in SQLFiddle these two queries worked
SELECT UNIX_TIMESTAMP(CAST('2012-05-10 00:00:00' AS DATETIME)) As UnixTime;
SELECT UNIX_TIMESTAMP(CAST('2012-05-10' AS DATE)) As UnixTime;
SQLFiddle Demo
I'm not sure what the problem you are having exactly, but the following works fine:
CREATE TABLE ex (dt VARCHAR(20));
INSERT INTO ex SET dt = '2012-05-10';
SELECT
UNIX_TIMESTAMP('2012-05-10') ex1,
UNIX_TIMESTAMP(dt) ex2,
UNIX_TIMESTAMP(CAST('2012-05-10' AS DATE)) ex3,
UNIX_TIMESTAMP(CAST(dt AS DATE)) ex4
FROM ex;
returns:
+------------+------------+------------+------------+
| ex1 | ex2 | ex3 | ex4 |
+------------+------------+------------+------------+
| 1336633200 | 1336633200 | 1336633200 | 1336633200 |
+------------+------------+------------+------------+
As you can see, there is no need to CAST() if the string is in the form YYYY-MM-DD or YYYY-MM-DD HH:MM:SS, as MySQL implicitly converts strings in this format to DATEs.
See http://sqlfiddle.com/#!2/1a215/3
Related
sort in ascending order whole column having a date in this format
Apr-18 |
Apr-19 |
Dec-15 |
Feb-19 |
Nov-13 |
Nov-17 |
ORDER BY CONVERT(DateTime, Date,101)
this is not working
The convert syntax you're using belongs to sql-server, not mysql.
For MySQL, you can use str_to_date:
ORDER BY SELECT str_to_date(DateTime, '%b-%y');
im mysql should be
if DateTime is a valid datetime column
order by date_format(datetime, '%b-%d')
if DateTime is a string column you should use str_to_date
order by str_to_date(datetime, '%b-%d')
Hello i need expert advice for my mysql query
I'm trying to filter values between 2 months
If start day lower then end day of different months all working well
But if i try higher day value on start return none
this is working code
SELECT id, teslim_tarihi AS tarih, toplam, marka, model, malzeme
FROM ariza
WHERE durum = '7' AND (teslim_tarihi BETWEEN '01-02-2018 00:00' AND '01-03-2018 23:59')
ORDER BY tarih DESC
Not working at all
SELECT id,teslim_tarihi as tarih ,toplam,marka,model,malzeme
FROM ariza
WHERE durum = '7' AND (teslim_tarihi BETWEEN '14-02-2018 00:00' AND '01-03-2018 23:59')
ORDER BY tarih DESC
date format dd-mm-yyyy H:i
As has been suggested by #stackFan, it really does make considerable sense to stick with mysql's default date and time formats. However, for whatever reason, you seem to be stuck with a different format so I'll attempt to work with that.
Your current query isn't working in your second example because mysql doesn't recognise these as dates and a strings starting '14-02-2018' is greater than another string starting '01-03-2018'. e.g.
SELECT '14-02-2018' > '01-03-2018';
+-----------------------------+
| '14-02-2018' > '01-03-2018' |
+-----------------------------+
| 1 |
+-----------------------------+
The values when using BETWEEN ... AND have to have the min value first and the max value Documentation, so in your second example the comparison is the wrong way round because '14-02-2018' is greater than '01-03-2018', hence no rows returned.
You didn't answer the query about the data type of your column teslim_tarihi which would have made answering your query simpler. I'll assume it is a DATETIME or TIMESTAMP. Your comparison should be made against something that mysql knows to be a date or recognises as a date and that means getting the dates into YYYY-MM-DD or YY-MM-DD format. Mysql will helpfully cast values to the appropriate type if the format is one it recognises. e.g.
SELECT DATE '2018-02-14' < '2018-03-01';
+-----------------------------------+
| DATE '2018-02-14' < '2018-03-01' |
+-----------------------------------+
| 1 |
+-----------------------------------+
You should be able to get the query working by turning your strings into dates in the following manner.
SELECT DATE '2018-03-01'
BETWEEN STR_TO_DATE('14-02-2018 00:00', '%d-%m-%Y %H:%i')
AND STR_TO_DATE('01-04-2018 23:59','%d-%m-%Y %H:%i') 'between dates';
+---------------+
| between dates |
+---------------+
| 1 |
+---------------+
If your column teslim_tarihi is a VARCHAR, then convert that in the same manner to get the query to work.
As per the documentation the default format of date in MySQL is YYYY-MM-DD. Can you try using this query instead?
SELECT id,teslim_tarihi as tarih ,toplam,marka,model,malzeme
FROM ariza
WHERE durum = '7' AND (teslim_tarihi BETWEEN '2018-02-14 00:00:00' AND '2018-03-01 23:59:59')
ORDER BY tarih DESC
I'm running MySQL and trying to convert the following row from VarChar to DateTime
+-------------------+
| review_date |
+-------------------+
| May 24, 2015 |
| June 3, 2013 |
| December 19, 2003 |
+-------------------+
I have tried CAST(review_date as DATETIME) However that results in a column of null values when I test it in the SELECT statement.
I also tried CONVERT(DATETIME, review_date, 107) however, it resulted in: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'review_date, 107) at line 3
I haven't converted to date time before but I can't understand how the above statements deviate from the documentation or why they wouldn't be working.
I used this syntax to create for the column: review_date varchar(255) DEFAULT NULL. And STR_TO_DATE(review_date, '%m/%d/%Y') also yields a column of Null cells.
The problem is when you cast or convert, your string value isn't in the standard yyyy-mm-dd format.
If you run this example, you will see what I mean. This example also has the proper way to convert string to date:
SELECT
CAST(review_date AS DATETIME) -- gives null due to incorrect format
, STR_TO_DATE(review_date, '%M %d, %Y') -- gives correct date
, CAST('2015-05-24' AS DATE) -- gives date due to correct format
FROM
date_test
This is for MYSQL (where I converted my string to date function)
SELECT cast(str_to_date(column_name), '%m/%d/%y') as date)
FROM Table Name;
Example:
SELECT cast(str_to_date(Release_Date, "%m/%d/%y") as date)
FROM movie-data;
Make sure that you use the delimiter in your date format correctly. Like in my case the date was in the string column was separated by '/'. In your case it might be separated with a comma ',' or dot '.'. Make sure you use that.
Example: 3/8/2016 "%m/%d/%y"
3,8,2016 "%m,%d,%y"
3.8.2016 "%m.%d.%y"
I have a char field which is having Date in it as '26-Nov-2011'
I want this date to be printed in YYYY-MM-DD format.
I used following date conversion which is not allowing me to do.
select date_format('26-Nov-2011','%d-%b-%y')
returns Null.
I know date_format function can accept date in YYYY-DD-MM format but
Please let me know how to convert a Character to Required Date Format
Try this:
SELECT STR_TO_DATE('26-Nov-2011', '%d-%b-%Y') dte;
Output:
2011-11-26
You can use STR_TO_DATE() to convert your strings to MySQL date values and ORDER BY the result
STR_TO_DATE(datestring, '%d/%m/%Y')
STR_TO_DATE can be used with this format '%d-%M-%Y', which will match the format of your current date string. Otherwise it will return null. Check on date formats as well.
select str_to_Date('26-Nov-2011','%d-%M-%Y')
from table1
;
Output:
| STR_TO_DATE('26-NOV-2011','%D-%M-%Y') |
-----------------------------------------
| November, 26 2011 00:00:00+0000 |
SQLFIDDLE DEMO
I have a table with a date field, having human date in it like: '2008-01-08 19:23:32'
Now i have to copy this field plus some other fields of the same table to another table, but date needs to be in unix timestamp.
Is there any function in mysql which converts human date to unix timestamp inside query itself?
mysql> select unix_timestamp('2008-01-08 19:23:32');
+---------------------------------------+
| unix_timestamp('2008-01-08 19:23:32') |
+---------------------------------------+
| 1199849012 |
+---------------------------------------+
1 row in set (0.04 sec)
found here: http://www.epochconverter.com/
UNIX_TIMESTAMP() Should do the trick!
From MySQL Docs:
If called with no argument, returns a Unix timestamp (seconds since
'1970-01-01 00:00:00' UTC) as an unsigned integer. If UNIX_TIMESTAMP()
is called with a date argument, it returns the value of the argument
as seconds since '1970-01-01 00:00:00' UTC. date may be a DATE string,
a DATETIME string, a TIMESTAMP, or a number in the format YYMMDD or
YYYYMMDD. The server interprets date as a value in the current time
zone and converts it to an internal value in UTC.
mysql> SELECT UNIX_TIMESTAMP();
-> 1196440210
mysql> SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');
-> 1196440219
SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');
Yes.
SELECT UNIX_TIMESTAMP(column) FROM TABLE
Query:
SELECT UNIX_TIMESTAMP(TIMESTAMP(`opened`)) as timestamp_date, `opened` as datetime_type FROM `myterminal`
Outputs:
| timestamp_date | datetime_type
|------------------- |---------------------
| 1536602012 | 2018-09-10 14:53:32
| 1536603854 | 2018-09-10 15:24:14