sql date ... get full year behind? - mysql

i need help for a function ... what i want to do is get the quantity of year behind of a date chosen .... for example i write '12-12-2017' ... so it will back year behind 'til '12-12-2016' i guess ?? any help...

SELECT DATE_ADD('2017-12-12', INTERVAL -1 YEAR)

Most simple to use is
SELECT '2017-12-12' - INTERVAL 1 YEAR
demo http://rextester.com/SUM99725

Related

Using date_add with a column as interval value and comparing dates in mysql

I have this employee table:
Name birth_date retirement_age
Andy 1964-03-13 56
Bertha 1964-11-16 55
Chris 1964-03-08 58
Damon 1964-04-11 56
I'm trying to figure out which employee retires this month (November 2019) by adding retirement_age to their birth_date using date_add().
The retirement date is always at the end of the month.
This is what I've been working on:
SELECT Name,
#ra:=CAST(retirement_age AS UNSIGNED),
LAST_DAY(DATE_ADD(`birth_date`, INTERVAL #ra year)) as `retirement_date`,
FROM `employees`
HAVING
`retirement_date`='2019-11-30'
The problem is that the query produces 0 result where it should've returned 1 result (Bertha).
I don't get what's wrong with it.
Can somebody help or maybe recommend me the correct way to do it?
Thanks in advance.
The problem is the variable #ra for which it is not guaranteed that it will be calculated before the column retirement_date or CAST(retirement_age AS UNSIGNED) directly.
So drop it and use retirement_age:
SELECT Name,
LAST_DAY(DATE_ADD(`birth_date`, INTERVAL retirement_age year)) AS `retirement_date`
FROM `employees`
HAVING `retirement_date` = '2019-11-30'
See the demo.

Sort row where date is today

I'm having a small issue where I try to select all the rows where the user birthday is today.
In my table, my data is something similar like this yyyy-mm-dd formated in a varchar.
My current request is :
SELECT * FROM rb_users WHERE user_birthday = DATE_FORMAT(NOW(), '%m %d')
I know that the above will return nothing as I readed on the documentation. As I understand, I would need to do DATE_FORMAT(NOW(), '%Y-%m-%d') to make it work.
My question is : Is there anyway to ignore the year in this kind of strucutre or I should edit my field to unixformat like 1402012800?
As is it done right now, I will only get people when there birthdays are in 2015.
Source #1 :
https://stackoverflow.com/questions/24076419/sql-query-to-check-birthday-is-today
Because you have indicated that the column user_birthday is a varchar, you can use string function as follows. Also be sure to add the hyphen ("-") into your DATE_FORMAT:
SELECT * FROM rb_users WHERE RIGHT(user_birthday, 5) = DATE_FORMAT(NOW(), '%m-%d')
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_right

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.

Number of days between current date and date field

I have this problem if anyone can help.
There is a field (date) in my table (table1) that is a date in the format 3/31/1988 (M/D/y), and my necessity is to define how many days have passed since that date.
I have tried to give this instruction
SELECT DATEDIFF(CURDATE(), date) AS days
FROM table1
But it gives back 'null' and I think this happens because the two date formats are different (CURDATE() is YMD.....
Is it correct? can anyone help me?
Thank you in advance
You can use STR_TO_DATE():
SELECT DATEDIFF(CURDATE(),STR_TO_DATE(date, '%m/%d/%Y')) AS days
FROM table1
SQLFiddle Demo
Your DATE field should have DATE or DATETIME format to be used as DATEDIFF argument correctly.
Also DATE is MySQL keyword and I am not sure that you can use it as valid field name.
You can use this for accurate result
SELECT DATEDIFF(CURDATE(), DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP(`date`)), '%Y-%m-%d')) AS days FROM `table1`
If you want to consider results without - signs that you have to follow parameters position as below :
SELECT DATEDIFF(Big_Date,Small_Date) AS days FROM table1.
positive results e.g 5 (with no sign), if you place a Small date as the first parameter then it will results minus sign e.g -5.

MySQL - Query last six weeks of data

In order to calculate projected sales for a given day, I need to query the last six weeks of data for a given day. For example, if I want projected sales for Friday, I need to query data from the last six Fridays only.
I'm assuming there is a way to do this within a query, just not sure exactly how. Any help or insight would be greatly appreciated, as always.
Thanks in advance.
The easiest way is to use a limit.
SELECT date, sales FROM yourtable WHERE DAYOFWEEK(date)=6
ORDER BY date DESC LIMIT 6;
EDIT: To get this relative to today, just add CURDATE()
SELECT date, sales FROM yourtable WHERE DAYOFWEEK(date)=DAYOFWEEK(CURDATE())
ORDER BY date DESC LIMIT 6;
You can use a combination of different MySQL date and time functions to achieve this. Your query could look something like this:
SELECT fields FROM table WHERE DAYOFWEEK(table.date) = DAYOFWEEK(CURDATE()) ORDER BY table.date DESC LIMIT 6
Of course you can replace CURDATE() with the date that you are trying to predict.
Select * From table Where date_field > DATE_ADD(now(),INTERVAL -42 DAY)
That's about what you will need to do.
I just seen you wanted to query only a particular day of each week. Give me a moment and I'll update this.
Nevermind, I'm not going to edit this. Bobby has your answer for you. You just need to place variables in there through your script as needed. +1 Bobby.