Select Date(String Date)<DATE(Text Column with Date) - mysql

I have a table with a column of the type text where I store various settings, including dates, using NOW() in the insert query.
Now I want to get all rows from this table where this column is before a specific date (e.g. 2012-09-19).
Comparing via DATE(date_column)<DATE(NOW()) is easy, but I don't know how to pass a specific date instead of NOW(), because date formats of the input string may differ.
Here's a sample code:
INSERT INTO table (date_column) VALUES (NOW())
And when selecting:
SELECT * FROM table WHERE DATE(date_column)<DATE('2012-09-19');
EDIT: The above code actually works. I was missing the quotes around the date initially.

Avoid placing functions around your column types (WHERE FUNCTION(col) ...). This decreases performance. You want to have a table that has domain integrity, meaning if you are storing a date, it is a date type.
To select records that match your date, simply encase your YYYY-MM-DD date in quotes:
SELECT * FROM table WHERE date_column < '2012-09-19'

If date_column truly is of type DATE, you don't need to run DATE() on it.
Also note that running DATE() on date_column will prevent MySQL from using any indexes, so you'll be doing a table scan.
Try:
WHERE date_column < '2012-09-19'

Related

Converting timestamp from one column of an SQL table to dates in another column of the same table

I'd like to convert unix timestamps from one column of an existing sql table to dates in another column of the same table. Something like this: take the values of column TIMESTAMP from every raw and convert it into a date and put it into column DATE.
I already figure out that the below expression converts the timestamps into dates:
SELECT FROM_UNIXTIME(TIMESTAMP) FROM MYTABLE
but I don't know how to plug this into a normal sql update expression.
Thanks for any advice!
W
Use the expression in the SET clause of the UPDATE query.
UPDATE MYTABLE
SET date = FROM_UNIXTIME(timestamp)

MYSQL: automatically extract the YEAR from the date input into another column

I have a table with a dateOfBirth column and a yearOfBirth column. The input only contains the DOB information. What I am looking for is to just insert DOB info into the db, and the mysql will auto update the year column field with the year extracted from the date inserted. Is it possible to be done?
Right now my MySQL query is just
INSERT INTO table
(name, dateOfBirth, yearOfBirth)
VALUES (?)
How should I change that?
It is possible to do this using a trigger, but I don't see any practical reason to do it. Instead, just leverage the YEAR() function if you want to present the year in a query:
SELECT
name,
dateOfBirth,
YEAR(dateOfBirth) AS yearOfBirth
FROM yourTable;
Storing the birthdates in a bona fide date column is the best design choice, and any derivative of that can be had using a function or cast.
Edit:
If your reason for wanting to do this is adding an index by year, you can instead just add an index on the dateOfBirth field:
CREATE INDEX idx ON yourTable (dateOfBirth)
If you wanted to query for a certain year, say 2018, you could try the following:
SELECT *
FROM yourTable
WHERE dateOfBirth >= '2018-01-01' AND dateOfBirth < '2019-01-01';
This in fact would be sargable, meaning that the index we created above could be used.

Use date in mysql database instead ID

How is smart to use date as primary value instead ID?
Where I can get better search mysql database performace:
to use timestamp: 1394319600
or to format date and use it as: 09032014
09032014 = 1394319600 = 9.Mart 2014
You likely should not be using a datetime type of data field as a primary key to begin with. I would suggest using autoincrementing integer field to guarantee uniqueness.
Now with regards to the datetime/timestamp field itself it is almost always better to use a native datetime or timestamp data type for these columns rather than string representations such a unix timestamps or other formatted date strings.
Why? Because when people put in timestamp data into their database base, they typically get to the point of wanting to run queries against that data. If you store your data in a non-native datetime format, you will typically need to convert it to such a format before you can use it in typical date/time functions that would be used in this sort of query. This usually means you lose the ability to leverage any index on the field for the query.
For example, say you wanted to run a query to see all records for the current day. With unix timestamp field that query may look like:
SELECT * FROM table
WHERE FROM_UNIX_TIMESTAMP(timestamp_field)
BETWEEN CONCAT(CURRENT_DATE(), ' 00:00:00') AND CONCAT(CURRENT_DATE(), ' 23:59:59')
whereas with a datetime/timestamp filed it would look like:
SELECT * FROM table
WHERE timestamp_field
BETWEEN CONCAT(CURRENT_DATE(), ' 00:00:00') AND CONCAT(CURRENT_DATE(), ' 23:59:59')
Here the simple requirement to use FROM_UNIX_TIMESTAMP() on the left hand side of the WHERE condition in the first query prevents use of an index since FROM_UNIX_TIMESTAMP(timestamp_field) does not exist in memory like timestamp_field would if properly indexed. This means you now need to do a full table scan to execute that query. If you have a large table, this could be very problematic.

records from table where date between two years

I need to get the record from one table where date between June-30-2011 and June-30-2012.
the problem is that the result is just only display the records of year 2012 although the table has records for year 2011.
below is my code
SELECT * FROM tbl_name where date between '06/30/2011' and '06/30/2012'
you need to convert it bact to date using STR_TO_DATE, eg
SELECT *
FROM tbl_name
where STR_TO_DATE(date, '%m/%d/%Y') between '2011-06-31' and '2012-06-31'
STR_TO_DATE
It is not good to store Dates as string on database because as you see it is hard to search for it, you need some extra functions to convert it back to date and to which I think it kills the index.
If you have time or privilege to alter, fix the values and change it to DateTime data type.

How can I convert datetime to date, truncating the times, leaving me the dates?

I have a field that's in datetime format when date would be better and more consistent with the rest of the database, so I want to convert. The time part is all 00:00:00 anyway.
How can I do this in MySQL?
Thanks.
If you want this in a SELECT-Statement, just use the DATE Operator:
SELECT DATE(`yourfield`) FROM `yourtable`;
If you want to change the table structurally, just change the datatype to DATE (of course only do this if this doesn't affect applications depending on this field).
ALTER TABLE `yourtable` CHANGE `yourfield` `yourfield` DATE;
Both will eliminate the time part.
Cast it as a DATE:
select DATE(my_date_time)
That will truncate the time from it, leaving only the date part.
when using change we have to repeat the same name again for the field.now we can use MODIFY to alter the filed type.
ALTER TABLE `yourtable` MODIFY `yourfield` DATE;