how to store a date in a mysql database? - mysql

If I have a date such as mm/dd/yyyy. How can I get mysql to actually store the date in that format. Is this possible or do have to store it in the form of yyyy/mm/dd and convert it later?
What I want to do is insert a date in the mm/dd/yyyy format but the database will not allow it. It wants yyyy/mm/dd

What is your reason for doing this? I can't see any reasonable use for it.
You cannot change the way MySQL stores dates, no. But you can of course format them when reading/writing them. You can do it in SQL query like this:
For example you can use STR_TO_DATE function to format the date when inserting it:
INSERT INTO mytable (mydate) VALUES (STR_TO_DATE('12/31/2009', '%m/%d/%Y'))
And vice versa:
SELECT DATE_FORMAT(mydate, '%m/%d/%Y') FROM mytable /* returns 12/31/2009 */
But as FactalizeR pointed out, it is not a good practice to do it in the query and it should be moved to script, like this (considering you are using PHP).
$date = '12/31/2009';
$date = date('Y-m-d', strtotime($date));
mysql_query("INSERT INTO mytable (mydate) VALUES ({$date})");
And vice versa
$date = mysql_result(mysql_query("SELECT mydate FROM mytable"), 0, 0);
$date = date('m/d/Y', strtotime($date)); //returns 12/31/2009

There is a built-in DATE type in MySQL. It allows to store a date. And later in your programming language you extract this date and convert to whatever format you like. Also, conversion can be done directly in MySQL via DATE_FORMAT(date,format) function.

