records from table where date between two years - mysql

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.

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)

Convert 2021-1-01 to 2021-01-01 in SQL (YYYY-M-DD TO YYYY-MM-DD)

As title.
There is a way to convert a date from YYYY-M-DD to YYYY-MM-DD?
In db I have 2021-1-01 but I need to get 2021-01-01.
TY
EDIT
I have a value 2021-1-01 and i need to insert it in a db like date 2021-01-01, maybe before I not was clear.
If you have '2021-1-01', then you do not have a date. You have a string. That is a problem with your data model. You should fix the data to store dates using appropriate types -- which are not strings.
MySQL is pretty smart about converting dates, so you can just use:
select date(string_as_date_col)
You can change the type of the column in the table using:
alter table t modify column string_as_date_column date
Here is a db<>fiddle.

Changing Date Format in SQL Table

I currently have a Release_Date(Date) in my Songs table. I been trying to change the date format. The current format is yyyy-mm-dd. I want mm/dd/yyyy.
Error: Invalid Date value.
Release_Date is stored in the database as a Date, not as a string, so you don't need to call the str_to_date function. You are getting an error because you are calling the str_to_date function on something that is already a date, not a string.
Furthermore, as it is a date, you can't update that field to a string value. You would have to create a new column defined as a string and store the date there.
However, it is highly advantageous to keep the dates stored as Date fields, because comparisons, sorting, and the various date functions will all work as they should.
So if you want to use the date in a different format, you would just use DATE_FORMAT(Release_Date,'%m/%d/%Y') whenever you access it, and leave the field as a native date, as in
SELECT DATE_FORMAT(Release_Date,'%m/%d/%Y') FROM Songs WHERE Release_DATE IS NOT NULL;
It is not possible to "update" the internal format of a MySQL date. If you want to display your text as mm/dd/yyyy, then you should only need a single call to DATE_FORMAT, e.g.
SELECT DATE_FORMAT('2019-07-29', '%m/%d/%y')
which prints:
07/29/19
As Tim suggests, you don't need to change the existing date format & value but if you insists, you could add another column - datatype VARCHAR - in the table and then update the column according to your desired date format. Steps are below:
create new column
ALTER TABLE songs
ADD COLUMN `rls_date` VARCHAR(50) AFTER `release_date`;
Update new column with desired date format
UPDATE songs SET `rls_date`=DATE_FORMAT(`Release_Date`,'%m/%d/%Y');
Just remember, by doing this, you can't expect the column to identify any date format related function outright. Lets say you run a query like this SELECT * FROM songs WHERE rls_date=CURDATE(); won't work.

Display Time in select query - Mysql

I got an existing Mysql table with one of the columns as time int(10)
The field has many records like
1455307434
1455307760
Is it a date time, encrypted.
What should be the select Query, so it should display an actual date.
FROM_UNIXTIME()
SELECT FROM_UNIXTIME(mycolumn)
FROM mytable

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

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'