How to use MYSQL on dates formatted as MM/dd/yy - mysql

I have a database full of dates that are in the format: MM/dd/yy. For example, today's date (November 4, 2011) is saved in the database as: 11/04/11. I'm having trouble with date ranges since my dates aren't formatted as yyyy/MM/dd (example: 2011/11/04). I don't have a way to change the way the database is populated, so I need to account for the date formatting differences within my MYSQL queries (via VB.NET). Here is my query (it doesn't work well because the dates are in the incorrect format):
SELECT CMP_DATE FROM my_data WHERE OBJ_DATE >= '1994/01/01' AND CMP_DATE <= '2011/11/04'
Is there anyway to reformat the dates within the query? Thank you.

The function you need is STR_TO_DATE:
SELECT CMP_DATE
FROM my_data
WHERE STR_TO_DATE(OBJ_DATE, '%d/%m/%y') >= '1994-01-01'
AND STR_TO_DATE(CMP_DATE, '%d/%m/%y') <= '2011-11-04'
#Piskvor has a good point, too, you should consider interpreting the 2-digit year yourself, instead of letting mysql perform some arbitrary conversion.

Related

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

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.

MySQL BETWEEN for dates saved as string

Here's the thing - I'm saving date in database as string in format dd/mm/yyyy. I want to get rows in which date is between two dates - let's say 11/07/2009 and 29/08/2014, how to do that?
I tried
SELECT * FROM attr WHERE time_added between '11/7/2009' AND '29/8/2014'
but it's not working correctly. Any great would be great?
First of all, it is recommended to use the MySQL's DATE type for dates, selecting the date range would be easy and efficient. But if you have your own reason to use string type (like you are working with a specific calender and you don't have the converter), then you should consider followings:
you told that you are using the dd/mm/yyyy format for dates but in your code you wrote 11/7/2009 which should be 11/07/2009
In order to select range you should save your date like yyyy/mm/dd, specially when you put index on this filed, it will be high performance.
You need not to save format charterers like '/' in database. you can format the output later and show the date in any order and format you want.
As the result I offer you the following solution:
Use the YYYYMMDD format to save the date. the select query will be something like:
SELECT *, DATE_FORMAT(time_added, '%d/%m/%Y') AS time_added2 FROM attr
WHERE time_added between '20090711' AND '20140829';
As and alternative if you can not change the database, then the following query will work on the existing database (date saved in dd/mm/yyyy format):
SELECT * FROM attr WHERE
CONCAT(SUBSTR(time_added, 7, 4), SUBSTR(time_added, 4, 2), SUBSTR(time_added, 1, 2))
BETWEEN '20090711' AND '20140829';

SQL query to get records from same day

I wish to find all records of the current day.
I have a field Date of type DATE.
If I use
WHERE `Date` = '2011-04-07'
it works
but if I use:
WHERE `Date`='CURDATE()'
or
WHERE `Date`='NOW()'
it does not return any results (when there actually are some).
How do I get the current date in the right format to use it in my SQL query?
I am using MySQL
And the date was originally entered in the database using NOW().
Use
WHERE `Date`=CURDATE()
The quotes (') are used to wrap up a string (text).
Edit: I can see now that you say the value was stored with NOW() : it probably includes a time element too. Will update answer imminently..
This will compare the date part of the Date field to today's date:
WHERE DATE(`Date`)=CURDATE()

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#