Getting info. about a date with no year in MySQL - mysql

I have a five character wide column of data that consist of a date with no year such as: 04-17 or 11-22. I am trying to create a MySQL query that will give me day of year for values in this column but am having problems because the values have no year. The closest I've come is
SELECT DAYOFYEAR(YEAR(CURDATE())-t.sdate) FROM tablex t;
but the concactenation does not seem to work. Any ideas how I can go about getting the Day of Year for date values such as these?

If you want concatenation, you have to ask for it explicitly with CONCAT().
SELECT DAYOFYEAR(DATE(CONCAT(YEAR(NOW()),'-',t.sdate)))
will do what you want, with reference to the present year. That makes a difference because some years are leap years.

You can't get an accurate DayOfYear from just a day / month pairing. You need the year to determine if it a leap year or not. You can fudge it, but it will be wrong every four years.

Related

Displaying number of years worked since joined date

List all keepers that earn more than $37,000 a year at the zoo. Show the keepers name, the total salary for a year (assume 38hrs week) and the number of years they have worked at the zoo.
Select surname
, given
, RatePerHour*38*52
From Keeper
Where ‘ratePerHour’ *38*52 > 37000
This is what i've got so far, not sure how I'm supposed to display number of years worked?
Theres a "joined" table that shows date joined at the company.
I thought about trying '2019-03-08' - 'Joined' but that displayed the incorrect answer.
My best guess would be to utilize mySql's CURDATE() function to fetch the current date rather than hard coding today's date.
Then you can use DATEDIFF(%FutureDate%, %PastDate%) function to calculate the number of days worked and finally convert those number of days in a yearly representation.

sql counting per year and month, show months even when zero

So I'm counting articles per year/month between the start of the year and the current time:
SELECT Year(FROM_UNIXTIME(date)) as year
, Month(FROM_UNIXTIME(date)) as month
, Count(*) as `total`
FROM articles
WHERE date BETWEEN UNIX_TIMESTAMP(DATE('2017-01-01 00:00:00')) AND UNIX_TIMESTAMP(DATE('2017-05-17 12:00:05'))
GROUP
BY Year(FROM_UNIXTIME(date))
, Month(FROM_UNIXTIME(date))
The only issue, is that months that have zero, won't show up.
Is there an easy way around it?
The best solution I can think of is to do an inner join with a lookup table that has months 1-12 in them. Thus verifying there will always be a 12 month result set?
Possibly including a restriction for the current date month can not be surpassed, so you don't actually always get for the whole year.
look here:
Include missing months in Group By query

Getting the number of days excluding years between two dates

I want to get the number of days ignoring year between two dates in MySql.
I am using the following query, which works fine when the dates are more than a year apart.
select dayofyear(from_days((to_days('2015-11-20') - to_days('2014-11-15'))))
select dayofyear(from_days((to_days('2019-11-20') - to_days('2014-11-15'))))
correctly both return 5
However when the two dates are in the same year
select dayofyear(from_days((to_days('2014-11-20') - to_days('2014-11-15'))))
this returns 0. I think this is because MySql has a problem with dates in years less than 100 (or at least the dayofyear function has).
Is there any way around this?
Absent your willingness to specify what happens on leap years, one is forced to guess.
I think this works. http://sqlfiddle.com/#!2/043543/1/0
IF(timestampdiff(DAY, st, str_to_date(concat_ws('-',YEAR(st),MONTH(fi), DAY(fi)),'%Y-%m-%d')) >= 0,
timestampdiff(DAY, st, str_to_date(concat_ws('-',YEAR(st),MONTH(fi), DAY(fi)),'%Y-%m-%d')),
timestampdiff(DAY, st, str_to_date(concat_ws('-',YEAR(st)+1,MONTH(fi), DAY(fi)),'%Y-%m-%d'))) delta
It tacks the starting year onto the ending day and tries to compute the number of days. Then, if that comes up negative, it tacks the year after the starting year onto the starting day and computes the number of days.
You are complicating this issue for nothing;
There is a DATEDIFF function in MySQL that allow you to do that.
SELECT DATEDIFF('2006-04-03','2006-04-01');
That would return 2 (days).
To get the result regardless of the year, then apply % 365.
Edit : If you want to get the exact number of days considering leap years, you could substract to the number of days, the number of days for both year at 1st january (or any other date) :
SELECT DATEDIFF('2006-04-03','2006-04-01') - DATEDIFF(concat(year(yourDate), '01-01'), concat(year(yourDate2), '01-01'));
This will substract the total number of days between 1st january of each year to the total number of days.

Automatically update a database column

Another answer shows how to set a default timestamp. However, it updates only the entry you edit. I was wondering if there is another way that would edit, let's say, the current year in all the entries.
For example, my database has 2 entries that were inserted in the year 2011, so the current date column for the 2 entries would be 2011. I would like to make this column update to 2012 when a new user is inserted, which means now there would be 3 entries and the current year column for all 3 entries would be 2012.
Is this possible?
My main objective is to calculate age.
You really don't need to - and should NOT - store the current year in a table's column (and possibly in several million rows) - unless you are writing a Star Trek application where some characters live in 2012 and others in 42012 (in other words if the "current year" is not the same for all).
You can always use this in your computations:
YEAR( CURRENT_DATE() )
If you store the user's birthdate as a DATE column then you can calculate their age. These calculations range from the "good 99%' of the time, to the good for everyone including leap year babies

How to get month using date in MySQL [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how do I get month from date in mysql
I want to get month using date example 2011-04-02 so I want month april. How to get this in MySQL?
SELECT MONTHNAME(date) AS monthName for January, February...
SELECT MONTH(date) AS monthName for 1, 2...
SELECT MONTHNAME(`date`) AS month_name FROM table_name;
You can use MONTHNAME() to get the month name. If you want month number, consider to use MONTH()
You can have a much more elegant solution to this if you use a second table as a date dimension table, and join your date field to it, in order to extract more useful information. This table can contain dates, month names, financial quarters, years, days of week, weekends, etc.
It is a really tiny table, only 365(ish) rows per year of data you have... And you can easily write some code to populate this table with as much data as you require. I did mine in Excel, exported as a CSV file and then imported the data into a blank table.
It also gives lots of benefits, for example, imagine a monthly data table with the following fields (and any others you can think of!) fully populated for all the months in a given range;
Date (E.g. 2009-04-01)
Day (E.g. 1)
Day of Week (E.g. Wednesday)
Month (E.g. 4)
Year (E.g. 2009)
Financial Year (E.g. 2009/10)
Financial Quarter (E.g. 2009Q1)
Calendar Quarter (E.g. 2009Q2)
Then combining this with your own table as follows;
SELECT `DT`.`monthName`
FROM `your_table`
INNER JOIN `dateTable` as DT
ON `your_table`.`your_date_field` = `dateTable`.`theDate`
There are many other nice outputs that you can get from this data.
Hope that helps!