MySQL DATE fields must store the date in YYYY-MM-DD (including dashes) format. If you attempt to store it any other way you will have problems with date comparisons and ordering.
Conversion later is a trivial task. Is there a compelling reason why you are trying to avoid doing this? What other technology are you using to talk to the MySQL database? (i.e. PHP, C# etc...)

I think you're mixing up what you are storing - the date itself - with how that date is subsequently referred to.
Why do you want to store in a particular format? Do you want to insert in that format? In which case you might be able to get away with it, depending on the localisation of your install, but otherwise convert - insert would be the way to do it (depending, of course, on how you're inserting).
Are you creating a SQL command from strings?

Related

How to filter by date=today in sqlite3 script

Here i have a piece of code which intention is to execute a query against a database (mainly in sqlite3 language) and after that, export the resulting table to an auto-named csv file.
set fch=%date:~11,4%%date:~8,2%%date:~5,2%%time:~0,2%%time:~3,2%%time:~6,2%
sqlite3 -header -csv -cmd ".separator ','" C:\ProgramData\PROISER\ISASPSUS\datastore\dsfile.db "select * from analysis;" > holaa%fch%.xlsx
My question is how to insert into the quoted query a filter to only show the data that matches the actual date (date is an actual field of the table that is exported)
Use strftime(), one of the sqlite date and time functions, to format the current date ('now') in the same format as used in the database. Use that in a comparison in your query's WHERE clause.
I suggest converting all existing values to standard ISO-8601 formats instead of making up your own, though. It'll make things easier, both for interoperability with other software and so you can manipulate the dates with sqlite functions and other date/time builtins.
For example, if you used the YYYY-MM-DD format, you could do
SELECT * FROM foo WHERE date_added = CURRENT_DATE
and avoid the strftime() completely.

Remove unnecessary string and Update date format in MySql

Basically my date in MySql table looks like this
2001-04-16
The format is actually day-month-year. Unfortunately 20 is added to the date. So finally i should update this date. This should become
01-04-2016
How to update my all date format in my table
Don't change this in the database, change it when you need to display it. The ISO date format is what's used internally and should be kept that way, it's the most native, efficient format, and it sorts correctly.
You can select them out:
SELECT DATE_FORMAT(date_column, '%d-%m-%Y') FROM table_name
Using the DATE_FORMAT() function.
Even better is to do this in your application layer, whatever that is, using a date formatting function tuned to the user's locale.
If you've got a bunch of bad data in your database you need to convert, you can re-write it with the DATE_FORMAT() function to strip out the mistakes.
For example:
UPDATE table_name SET date_column=DATE_FORMAT('20%d-%m-%y', date_column)
For mysql, MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. so you can fetch date in whatever format you want from database, please see https://dev.mysql.com/doc/refman/5.6/en/datetime.html for more info.
you can use DATE_FORMAT() function :
SELECT DATE_FORMAT('%d-%m-%Y', date) as desired_date FROM table_name where 1

Convert varchar string data to time format in mysql

I need to convert varchar string data to time format in mysql.
For example,
I have a varchar column in table which stores time. The accepted values should be like 9:30 AM or 1200 PM. But currently it has either blank values or it has values like 9.30am or 12:00
There are many records like this, so cannot update manually.
Ithere any work around or function or procedure to do so?
please help.
Thanks
You can use the STR_TO_DATE() MySQL function to convert any string to a date.
Additionally you can use TIME() to extract the time portion of a datetime. A combination of both function is used to convert an arbitrary date string to a datetime and then you can extract the time portion from it as a valid MySQL TIME.
By default MySQL functions follow standard format but custom format can be specified and if your values don't use the international formats you'll need to check with the documentation and provide the format your system is using.

Format date in mysql query that uses cast

I've got this as the select part of my query:
SELECT cast(cast(exp_channel_titles.edit_date as char(14)) as datetime) AS Edit_Date
That takes data from a db in this format 20130501092128 and returns it in this format 2013-05-01 09:21:28
I can only assume it is some kind of magic as i don't fully understand how this works tbh.
But, i need to change the format of the date that it spits out to this format: %d/%m/%Y %k:%i:%s
I can honestly say i have no idea how to do this in that query, i've tried adding it as a param to datetime (is that even a mysql function?!?) but no joy and many other poor attempts that i wont go into.
If anyone can help, i'd be hugely grateful!
MySql automatically converts 20130501092128 to a date and time field, even if it is a VARCHAR or a INT, and you can just use this:
SELECT DATE_FORMAT(exp_channel_titles.edit_date, '%d/%m/%Y %k:%i:%s')
Please see fiddle here.
You can change output format using DATE_FORMAT() function from MySQL. Here is the documentation post about it.
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
You can change the output format into whatever format you want, but if you recieve that data into an application, modifies it and return that data to server (editing a row for example). Remember to reformat it into a valid date for MySQL.
If you dont know how to do it, just have to do this into your query:
SELECT DATE_FORMAT(cast(cast(exp_channel_titles.edit_date as char(14))
as datetime), '%e/%m/%Y %k:%i:%s') AS Edit_Date

MS Access SELECT and WHERE

I am having trouble getting records out of a database for a certain date. This this the SQL command I have at the moment:
SELECT * FROM ict1 WHERE date='26/03/1992'
It should return one record from my database. "date" is a column and one of the records has '26/03/1992' as the value. I have tested the db connection and I can use:
SELECT * from ict1
So I know it's not that. It's probably just the SQL syntax being a lot different, I'm used to MySQL :#)
Should probably mention that I'm using .NET with an OleDbConnection.
Thanks.
Usually dates need to be formatted as for access like the following
Select * from ict1 where date= #03/26/1992#
The # denotes a date in access.
Be very careful with dates formatted as '10/03/1992' as it could mean 10th of march or 3rd of october depending on where you are.
Also #1992/03/26# also works
You may want to use a date comparison function instead of date = #xxxx/xx/xx#. Date comparisons do not yield expected results because of formatting and data type issues. In SQL Server your date might be stored as a datetime or a date data type. You need to make sure you are comparing things in the same type and format.
DateDiff ("d", #1992/03/26#, date) = 0
Use the date as YYYY/MM/DD format:
SELECT * FROM ict1 WHERE date='1992/03/26'
SELECT * FROM ict1 WHERE date=#1992/03/26#