SQL: Convert date (26.11.17) to timestamp (1516184035) - mysql

Is there any way to convert a normal european date to a timestamp-value in mySQL?
I have a mysql database with a column date (varchar(64)) containing dates in european format like 01.12.15. For this I need a update-sql-query that converts all lines into timestamps.
Thanks for your help!

Use STR_TO_DATE to convert your date string 26.11.17 to a bona fide date. Then call UNIX_TIMESTAMP on that date to get the UNIX timestamp.
SELECT
UNIX_TIMESTAMP(STR_TO_DATE('26.11.17', '%d.%m.%y')) AS output
FROM dual;
1511650800
Demo

Related

How to extract date and hour only from MySQL?

I have this column in MySQL table:
Date
2022-07-13 07:01:20
I want to extract this column to get this (the date and hour only):
2022-07-13 07
Any help is appreciated thanks!
This date column is in the form of timestamp
Apparently timestamp is a string so i can use substring.
To select the date and hour from that format first you have to take the substring using SUBSTRING()
the convert to datetime format using STR_TO_DATE()
you can take the specific date using SELECT CONVERT(date, your_date_time);
then extract the hour to add it in there SELECT HOUR()
you can then convert back to a string or use it that way

MySQL - How to convert date to the month has leading zeros?

So I'm trying to insert dates into a table and the date is in this format:
8/3/2021
However I want to add a leading 0 before the month and day so the date shows 08/03/2021. Also I want to add it as a string concatenated with another string so test123-08/03/2021
If you really store date in that format then you may try this:
SELECT
DATE_FORMAT(STR_TO_DATE(date_col_string,'%d/%m/%Y'),'%d/%m/%Y') as 'zero-padded',
CONCAT(string_val,'-',DATE_FORMAT(STR_TO_DATE(date_col_string,'%d/%m/%Y'),'%d/%m/%Y')) as 'concatenated'
FROM mytable;
Use STR_TO_DATE() function to change the date value to standard MySQL date format of YYYY-MM-DD then use DATE_FORMAT() function to display the date value as per your desired output. The second operation is adding CONCAT() function on the converted date with your selected string. I'm assuming that your date value is d/m/y, because as #Stu mentioned in the comment, since you're not storing as MySQL standard date format, that means 8/3/2021 can be either d/m/y or m/d/y. With a standard date format value, the query would be shorter:
SELECT
DATE_FORMAT(date_col,'%d/%m/%Y') as 'zero-padded',
CONCAT(string_val,'-',DATE_FORMAT(date_col,'%d/%m/%Y')) as 'concatenated'
FROM mytable;
Demo fiddle
You should be inserting your source dates into a proper date or datetime column. Then, to view your dates in the format you want, use the DATE_FORMAT() function with the appropriate format mask:
SELECT DATE_FORMAT(date_col, '%d/%m/%Y') AS date_out
FROM yourTable;

Convert MySQL date time 24 hour format to UNIX timestamp

I'm storing date time in 04-09-2019 10:31:AM(4 September 2019 10:31 AM) in this format in MySQL table.
May I know how to convert this format to unixtime stamp in sql query
First you need to convert this into a proper mysql datetime using str_to_date() function.
str_to_date(dd, '%d-%m-%Y %h:%i:%p')
Then after getting the correct datetime value, use unix_timestamp() function to convert it to unix timestamp.
select
unix_timestamp(str_to_date(dd, '%d-%m-%Y %h:%i:%p'))
from test
try this dbfiddle.

Selecting date range MySQL with date_format

I've got an issue selecting a date range with MySQL.
SELECT MvtDate,date_format(MvtDate,'%d-%m-%Y')
FROM (`immmvt`)
WHERE date_format(MvtDate,'%d-%m-%Y') BETWEEN '01-01-2010' AND '02-01-2010'
mvtDate type is date like 2010-01-01 00:00:00.
When I run the query, the result works for the days and the months but it also show me other result from other years.
Like 01-01-2011 etc.
You should use STR_TO_DATE since you want to convert string back to date
SELECT MvtDate, date_format(MvtDate,'%d-%m-%Y')
FROM `immmvt`
WHERE MvtDate BETWEEN STR_TO_DATE('01-01-2010','%d-%m-%Y') AND
STR_TO_DATE('02-01-2010','%d-%m-%Y')
FYI: DATE_FORMAT() converts date to formatted string representation.
STR_TO_DATE() converts formatted string back to date
SELECT MvtDate,date_format(MvtDate,'%d-%m-%Y')
FROM (`immmvt`)
WHERE date_format(MvtDate,'%d-%m-%Y') IN ('01-01-2010', '02-01-2010')

convert irregular date in mysql

I am working with a mysql database that somebody else created. I have a table with two different date fields one is called eventDate and is in the format YYYY-MM-DD and the other is called creationDate and is in the format M/D/YYYY H:MM:SS AM/PM
What I want to do is query the table and return results where the eventDate is the same as the creationDate but how do I convert the creationDate into YYYY-MM-DD format?
Try STR_TO_DATE() and a format matching the one used by your creationDate strings:
SELECT
…
WHERE DATE(STR_TO_DATE(creationDate, '%c/%e/%Y %r')) = STR_TO_DATE(eventDate, '%Y-%m-%d')
Perhaps MySQL would be able to convert YYYY-MM-DD strings to dates implicitly, so the second STR_TO_DATE() call might be unnecessary.