Linq to SQL - Start Of month - linq-to-sql

I have an sql statement which returns me the 1st day of the month:
DATEADD(month, DATEDIFF(month, 0, #input), 0)
How would I recreate this call using SQLFunctions.DateAdd and SQLFunctions.DateDiff?
Thanks in advance

If you are really using LINQ-to-SQL:
var zero = new DateTime(2000, 1, 1);
var resxxxx = (from p in ctx.Addresses
select new {
month = zero.AddMonths(SqlMethods.DateDiffMonth(zero, p.Date))
}
).FirstOrDefault();
The important part is month = zero.AddMonths(SqlMethods.DateDiffMonth(zero, p.Date)).
Note that I've used the zero = new DateTime(2000, 1, 1);, but any 1st of the month would be ok (1st jan 1980, 1st jul 1983, 1st dec 2100...). It's the two 0s in your SQL command. p.Date is the date of which you want to calculate the first of the month (so #input).

Related

SSRS tweak to time expression

I am currently using this expression to show the previous working day in an SSRS report:
=DateAdd("d"
, Switch(DatePart("w", Today) = 2, -3
,DatePart("w", Today) = 1, -2
,True, -1)
, Today)
which works fine.
However I would like the output to be,if I ran the query today for example,:
24/04/2020 23:59:59
Instead of the current 24/04/2020
Please can you advise on how I could add hours, minutes and seconds- 23:59:59 - to the above expression?
Thank you
There may be a more elegant way of doing this but I based this on you current expression. I've text it and it seems to work OK.
=DateAdd("s"
, Switch(
DatePart("w", Today) = 2, (-3 * 86400) -1,
DatePart("w", Today) = 1, (-2 * 86400) -1,
True, -85401
)
, Today)
This simply does a datediff in seconds rather than days and then adjusts the amount of seconds to remove by 1

Set age group based on DOB

I would like to create a Google Sheet function that if supplied with a date of birth, looks up the below table and gives the appropriate age group. I will be using this table to group 200 kids for surf lifesaving.
for example...date of Birth is 11 Oct 2011, then the age group will be U7. I have use the query function to get this data but I would like to contain it in a function and store the table as a array and compare the date to the array table.
- U 5 01 Oct 2013 30 Sep 2017
- U 6 01 Oct 2012 30 Sep 2013
- U 7 01 Oct 2011 30 Sep 2012
- U 8 01 Oct 2010 30 Sep 2011
- U 9 01 Oct 2009 30 Sep 2010
- U 10 01 Oct 2008 30 Sep 2009
- U 11 01 Oct 2007 30 Sep 2008
- U 12 01 Oct 2006 30 Sep 2007
- U 13 01 Oct 2005 30 Sep 2006
- U 14 01 Oct 2004 30 Sep 2005
Dates can be a little tricky. See This question to get a better idea. But this script can at least get you started.
This Google Sheet demonstrates a custom formula (AGEGROUP) to compare the input date to a "age-groups" sheet.
Note: Only add to "age-groups" sheet if needed.
The last row with a value is used to consider if someone is too old.
The first row of the sheet is used to see if someone is too young.
AGEGROUP() will accept a cell reference to look up
Example AGEGROUP(B2)would check cell B2, assuming it contains a date, and return a group assignment (U 5, U 6, U7...).
Looking up dates manually (instead of referencing another cell) can be accomplished by nesting DATE inside of AGEGROUP. You must follow the right arguments for DATE (year, month, day).
Example AGEGROUP(DATE(2010,1,21))
I'm new to custom functions and did not look into the options for supporting function details like #param or #return and so on.
Apps Script
/**
* Finds age group of the input value (date).
*
* #param {date} Input the date to compare to date ranges.
* #return Returns age groupd based on provided date.
* #customfunction
*/
function AGEGROUP(date) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var ages = ss.getSheetByName("age-groups");
var range = ages.getDataRange();
var values = range.getValues();
var i = 0;
var ranges = values.length;
var lastRow = ranges -1;
// loop through dateRanges
for(i; i < ranges; i++) {
// if date in AGEGROUP()
if(date >= values[i][1] && date <= values[i][2]) {
// return age group
return values[i][0];
}
// if child is too old
if(date > values[0][2]) {
return "Child is too young.";
}
// if child is too young
if(date < values[lastRow][1]) {
return "Child is too old.";
}
}
}

SSRS Expression First Day of of First Week of Current Year

Everyone,
I have a question that has stumped me for a day and can't figure out. What I am looking for is a formula in SSRS Expression that will tell me what the date is for the first day of the first ISO week of the current year.
For Example:
2014 would yield: 12/30/2013. The reason for this would be that the first ISO week of the 2014 year is from (12/30/2013) - (01/05/2014).
2013 would yield: 12/31/2012
I would appreciate any help anyone?
Thanks,
You can use this function:
Public Function dtFirstDayOfISOYear(ByVal intYear As Integer) as Datetime
'the first week of a ISO year is the week that contains the first Thursday of the year (and, hence, 4 January)
Dim intDayOfWeek As Integer = CInt(New DateTime(intYear, 1, 4).DayOfWeek)
'ISO weeks start with Monday
If intDayOfWeek < DayOfWeek.Monday Then intDayOfWeek = intDayOfWeek + 7
Return DateAdd(DateInterval.Day, -intDayOfWeek + 1, New DateTime(intYear, 1, 4))
End Function
And call it using an Expression like this:
=Code.dtFirstDayOfISOYear(2014)
You can also use a standalone Expression like this:
=DateAdd("d", (-1) * (CInt(New DateTime(2014, 1, 4).DayOfWeek) + IIf(CInt(New DateTime(2014, 1, 4).DayOfWeek) < DayOfWeek.Monday, 7, 0)) + 1, New DateTime(2014, 1, 4))

Action script, Date constructor missreads month

This is how i call the constructor:
Date registerDate = new Date("2012","09","20","09","49","25","325");
And this is the output:
Sat Oct 20 09:49:25 GMT-0600 2012
"09" = October?
I get those parameters from a java web service in an xml, thats why they are strings, though, i get the same results with int values.
It's by design, I think:
Date() Constructor
public function Date(yearOrTimevalue:Object, month:Number, date:Number = 1, hour:Number = 0, minute:Number = 0, second:Number = 0, millisecond:Number = 0)
...
month:Number — An integer from 0 (January) to 11 (December).
...
In other words, it starts counting months from 0, so yes, October is 9. )
this is not a mistake, please look at the documentation. In a date, the value for the month is between 0 and 11, 0 for january and 11 for december. I hope this help you.
http://livedocs.adobe.com/flash/9.0_es/ActionScriptLangRefV3/Date.html

automatically update age

I got a website with an about section. there is some text about me including my current age. Lazy as I am I don't want to change that date after every birthday.
Is there an easy script to display my age based on my birthdate? I didn't find anything like that on the web.
Thx ahead.
You can do it in javascript. This should be more than enough to get you started..
// The date you were born
var birthDate = new Date(1990, 6, 19, 0, 0, 0, 0);
// The current date
var currentDate = new Date();
// The age in years
var age = currentDate.getFullYear() - birthDate.getFullYear();
// Compare the months
var month = currentDate.getMonth() - birthDate.getMonth();
// Compare the days
var day = currentDate.getDate() - birthDate.getDate();
// If the date has already happened this year
if ( month < 0 || month == 0 && day < 0 )
{
age--;
}
alert( age );