MySQL string to date brings all rows with the same year - mysql

I have a table that the data is a text in the format: 05/07/2019 (dd/mm/yyyy).
When I try to convert it to a date with
str_to_date(date_field,'%d/%m/%y')
I get the response in the format 2020-06-21 (yyyy-mm-dd). But the problem is that in the result all of the years are 2020 though this isn't the actual sitatuion.
Is there a way to solve this or do I need to recreate the DB with the date to be in date format?

If you are using the format dd/mm/yyyy you need to use capital Y
%Y Year as a numeric, 4-digit value
%y Year as a numeric, 2-digit value
So, in your case, you should use str_to_date(date_field,'%d/%m/%Y') because when you use y, it is taking only the first two digits for the year.
And, as I said in my comment (and Tim also said) if you are storing dates in a RDBMS, you should use ALWAYS proper date/datetime data types.

Related

Converting string column to timestamp in BigQuery?

I am trying to convert a column in a BQ table to timestamp. I have two string columns, one for utc_hour (string 0-23) and utc_day (string yyyymmdd) imported from a public data source. I merged the two columns to produce a string column, utc_timestamp, with strings like this - "20171208 500" .
I need to convert that string into timestamp, and when I use
TIMESTAMP(utc_timestamp)
I get the error message
Invalid timestamp: '20171208 500'
I tried using dataprep, which also could not convert that string to a timestamp.
How can I convert this format to a timestamp?
Try to parse with %Y%m%d%k%M format.
PARSE_TIMESTAMP("%Y%m%d%k%M", utc_timestamp)
This question is already answered but in case someone else visits here with his/her own unique timestamp format (in which case the accepted answer might not work), you need to follow the notations on this documentation page
For eg, in my case values were like this 2020-05-11-00:00:00. So, I went to the above-mentioned page and found that the format string that I needed would be something like %Y-%m-%d-%H:%M:%S.
I will put the description for the ones that I used in my format string:
%d The day of the month as a decimal number (01-31).
%H The hour (24-hour clock) as a decimal number (00-23).
%M The minute as a decimal number (00-59).
%m The month as a decimal number (01-12).
%S The second as a decimal number (00-60).
%Y The year with century as a decimal number.
The :s and the -s are quite self-explanatory.

MySQL timestamp format and datediff

Hi I'm writing queries for MySQL, and now my database has a column containing the timestamp in this format: 7/14/2015 7:57:49 AM, but I need to use the DATEDIFF function, so how can I convert the timestamp into the format like: 2015-7-14 (or 2015-07-14, I'm not sure which one is correct; just the date)?
This should convert your string to just the date in a date format, then you can use DATEDIFF on the date fields in question:
SELECT STR_TO_DATE(LEFT(t,LOCATE(' ',t) - 1), '%m/%d/%Y') FROM my_table;
The LEFT function will take the substring to the left of the space, which is just your date, then STR_TO_DATE will convert that substring to a date the system can use.
(Not knowing your field and table names, I used t and my_table.)
You don't need to. The way MySQL displays timestamps has nothing to do with the way they're stored internally; as long as it's TYPE TIMESTAMP or some compatible type, the DATEDIFF() function will know what to do with it.
TIMESTAMPs are actually stored as a really huge integer representing (I think) milliseconds from Midnight UTC, January 1st, 1970. The display format is determined by a system global variable, and has nothing to do with the actual value.
Converting from a string to a DATETIME or TIMESTAMP is actually also fairly straightforward using the STR_TO_DATE() function; in your case the format string would be something like
STR_TO_DATE('%c/%e/%Y %l:%i:%s %p', datecol)
although you might have to experiment a bit to make it work reliably.

Converting MySQL dates (from what I think are seconds)

I am pretty new to MySQL, and am looking at a table (through a query) that has three date fields. However, they appear to be in seconds (but I could be wrong), but ultimately, I need to convert them to a valid date/time.
The numbers are:
1366272682
1366239600
1366272682
I think one of these dates is 18th April 2013.
Can someone let me know how I can convert them within the query (or indeed if I am right).
Thank you.
Those "numbers" are actually Unix Timestamps. Use FROM_UNIXTIME() to convert them into human friendly formats:
Returns a representation of the unix_timestamp argument as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context.
For example:
SELECT FROM_UNIXTIME(1366272682, '%e%D %M %Y')

Should I use SMALLINT instead of DATETIME in MySQL to save space?

I'm trying to decide whether to use SMALLINT(4) vs DATETIME to represent a year in MYSQL.
My table needs to have the model year, but I don't want to waste space with month or date like this: 20xx-01-01 00:00:00.
So I could save space representing a year like this:
year SMALLINT(4);
If it's not a big savings, I'm fine using DATETIME. If using 4 digit year is good for saving space, then how do I $_POST a four-digit year as a DATETIME?
There is year data type in MySQL - http://dev.mysql.com/doc/refman/5.1/en/year.html
The YEAR type is a 1-byte type used to represent year values. It can be declared as YEAR(4) or YEAR(2) to specify a display width of four or two characters. The default is four characters if no width is given.
NOTE:
The YEAR(2) data type has certain issues that you should consider before choosing to use it. As of MySQL 5.1.65, YEAR(2) is deprecated.
Possible range : 1901 to 2155, or 0000
You'd use mktime(date(Y), 0, 0, 0, 0, 0)
You can use DATE data type (uses 3 bytes) instead of DATETIME (8 bytes). You still have to store the month and day, but you don't need to store the time.
MySQL DATE type allocates 3 bytes, while SMALLINT is 2-byte integer. So, it's eager savings, if any. On the other hand, every time you want to compare year-as-integer column with other date column, you need to do an extra work.
To convert integer input value to MySQL date you can use something like this:
$year = isset($_POST['year']) ? intval($_POST['year']) : 1970;
// ISO 8601 standard date representation is YYYY-MM-DD
mysqli_query($link, "INSERT INTO `mytable`(`datefield`) VALUES('{$year}-01-01')");

Converting date format using str_to_date

In one of my Mysql database table the dates are stored in the format 31-Jan-05.
I'm trying to convert this format to 2005-01-31 before inserting them in other tables.
I've tried in this way str_to_date(exam_date, '%d%M%Y'), but i encounter the following error
Incorrect datetime value: '31-Jan-05' for function str_to_time
Can't i change the date format from 31-Jan-05 to 2005-01-31 using str_to_date?
Thanks in advance.
Yes. But you have two problems.
The second parameter is the current date format. (i.e. of the string)
You need to have the proper format (i.e. %b instead of %M).
Read the docs the for str_to_date().
str_to_date(exam_date, '%d-%b-%y')
Note: If you don't have a zero padded day, then you need to use %e instead of %d.