How to add 1 year to date stored as varchar? - mysql

How can add 1 year to a date value stored in the DB as varchar (format: "2011.03")?
I'm trying with this, but returns NULL :(
DATE_FORMAT(DATE_ADD(STR_TO_DATE('2011.03', '%Y.%m'), INTERVAL 1 YEAR),'%Y.%m')
Thank you very much!
edit.: this is the query i want to use in:
SELECT DISTINCT column FROM table WHERE column BETWEEN '2011.03' AND DATE_FORMAT(DATE_ADD(STR_TO_DATE('2011.03', '%Y.%m'), INTERVAL 1 YEAR),'%Y.%m') ORDER BY column DESC
("2011.03" is a parameter value and comes outside)

What you are trying is giving correct result
Try :
SELECT DATE_FORMAT(DATE_ADD(STR_TO_DATE('2011.03', '%Y.%m'), INTERVAL 1 YEAR),'%Y.%m')
it's giving 2012.03.
Edit:
You can use below trick to make it workable in mysql version > 5.0
Add month with date because in Mysql version > 5.0 str_to_date() sometimes would return NULL if the %D format specifier was not the last specifier in the format string input.
Try below:
SELECT DATE_FORMAT(DATE_ADD(STR_TO_DATE('2011.03.01', '%Y.%m.%d'), INTERVAL 1 YEAR),'%Y.%m')

Related

get recent most Row specific to date in mysql

I have a table
id |value |date
-------------------
1 |2.8 |28-3-14
2 |2.9 |28-7-14
3 |3.9 |20-1-14
in this table i need to get the value of 21-3-14.
but if value or object is not present for that then query get output of 20-1-14 directly without one by one search object by minus date by 1 day.
if any one know about this please give me suggestion.
You just need to sort by date
SELECT value FROM table WHERE date<='21-3-14' ORDER BY date DESC LIMIT 1;
Based on your table it should print:
2.8
Assuming the date 21-3-14 wasn't there, it should print:
3.9
try this,
SELECT
*
FROM
<tablename>
WHERE
STR_TO_DATE(`date`,'%d-%m-%y') <= STR_TO_DATE('YOUR_DATE','%d-%m-%y')
ORDER BY
`date` DESC < LIMIT 1 >
It is recommended to store date in date format i.e. < yyyy-mm-dd >
you may refer,
PHP mysql insert date format

SQL Date_Format Month Number to Month Name

I have an issue in regards to trying to run a sql statement that returns the values of a column called month(of which I have defined as a varchar type, but only has integer values 1-12) as the associated month name. So, for example, the query would return a value of 1 as january. The issue I have is I am trying to use date_format
select date_format(month,'%M')from db.table name
but the values return as null. I was informed that the month values have to be a 'date' type in order for date_format to work. However, the values in this column 'month' are simply integers. So I run into the issue of not being able to assign the date type to the month columns because they're just integers and not correct format for dates? How could I take these single integers and return the month then?
Syntax
DATE_FORMAT(date,format)
Requires date as first param
Check out MySQL date function here:
http://www.w3schools.com/sql/sql_dates.asp
For this you can use this,
SELECT col as MonthNumber,
MONTHNAME(STR_TO_DATE(col, '%m')) as MonthName
FROM table_name
WHERE col <= 12

How to select latest date from database in MySQL

I have list of date in MySQL in the format of "MM-DD-YYYY" and When I was trying to fetch the latest date from table it just return the last date of a Year like 12-01-2014 instead of return latest date 03-16-2016.
Payment history table:
to_date
03-16-2016
12-01-2014
11-07-2014
10-03-2014
01-09-2014
I used following query:
SELECT MAX(to_date) FROM paymenthistory WHERE empid=59;
Result : 12-01-2014
Related post: Get the latest date from grouped MySQL data
Thanks in advance
You're working with strings, not native dates, so you're getting the maximum date.
Either convert those strings to ACTUAL mysql date/datetime values, or you'll have to go with ugly hacks, like
SELECT MAX(STR_TO_DATE(to_date, '%m-%d-%Y'))
and performance will be massively bad. MySQL's native date format is yyyy-mm-dd hh:mm:ss, which is a natural "most significant first" format. If your date strings were formatted like that, then even a max(string) would work.
It sounds like your date column is actually a VARCHAR format since it is seeing 12-01-2014 as the last date which is only true if stored as a VARCHAR.
Be sure your to_date column is a DATE type.
have you tried this?
SELECT TOP 1 * FROM paymenthistory WHERE empid = 29 ORDER BY to_date DESC;
For mysql try this
SELECT * FROM paymenthistory WHERE empid=59 ORDER BY to_date DESC LIMIT 1;

