get certain date, preventing holidays or sundays stored on database - mysql

i have column datetime format with consecutive dates, but i need count 60 days ago from now but i need prevent days marked with 0 because days are sundays or holidays.
this not work:
SELECT DateDVH as refDays
FROM DVH_days
WHERE DateDVH>'2016-12-20 00:00:00'
AND (DateDVH_dia<'2017-02-18 00:00:00' + INTERVAL 60 DAY)
AND DateDVH_daytype!=0
DateDVH_daytype=0 is holidays and sundays.
It is possible to place a counter in the query or a condition that prevents the days type = 0 or a stored procedure??
update
i solve it with :
SELECT DateDVH_dia
FROM Avipac_DVH_dias
WHERE DateDVH_dia>now()
AND DateDVH_diatype!=0
LIMIT 60
but i get a list of 60 date and only need get the last date

Related

MYSQL: Select statement that counts event in timestamp column for the current date adjusting for UTC

So this has been the biggest pita... My db is in UTC and I am trying to create a select statement that accurately displays the count of newly created customers today in my TZ. The first interval works as intended, but when the clock strikes 5pm PST (UTC -7) and the db switches over to the next day, the second part after the and clause fails, and I am left with an empty set. Tweeked the hell out of the second part but have had no success. Any help would be most appreciated!
SELECT count(*) FROM product36.associate where date(created_date - interval 7 hour) >= curdate() and date(created_date + interval 17 hour) >=curdate();

MySql date range search with month and day (any year)

For example, if i have search string "2017-12-14" then i need to find all rows that matches 7-day range: "%-12-11", "%-12-12", "%-12-13", "%-12-14", "%-12-15", "%-12-16", "%-12-17".
Is it possible to do with MySql only?
To select same day cross years you can use following trick.
get the list of last 7 days/1 week NOW() - INTERVAL 1 WEEK
get the DAYOFYEAR of that days
search the database for all values of the same day in year
.
SELECT * FROM timevalues
WHERE DAYOFYEAR(timefield) IN (
SELECT DAYOFYEAR(timefield)
FROM timevalues
WHERE DATE(timefield) BETWEEN NOW() - INTERVAL 1 WEEK AND NOW()
)
;
Note: Leap year is not taken into calculation!
According to my brief investigation according to the leap year it would be easier to extend the SQL query with tolerance of 1 day, ie to use - INTERVAL 8 DAY instead of 7 and then control the validity of the day outside the database during processing the data in a loop.
Yes, it is possible.
The function you are looking for is +/- INTERVAL expr unit. See MySQL Date and Time Functions
So to get 7 days back use - INTERVAL 7 DAY:
SELECT *
FROM tablename
WHERE DATE(timefield) BETWEEN '2017-12-14' - INTERVAL 7 DAY AND '2017-12-14'
According to your example would be enough to use -INTERVAL 1 WEEK:
SELECT *
FROM tablename
WHERE DATE(timefield) BETWEEN '2017-12-14' - INTERVAL 1 WEEK AND '2017-12-14'
And List of all possible units
MICROSECOND SECOND MINUTE HOUR DAY WEEK MONTH QUARTER YEAR
SECOND_MICROSECOND MINUTE_MICROSECOND MINUTE_SECOND HOUR_MICROSECOND
HOUR_SECOND HOUR_MINUTE DAY_MICROSECOND DAY_SECOND DAY_MINUTE DAY_HOUR
YEAR_MONTH

Select Range of Items by Year and Month

I'm attempting to create a select statement which gets items by year and month.
This is what I have so far:
SELECT * FROM sales WHERE YEAR(Date) = 2013 AND MONTH(?) = 'June'
I can't merely select date ranges because different months have different number days. It would be ideal to select months by their number (ie, January being 1) or a similar approach.
How is this worked out in a mysql statement?
The fields are datetime fields such as 2012-12-01 00:00:00
Have a look at the performance and write your condition as
SELECT * FROM sales
WHERE
Date >= '2013-06-01 00:00:00'
AND
Date < '2013-07-01 00:00:00'
for the example month: June of 2013
so MySQL can use an index on the column date. You will get exactly all rows with a date in the June of 2013, even those in the first second, but not those in the first second of the July of 2013.
You see, that you don't need to know the number of days of the particular month, because you will ever use the first of both months.
You could use a bit of date calculation too:
SELECT * FROM sales
WHERE
Date >= '2013-06-01 00:00:00'
AND
Date < '2013-06-01 00:00:00' + INTERVAL 1 MONTH
so you need only to know the start and the length of the interval.
Note
The most important part of my answer is to use the column Date without using a function on this column, so MySQL can use an index.

MySQL intervals dates remaining

I have to write a query that pulls the users that are ten days away from ending their free trial. My question is, do i do a minus 10 day interval or a plus 10 day interval against CURRENT_DATE().
I am having difficulty thinking about this.
Here's my query:
SELECT * FROM users WHERE freetrial=1 AND date_format(date_created,'%Y-%m-%d 00:00:00') = CURRENT_DATE() - INTERVAL 10 DAY
If the trial period is 30 days long then you need to add 20 days to the date they registered and compare that to today's date.
SELECT *
FROM users
WHERE freetrial=1
AND FROM_UNIXTIME(date_created,'%Y-%m-%d') + INTERVAL 20 DAY = CURRENT_DATE()

Get specific date range

I'm using
SELECT * from tbl_name WHERE DATE_SUB(CURRENT_DATE(), INTERVAL 3 DAY)
to select data for specific days. The problem is that line gets data right before 3 days.
What to do so selected data to be period three days before till now ?
First your field should be of type datetime or date and then you can use a between clause
your_date_field BETWEEN now() - INTERVAL 72 HOURS AND now()