How to extract date and hour only from MySQL? - 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

Related

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;

Date conversion query in SQL using STR_TO_DATE

In my table, there is a date column i.e. ORDER DATE which is in dd-mm-yyyy format, and the datatype of this date column is text. Now, I want to convert the format from yyyy-mm-dd. But the date format is not changing at all. Trying to use STR_TO_DATE but the year in the dates are coming as 2020 only. Not sure how to fix it or if there is a better way to convert the date in MySQL.
For example:
SELECT STR_TO_DATE('14-09-2017','%d-%m-%y') AS DATE;
Result coming as 2020-09-14
Or suppose
SELECT STR_TO_DATE('20-MAR-2018','%d-%b-%y') AS DATE;
Result coming as 2020-03-20
day and month are coming properly but the year is not coming. Kindly help how to get convert the date format correctly.
Try to change your command like this below and you will get the answer:
SELECT STR_TO_DATE('14-09-2017','%d-%m-%Y') AS DATE;
Refer to the documentation for more info about the commands.

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

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

query between dates returns rows from previous months too

I have the following where clause
AND DATE_FORMAT( l.created_on, "%d/%m/%Y" ) BETWEEN '06/02/2013' AND '07/02/2013'
where created_on is a timestamp.
Now for some reason the query returns rows from previous months as well. anyone knows why?
NOTE :
I need the date to be in that specific format
Mysql string date format follows pattern yyyy-mm-dd. Do not convert to dates if you have timestamps, just compare the timestamps.
WHERE l.created_on
BETWEEN UNIX_TIMESTAMP('2012/02/06') AND UNIX_TIMESTAMP('2013/02/07')
if created_on is already a date (datatype),
you can directly query it using,
WHERE created_on BETWEEN '2013-02-06' AND '2013-02-07'
but if date is a string, use STR_TO_DATE
WHERE STR_TO_DATE(l.created_on, '%d-%m-%Y') BETWEEN '2013-02-06' AND '2013-02-07'
UPDATE 1
since you don't want to change the format of your inputted date, then you need to format it using STR_TO_DATE
WHERE l.created_on BETWEEN STR_TO_DATE('06/02/2013','%d/%m/%Y') AND
STR_TO_DATE('07/02/2013','%d/%m/%Y')

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')