Get alternate weekend in month - sql

I want to check a set of values of dates if it falls among 1st or 3rd or 5th Monday of any month. How to do it in SQL Server 2008?

Okay, that can easily be expressed by a couple of conditions:
WHERE
DATEPART(weekday,DateToCheck) = DATEPART(weekday,'20120910') AND
(
DATEPART(day,DateToCheck) between 1 and 7 OR
DATEPART(day,DateToCheck) between 15 and 21 OR
DATEPART(day,DateToCheck) between 29 and 31
)
(I do the DATEPART(weekday,... check as above so that I don't have to know what your date settings are on the server - I'm just checking the value against a "known good" Monday)

try this:
SELECT *
FROM <your_table>
WHERE datename(weekday,<date_col>)='Monday'
AND DATEPART(day,<date_col>)/7 in (0,2,4)

Related

MS SQL How to get the next month if its december

Hi I wanna get the next month in SQL server but what if the month is 12.
when i have date = '2016-10-04' then the next month will be date = '2016-11-04'.
I want to put this into this query :
if EXISTS(
select * from month
where id_Prod = #id_Prod
and datepart(month,DATEADD(month,1,_date)) = datepart(month,DATEADD(month,1,_date))
and datepart(YEAR,_date) = datepart(YEAR,#date)
);
you can try dateadd
declare #dt date = getdate()
select datepart(MM,dateadd(mm,1, #dt))
If the spec I've given in the comments is correct, you want something along the lines of:
if EXISTS(
select * from month
where id_Prod = #id_Prod
and _date >= DATEADD(month,DATEDIFF(month,'20010101',#date),'20010201')
and _date < DATEADD(month,DATEDIFF(month,'20010101',#date),'20010301')
);
The DATEADD,DATEDIFF pairs are just being used to generate "the 1st of next month" and "the 1st of the month after that", using arbitrary (fixed) dates to compute those. E.g. the first line computes how many whole months have occurred between 1st January 2001 and #date. It then adds that number of months onto 1st February 2001. This expression should therefore always generate the 1st of the month that comes after #date. The second pair does the same but adds the computed number onto 1st March instead.
You should also note that I'm not applying any functions to _date, so if there happens to be a useful index on that column, it should be usable for this query.
This seems pretty simple,
Get Previous date of current date
SELECT DATEADD(MONTH,-1,GETDATE()) AS PrviousDate
Get Next date of current date
SELECT DATEADD(MONTH,1,GETDATE()) AS NextDate

Datepart Week Sql (With 53 weeks for year)

I'm calculating the week for a specific date in SQL for example
'2016-01-20' (yyyy-mm-dd) but SQL returns week: 4, and that is wrong because this year the first week started on '2016-01-04' the result must be week: 3.
I think the issue is generatad because 2015 was a year with 53 weeks, any solution to that? Thank you and I'm sorry for my bad English
In tSQL the DATEPART() is returning the correct data based on US and Most of Europe as well as UK See here
You can use SET DATEFIRST to adjust the start position however.
The ISO 8601 definition for week 01 is the week with the year's first Thursday in it. I am using Intersystems cache which apparently does not account for that either. So I have used this to address that
CASE WHEN 7-datepart(dw,dateadd(dd,1,dateadd(yy,datediff(yy,0,getdate())-1,0))) < 2
THEN datepart(wk,getdate())-1 ELSE datepart(wk,getdate()) END as WeekNum

Make calc on dates using informix db

I want to put the following condition in my query ,but i don't know the correct syntax in informix to do that .
At least one year passed on his work date ..
So
I try some thing like that
b.work_date - CURRENT >= 12 -- 12 month
How to do that ?
You can do:
b.work_date <= CURRENT - 12 UNITS MONTH
You need to be careful with the CURRENT - 12 UNITS MONTH approach. It does not take the leap-day in February into account, and would explode with an Invalid day in date error if you ran it on 2012-04-29.
It is safer to write
b.work_date < TODAY - 365

MySQL SUM Query daily values of a week problem

Am trying to return the sum of each day of a week in mysql but it returns nothing despite having values for the 3rd Week of March 2010
SELECT SUM(expense_details_amount) AS total
FROM expense_details
WHERE YEAR(expense_details_date) = '2010'
AND MONTH(expense_details_date) = '03'
AND WEEK(expense_details_date) = '3'
GROUP BY DAY(expense_details_date)
How do I go about this?
According to the MySQL docs for WEEK:
This function returns the week number for date. The two-argument form of WEEK() allows you to specify whether the week starts on Sunday or Monday and whether the return value should be in the range from 0 to 53 or from 1 to 53. If the mode argument is omitted, the value of the default_week_format system variable is used. See Section 5.1.4, “Server System Variables”.
Their examples are:
mysql> SELECT WEEK('2008-02-20');
-> 7
mysql> SELECT WEEK('2008-02-20',0);
-> 7
mysql> SELECT WEEK('2008-02-20',1);
-> 8
mysql> SELECT WEEK('2008-12-31',1);
-> 53
So you should not be checking for week 3, but whatever week of the year the 3rd week in march is.
It also looks like these functions return integers instead of strings, so you might want to lose the single-quotes in your query.
Your where clause excludes all of the data, as the third week of the year is in January and you have also specified the month to be March. Try using a where clause of the form:
WHERE expense_details_date BETWEEN '2010-03-15' AND '2010-03-22'
WEEK() actually works like WEEKOFYEAR(), not WEEKOFMONTH(), so it gives values 0 through 53 as the result, by default.
If you're saying that the first week of the month is the first 7 days, not starting at the first Sunday, then you can use DAYOFMONTH(), and BETWEEN to get your third week:
SELECT SUM(expense_details_amount) AS total
FROM expense_details
WHERE YEAR(expense_details_date) = 2010
AND MONTH(expense_details_date) = 3
AND DAYOFMONTH(expense_details_date) BETWEEN 15 AND 21
GROUP BY DAYOFMONTH(expense_details_date)
Notice that I also changed the constants to numerals instead of Strings so MySQL doesn't have to do the conversion.

Add date without exceeding a month

I hope someone could help me on this.
I want to add a month to a database date, but I want to prevent two jumping over month on those days at the end.
For instance I may have:
Jan 31 2009
And I want to get
Feb 28 2009
and not
March 2 2009
Next date would be
March 28 2009
Jun 28 2009
etc.
Is there a function that already perform this kind of operation in oracle?
EDIT
Yeap. I want to copy each month all the records with some status to the next ( so the user don't have to enter again 2,000 rows each month )
I can fetch all the records and update the date manually ( well in an imperative way ) but I would rather let the SQL do the job.
Something like:
insert into the_table
select f1,f2,f3, f_date + 30 /* sort of ... :S */ from the_Table where date > ?
But the problem comes with the last day.
Any idea before I have to code something like this?
for each record in
createObject( record )
object.date + date blabala
if( date > 29 and if februrary and the moon and the stars etc etc 9
end
update.... et
EDIT:2
Add months did the trick.
now I just have this:
insert into my_table
select f1, add_months( f2, 1 ) from my_table where status = etc etc
Thanks for the help.
Oracle has a built-in function ADD_MONTHS that does exactly that:
SQL> select add_months(date '2008-01-31',1) from dual;
ADD_MONTHS(
-----------
29-FEB-2008
SQL> select add_months(date '2008-02-29',1) from dual;
ADD_MONTHS(
-----------
31-MAR-2008
I think you're looking for LAST_DAY:
http://download.oracle.com/docs/cd/B28359_01/olap.111/b28126/dml_functions_2006.htm
I just did:
select add_months(TO_DATE('30-DEC-08'), 2) from dual
and got
28-FEB-2009
No need to use LAST_DAY. If you went that route, you could create a function that:
1. takes a date
2. Changes the day to the first of the month.
3. Add a month.
4. Changes the day to the LAST_DAY for that month.
I think you'll have to write it on your own, My advice is first to evaluate the "last day of the month" with this method:
Add one month (not 30 days, one month!)
Find first day of the month (should be easy)
substract one day
Then compare it to your "plus x days" value, and choose the lowest one (I understood the logic behind the jump from 31/Jan to 28/Feb, but I don't get it for the jump from 28-Feb to 28-Mar)
It sounds like you want the current month plus one (with appropriate rollover in December)
and the minimum of the last day of that month and the current day.