Mysql Select with Dates and maybe Case when

im having a problem where i cant think of a solution, maybe im having a bad table-structure or i just dont know enough about mysql select commands to think of a good solution. Maybe you can help me out:
So i got a table that has a Column with the Date-format (yyyy-mm-dd) i wanted to select all upcoming dates so i did:
SELECT * WHERE date >= now.
This worked kinda well but i also got "dates" where only the year is entered (2014-00-00) i also wanted to select these but "now" is already bigger so i made another column with the year only and if the month, date or both arent known i will use 0000-00-00 and the Column "year" now i could select like this:
SELECT * WHERE date >= now AND year >=now(year)
Now all entrys with 0000-00-00 wont be selected. If i use OR the entrys from last year will be shown.
So thats my problem, is there any way i can change my table so i can have entries with only the year or only year and month and of course all together? I already considered get rid of the date-format and use simple INT with seperated columns for year, month and date. But i think i will have the same problem.
Sometimes i just want to do a capsuled select like
SELECT *
WHERE (date >= now AND year >= now(year))
OR date == "0000-00-00" (i know that this doesnt work)
If I understood your problem correctly, you could use this request:
WHERE (date >= now OR year > now(year))
There is probably a simpler way though, that would preserve your design, like initializing at January 1st (01-01) instead of 00-00
I think you can use this code:
$_SESSION['month'] = //set here your selected month
$_SESSION['year'] = //set here your selected year
SELECT * FROM table WHERE DATEPART(m,date) >= '".$_SESSION['month']."' AND DATEPART(yyyy,year) >= '".$_SESSION['year']."' AND date <> '0000-00-00'
Change your table structure format. Actually just allow for that field to have null value when not entered. By default it will be null then. You shouldn't be storing 0000-00-00 as a value for Date type field. I would rather leave it as null , or as suggested in some of previous answers, initialize it with some other date. It would be much easier to manipulate with database then.
the problem is that half of you write is not MySQL and your database schema is terrible...
You have the following problems:
column data date does not have the date data type.
To fix it, you need to add a cast to the select statement eg. cast(datecolumn as date)
select * from table where cast(datecolumn as date) >= '2014-01-10';
the way to use now date is using the now function.
select now(), date(now());
result> 2014-01-10 11:11:36, 2014-01-10
select * from table where cast(datecolumn as date) >= date(now());
Because your datecolumn is not a date (2014-00-00 is not a valid date), you need to use string manipulation to extract the year.
select substring('2014-01-01', 1,4)
result> 2014
select * from table where substring(datecolumn, 1,4) = year(now());
The comparassion operator is = and not ==
the select statement syntax looks like this (pay attention because you are missing the table in your statement)
select * from [Table] where [column] = condition ...
You probably need or instead of ands, therefore your query should look like this:
select * from FooTable where
cast(datecolumn as date) >= date(now())
or substring(datecolumn, 1,4) >= year(now())
or datecolumn = '0000-00-00'
You should use something like phpmyAdmin or mySQL workbench to test your sql queries before try to use them on php, java or whatever is your programing language.

Get Minimum DateTime in MySQL [duplicate]

This question already has answers here:
SQL Query to show nearest date?
(5 answers)
Closed 3 years ago.
I want minimum Data From MySql DataBase. In my database Column's DataType is VARCHAR and i inserting data is like as "2011-03-01 09:00" (yyyy-MM-dd hh-mm). Now i need Minimum Date from my Database. How I can do this.? Please Help Me.
Thanks in advance.
Convert the col into a unix timestamp, get the current date as a unix timestamp, get the difference, make it a positive number, order by that number, return the first row.
SELECT * FROM tablename ORDER BY ABS(UNIX_TIMESTAMP(dateCol) - UNIX_TIMESTAMP(NOW())) ASC LIMIT 1
Edit: from your comments, you need the next row (ordered by date):
SELECT * FROM tablename WHERE UNIX_TIMESTAMP(dateCol) > UNIX_TIMESTAMP(NOW()) ORDER BY UNIX_TIMESTAMP(dateCol) ASC LIMIT 1
sort the table by your date (string) column ascending.
limit the rows to the first. get you date column.
then you have the smallest date.
no conversions are needed.
this is only possible, when you are using this format: yyyy-MM-dd hh-mm
but you are doing this, already
select datecol from table order by 1 limit 1,1
maybe the limit in the sample is wrong - i don't know if mysql starts at 1 or 0