i need your help on this
i need to pull birthdays between two dates irrespective of year
i use the below query
$sql = "SELECT * FROM family_member WHERE DATE_FORMAT(dob, '%c-%d') BETWEEN DATE_FORMAT('2013-".$from_month."-".$from_day."', '%c-%d') AND DATE_FORMAT('2013-".$to_month."-".$to_day."', '%c-%d') order by MONTH(dob), DAYOFMONTH(dob)";
the query works well if i give the
- start date as Nov 6 &
- End date as Dec 13
but the query returns zero records if i give
- start date as Sept 6 &
- end date as Dec 13
it works at certain scenarios. can you please let me know the issue i need to correct
This is due to the fact, that September is the 9th month and "09" and "9" are not the same string. Use %m instead of %c
Edit
Some explanation as requested: For the month range of September to December your comparison range was 9-06 to 12-06. Now remembering, that this is a string comparison, e.g. 10-25 is NOT bigger than 9-06, meaning the BETWEEN clause will produce no meaningful results. If you chose 2-digit months, you end up comparing 09-06 to 10-25, which works as expected.
Related
Please share your feedback on this problem. I need to calculate difference in 'years' and store it under a new column 'Age'.
While the formula works fine, it gives me incorrect output when I consider dates starting from 1st Jan of any year
For example: difference in years between 1st Jan 2019 and 31st Dec 2021 is 3 years - this includes end date in calculation. My result shows 2 years.
Here are the 2 date columns from which I am deriving the difference:
However, when I consider dates from 1st Jan - the result shows me one year less:
Here is the code I used to calculate difference:
UPDATE animals
SET age = abs(benchmarkdate :: date - birthdate :: date)/ 365;
Any help would be appreciated. Thank you.
I would use EXTRACT here and then take a difference of only the year components on the two dates:
UPDATE animals
SET age = EXTRACT(year FROM benchmarkdate) - EXTRACT(year FROM birthdate);
Note that you might even want to avoid doing this update, and instead just compute the age when you select. If you foresee the need to frequently do such an update, that would be a good indicator that you probably should change your approach.
I'd like to seek help with my access database. i would like to distribute value (format on currency) into multiple field base on date start and date end.
for example on:
if field Spend Amount value is - $10,000
if field start date is - Jan-2018
if Field end date is - April-2018
then the Jan field will be $2,500
and Feb field should be $2,500
Mar field also $2,500
april field also $2,500
basically the amount should be equally divided base on start month and end month category
appreciate your help please
The expression to put in your update can look something like this:
Amount/(DateDiff("m",StartDate,EndDate)+1)
This will divide 10.000 with 4. The 4 comes from the DateDiff that counts the number of months between the start and end (which results in 3) plus one. Make sure to test edge cases like Nov 30 to Dec 1, leap years and other problematic combinations.
Edit, clarification - this is not VBA. Put it in an update statement
I'm using SSRS 2008r2 to generate reports. Using following WHERE statement in SQL query
WHERE (NonPMJobs.npmJobCreateDate >= #Created_Parameter) AND
(NonPMJobs.npmJobCreateDate <= #Created_Parameter2)
I'm using parameters with the data type of DateTime. Users then select day from a calendar. I want to get results where jobs have been created between date 1 (#Created_Parameter) AND date 2 (#Created_Parameter2) INCLUSIVE.
But results being returned do not include the second date (#Created_Parameter2). If I select 01/07/2013 - 05/07/2013 I get 01, 02, 03, 04 but no 05. If I select 01/07/2013 - 06/07/2013 I get 01, 02, 03, 04, 05.
I've tried using:
WHERE (NonPMJobs.npmJobCreateDate BETWEEN #Created_Parameter AND #Created_Parameter2)
but get same results.
What am I missing here and why isn't WHERE statement inclusive? Any pointers would be very much appreciated.
Well, you need to think about this: a DATETIME like this: 05/07/2013 means the 5th of July (or 7th of May - depending on your locale) at midnight when the day starts (a 00:00 hours, so to speak).
So this does NOT include the events that happen on that day!
The best solution would be to use
WHERE (NonPMJobs.npmJobCreateDate >= #Created_Parameter) AND
(NonPMJobs.npmJobCreateDate < DATEADD(DAY, 1, #Created_Parameter2))
and basically getting everything that's smaller than the next day based on #Created_Parameter2. So for your 5th of July, that would be anything before the 6th of July - which includes all events from the 5th of July.
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)
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.