MySQL BETWEEN for dates saved as string - mysql

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

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

How to convert a string to date and extract values in Access query

I'm using Access DB 2007 - 2010; I've tried to import many CSV files but the timestamp column keeps failing to import correctly.
So I linked all of the CSV's to an Access DB and I'm trying to query all of the tables.
I'm trying to extract the year and day of the year from the time stamp (which is currently a string)
I'm trying to combine the Format with datepart functions and it keeps failing. (it just says error in the table)
The format function by itself works but I can't combine it with anything.
I'm basically trying to do this:
select datepart("y", Format(gmt, "dd-mmm-yyyy hh:nn:ss")) as DOY from Table1;
but it fails. I've also tried CDate and DateValue in different combinations but it all fails.
Does anyone know how to get this to work?
UPDATE
The format function isn't doing anything. The text remains the same no matter how I try to format it.
Here's a datetime sample: 05-Dec-2008 13:40:01.955
Access can't cope with the milliseconds in your date strings.
Use Left() to exclude them and feed the resulting substring to CDate().
SELECT CDate(Left(gmt, 20)) AS date_from_string
FROM Table1;
Once you have a valid Date/Time value, you can use Year(<Date/Time value>) or DatePart("yyyy", <Date/Time value>) to extract the year. And DatePart("y", <Date/Time value>) will give you the day of the year.
Just solve this issue, here is my code for your reference:
update tablename
set date=cdate(format(left(gmt,4)&"-"&right(gmt,2),"yyyy-mm"))

UNIX_TIMESTAMP outputting NULL in MySQL?

I've got a table setup which has populated data. In column "date", I have dates in the following format:
yyyymmdd i.e. 20131110
I have created a new field and called it newdate with the text format.
Then, I open up the SQL window and put the following in
UPDATE wl_daily
SET
newdate = UNIX_TIMESTAMP(date)
For some reason, it is running correctly, however it only outputs NULL to all the rows. Also, the column name is blank for some reason
Any suggestions?
That's because your field in a string and you're trying to add timestamp to it which is not a string. You need to use a valid datetime field like timestamp for this to work.
Advice: don't store dates and times as strings. Store them in their native format. It makes working with dates and times much easier.
While John Cronde's answer is correct - it doesnt help your situtation
UNIX_TIMESTAMP(STR_TO_DATE(`date`, '%Y%m%d'))
will do the conversion for example
SELECT UNIX_TIMESTAMP(STR_TO_DATE('20131111', '%Y%m%d'))
returns
unix_timestamp(STR_TO_DATE('20131111', '%Y%m%d'))
---------------------------------------------------
1384128000
You should only use this to convert your columns to the date specific columns. Converting each time you need a number will add load and slow down the query if used in production

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

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.

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#