Populate SQL column with days of the week within a date range - mysql

I currently have a table in my database which lists a range of dates, going from May until October, in the following format: YYYY-MM-DD
What I would like to do is create a new column with the days of the week, matching the date. I thought of two options;
Update all rows, in column 'day', starting with monday, then the next tuesday, next wednesday, etc, etc. and when reached sunday, loop the sequence until end of table.
Read the date in column 'date' and update the column 'day' with the matching day of the week.
I think option 1 is the most easy, and very well possible, because there are no dates being skipped. But I have no idea how to do this and couldn't find anything similar searching the web.
Any help would be greatly appreciated! Thank you

As a general rule, I would say: don't do it. The day of the week is derived directly from the date; storing it separately in the database breaks one of the basic rules of normalisation (no derived data).
There are MySQL functions to find the day of the week - dayofweek() and weekday().

Try this:
update table
set day = DATENAME(dw, date)

Related

Selecting PHP data by date for the last 7 days from mysql database with a different date layout

I have a transactions table in my database where the date is stored in the date field like this: 2014-08-30 02:22:35.
I'm making basic analytics and need to be able to display all transactions for each day for the last 7 days but am a little confused as to how I can achieve this when there is a timestamp along with the date stored in the same field.
Any suggestions would be greatly appreciated.
You can get the last seven days with:
where `date` >= CURDATE() - interval 7 day
This will go back seven days, ignoring the time field.
I'm not sure what you mean by "display all transactions for each day for the past 7 days". You can extract just the date for the field using date (so, date(date)) and use the value for filtering, aggregation, or sorting.

My SQL query to list dates that have no records

I have an online calendar system that I use for tracking my band's gigs - I'd like to construct a query that will display all Fridays and Saturdays that don't currently have a record assigned to them.
eg,
if I have a record in the DB for Friday 23rd Aug and Friday 30th Aug (records being gigs that are booked), what would the query cirteria be to output Saturday 24th Aug (as it has no record)?
Select * from ['giglist']
where ['gigdate'is in 'friday','saturday']
and ['gigdate' doesn't have a record]
I will probably set the days of the week as variables so that the user can run the query for any day or selection of days.
Thanks,
Darren
if assuming from your question there is a field gigdate of date type that keeps date information and a seperate record field.
Then query would be,
select DAYNAME(gigdate), DAYOFMONTH(gigdate), MONTHNAME(gigdate) from giglist where
DAYNAME(gigdate) in ('Friday', 'Saturday') and
recordfield is NULL;
It's better to use single date type field and just store date only, as mysql has powerful set of date functions to help you out for your needs.

MySQL selecting date range but also between

My db is made of groups of entries (by user) with a row for each day of the week and also groups where there is only 1 row per week of the year. This week may start Sat, Sun or Mon.
The sql groups all these rows by user id and works fine for the entries where the user has a row for every day
The problem I have is selecting the users rows where there is only one entry per week
Basically if the rows date is 11th Feb 2012 then I need to be able to select that row if the start date criteria falls on that date or within that following week and all rows upto but not including the row where the date column is after the end date
I'm trying everything like dateadd in the sql but I just cannot get it to add these rows in.
Hope I've made myself clear.
Say I have two entries in the db
2013-02-02
2013-02-09
I have a start date of 2013-02-05 and an end date of 2012-02-13
I need to get those two row as:
the start date falls on or within the week of 2013-02-02
and I also need 2013-02-09 as the end date falls on or within the week of that date.
Hope that makes it a bit clearer.
Not sure exactly what your question is asking.
If your field is of mysql date or datetime type, and you wanted to find if there was an entry for a given week could you not use MySQL WEEK Function to find all entries for the given week, you may also need to include a restriction on YEAR too.
You could also include the following week, but you may encounter problems. The main problem being week 52+1 of 2012 wont give week 1 of 2013, but week 1 of 2012.

MySQL only allowing Date to be stored: how to store Time as well?

I would like to store in my table a full date: year, month, day, hour, minute.
Using the date type is limiting me to year month and day only.
What should I do? I have to mention that I will select records from the db order by the full date so storing the hour and minute seperatly as strings might be a problem right?
You may use DATETIME instead of DATE.
Took me a while to figure this out but to change it in MySQL you have to go to the column that you are looking to change in structure. Change the Type column from DATE to DATETIME.

How to insert just year and month into date field?

I have a column called table_date which currently I am using now() to insert the current date (2011-02-23). I know I can manipulate this with sql/php to show me year and monthname. However, I want to know if it's possible to just insert into table_date the current date as year-month like this 2011-02? Thanks
A DATE field is always going to be a full date, and as far as I know, it is also always required to specify a full date.
It might be easiest to use 01, like 2011-02-01 for February 2011.
Obviously, you can format the output of a DATE field to your liking when querying it:
SELECT DATE_FORMAT(fieldname,"%Y-%m");
If you insert something like 2010-10 into a DATE column, mysql throws a warning and inserts 0000-00-00. If you do not want to specify a certain day, you can insert something like 2010-10-00. Pay attention when querying for "all entries in October 2010" since WHERE date >= '2010-10-01' will not return 2010-10-00.