MySql YYYY-MM-DDTHH:MM:SS to time - mysql

I have a table with a varchar column DateFrom that has this format:
2014-02-22T08:08:00
I want an sql that prints 08:08 and one that prints 22-02-2014 but i can't seem to get the time function to work.
What i'm trying to do is get all entries in DateFrom and print them as just time (HH:MM)
and the same with date.

Altough I think string functions are a better option in this case (like #hakre answered) and less cpu expensive, you can also achieve this goal using the STR_TO_DATE, DATE and TIME function.
SELECT
DATE(STR_TO_DATE('2014-02-22T08:08:00', '%Y-%m-%dT%H:%i:%s')),
TIME(STR_TO_DATE('2014-02-22T08:08:00', '%Y-%m-%dT%H:%i:%s'))

If you're not looking for date/time but for string functions, they are available here:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html
One string function that I think is useful for your substring operation is SUBSTRING. You can tell per the varchar column that you want a sub-string starting from a position for a certain length with it:
SUBSTRING(DateFrom FROM 1 FOR 8) AS DateName -- "2014-02-22"
SUBSTRING(DateFrom FROM 10 FOR 5) AS TimeName -- "08:08"
Use other string functions to concatenate parts in the order you need it.
Alternatively you can cast your varchar string in that format to a datetime type and then format as needed:
CAST(DateFrom AS datetime)
See the Mysql manual for more information about casting types and the date-time functions that are available:
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html
SQL Example:
SET #DateFrom = '2014-02-22T08:08:00';
SELECT DATE(CAST(#DateFrom AS datetime)); -- '2014-02-22'
SELECT TIME(CAST(#DateFrom AS datetime)); -- '08:08:00'

Related

MySQL equivalent of php's strtotime()

Is there a way to convert a string such as "-1 week" or "-5 minutes" into a datetime value in MySQL similar to php's extremely convenient strtotime() function?
I have a table that stores a human-readable time interval (such as "2 minutes") in one column and a datetime in another column.
I would like to select the rows where more than the amount of time specified in interval has elapsed since datetime.
MySQL doesn't have an equivalent of PHP's strtotime() in the sense that there is nothing that will automatically attempt to parse and determine the format of a date string using by assuming multiple formats.
What it does have is STR_TO_DATE(str,format) which requires you specify the format of your date, time or date + time string. It is the equivalent of PHP's date_create_from_format(format, str) function (though the format of the format parameter are different).
Here are some examples given from the MySQL documentation. They show a date being passed along with the format string that lets it know how the date string is to be interpreted:
SELECT STR_TO_DATE('01,5,2013','%d,%m,%Y');
SELECT STR_TO_DATE('May 1, 2013','%M %d,%Y');
Alternatively, you can cast a string to a date, time or datetime type, but they require a specific format (YYYY-MM-DD hh:mm:ss.fraction) for it to work:
SELECT CAST("2019-11-21" AS DATE);
If you deviate too far from that format it will make a few assumptions but could produce an incorrect date.

Check datatype of column after applying function MySQL

I want to know datatype of column after using a function.
SELECT timediff(date('2020-10-01 00:00:00'), date('2020-10-00 00:00:00'));
After using timediff function what datatype is it? I've looked at the doc to search for output datatype of a function https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html but could not find, Also it would be inefficient to memorize which function outputs what datatype, so I would like to perform an operation creating new column and know its exact datatype.
When I connect MySQL to python and check its datatype it is timedelta but is it also called timedelta in MySQL?
After using timediff function what datatype is it? I've looked at the doc to search for output datatype of a function but could not find.
Well, the documentation sure gives you the answer (emphasis mine):
TIMEDIFF() returns expr1 − expr2 expressed as a time value.
And further:
The result returned by TIMEDIFF() is limited to the range allowed for TIME values. Alternatively, you can use either of the functions TIMESTAMPDIFF() and UNIX_TIMESTAMP(), both of which return integers.
A timediff function will give you output in TIME type which is a type in mysql. However, to make sure for the output of the type you can cast it into the type like below,
select cast(timediff(date('2020-10-02 00:00:00'), date('2020-10-00 00:00:00')) as TIME);
To know more about TIME type click here. Know about CAST() click here.
You can add by following sample query,
ALTER TABLE `test`
ADD COLUMN `colTime` TIME NULL ;

Why i got null from this query

Following is my sql query kindly let me know why is it returning null
Select STR_TO_DATE ('11-APR-74','%e%b%Y')
OR
Select DATE_FORMAT ('11-APR-74','%e%b%Y')
From MySQL STR_TO_DATE function:
The server scans str attempting to match format to it. ... Scanning
starts at the beginning of str and fails if format is found not to
match.
This is why your first query fails: 11-APR-74 does not look like %e%b%Y, so date cannot be parsed. You should do instead
SELECT STR_TO_DATE ('11-APR-74','%e-%b-%Y')
From MySQL Date and Time types:
Although MySQL tries to interpret values in several formats, date
parts must always be given in year-month-day order (for example,
'98-09-04'), rather than in the month-day-year or day-month-year
orders commonly used elsewhere (for example, '09-04-98', '04-09-98').
This is why your second query fails: 74 is not a valid day of month, you should do instead
SELECT DATE_FORMAT ('74-APR-11','%e%b%Y')
Note, that DATE_FORMAT is usually used on DB values, not string literals as you do - to get an output different from the default one.
If you want to convert from string to date
Select STR_TO_DATE ('11-APR-74','%d-%b-%y')
use it like ::
Select STR_TO_DATE ('11-APR-74','%e-%b-%Y')
Because '%e%b%Y' format does not correspond to '11-APR-74' string value (as STR_TO_DATE function expects), and because '11-APR-74' value is of type CHAR, but not DATETIME (as DATE_FORMAT function expects).
If you want to reformat a date represented by a CHAR value, convert it from its original format to DATETIME first, and then convert it to a string of desired format:
SELECT DATE_FORMAT(STR_TO_DATE('11-APR-74','%e-%b-%Y'),'%e%b%Y');
By the way, you could strip dashes with a plain string sunction:
SELECT REPLACE('11-APR-74','-','');

MySQL: SQL Query to return Date from Date/Timestamp Column

In a table I have a stored string column "MM/DD/YYYY HH:MM:SS" and I'm looking for a query to return just the "MM/DD/YYYY" part.
Any ideas?
If the column type is char or varchar, then
SELECT LEFT(colname, 10)
will suffice.
If it's a datetime type, then try
SELECT DATE_FORMAT(colname , "%d/%m/%Y")
You should be using a MySQL DATETIME field instead of a string field, really. That would allow you to apply date and time functions, which help tremendously when dealing with temporal data.
In your case, since your data is a string type, and not in MySQL Isodate format (YYYY-MM-DD), you can work only using string functions like SUBSTRING() and specialisations thereof (LEFT, ...).
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
Date and Time functions overview
http://dev.mysql.com/doc/refman/5.5/en/string-functions.html
String functions
i think you can use substring !!!
SELECT SUBSTRING(TimeStamp,7,10);
if it is a string use SUBSRT() or LEFT() to extract required part, however it would be wise to have it stored as datatime type

Mysql time stamp queries

I have a column that uses time stamp. My question is I am a bit confused about how to make queries against it,how would I say
Where $time is after X date
Are queries made in local time or CUT?
When I just try to do where andthe post date /time I get an error because of the space and if I quote it I think it takes it as a string : /
What format do I use for the date in the WHERE clauss !?!?
You can use the normal logical comparisons, for example:
SELECT * FROM table WHERE date >= '2010-01-31'
SELECT * FROM table WHERE date >= '2010-01-31 12:01:01'
Time is generally in your current local time, but you can run the query "SELECT CURTIME()" to check. Also, make sure you have the year-month-date in the right order... that can cause issues.
The manual has more details:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
Assuming you talk about TIMESTAMP column type (not Unix timestamp) the format is either 'YYYY-MM-DD HH:MM:SS' (quoted) or YYYYMMDDHHMMSS
Atually, in the first case you can use any spearators you wish (or none), only numbers are taken into ccount