Finding the week number from a date in Visual Basic - vba

I want to compare 2 dates and check that they are in the same week. Is there a way to find the week number of each date and compare them, or an alternative function to compare the dates and see if they're in the same week?
Thank you :)))))))))))))))))

Look at the vba Format function - use something like Format(DateVariable, "ww") to get week number of a date.

in VBA, use DateDiff which returns 0 (zero) for a match:
WeekMatch = Not CBool(DateDiff("ww", Date1, Date2, vbUseSystemDayOfWeek, vbUseSystem))

Related

How to find the weekday from the date for an entire column of dates

I know that you can use the DATEPART(), FORMAT(), WEEKDAY() functions to find the weekday of a single date, but I need to use these functions to create a new column that finds the weekday for an entire column of dates in date_dim_id. How do I do this?
Below is my date_dim_id column. I want to add a column beside it that tells me which weekday each date is.
Try this:
select date_part(dow, '20210805') as dayoftheweek

SQL: Creating Dynamic Fiscal Quarters

I have a set of data which includes an account #, and a fiscal end date. The fiscal end date does not always match the calendar year. I need to take the given fiscal end date and generate the quarters based on it. For example, if fiscal end date equals 6/31 then the first quarter would be 7/01 thru 9/30, second would be 10/1 thru 12/31, etc.
The only way I can currently think to do this would be a very long and complicated if/then structure. Any help making something more efficient would be greatly appreciated.
You can use DATEADD() function
select convert(varchar(10),dateadd(day,1,'6/25/2016'),101) as startdate,
convert(varchar(10),dateadd(month,3,dateadd(day,1,'6/25/2016')),101) as enddate
Result will be like:

Get the month and year now then count the number of rows that are older then 12 months in SQL/Classic ASP

I know this one is pretty easy but I've always had a nightmare when it comes to comparing dates in SQL please can someone help me out with this, thanks.
I need to get the month and year of now then compare it to a date stored in a DB.
Time Format in the DB:
2015-08-17 11:10:14.000
I need to compare the month and year with now and if its > 12 months old I will increment a count. I just need the number of rows where this argument is true.
I assume you have a datetime field.
You can use the DATEDIFF function, which takes the kind of "crossed boundaries", the start date and the end date.
Your boundary is the month because you are only interested in year and month, not days, so you can use the month macro.
Your start time is the value stored in the table's row.
Your end time is now. You can get system time selecting SYSDATETIME function.
So, assuming your table is called mtable and the datetime object is stored in its date field, you simply have to query:
SELECT COUNT(*) FROM mtable where DATEDIFF(month, mtable.date, (SELECT SYSDATETIME())) > 12

Always Return the Previous Saturday's Date Using NOW Function in MS Excel 2007

I have a workbook that I am using the NOW function to return today's date and then I have another cell to convert to WEEKNUM that I use to MATCH on with a different tab. I need to be able to create a formula to read today's date and always return the previous Saturday. Example: Today is 6-8-2015. I want this formula to return 6-6-2015. Any ideas on how to make this work. I am struggling here.
How about:
=NOW()-(WEEKDAY(NOW(),1))
This will return the time 'now' minus the number of days from Saturday where Sunday = 1, Monday = 2 etc.
Note, this does include the timestamp along with the day (eg. 6/6/2015 14:48). If you want just the date you could daisy-chain a text function around it.

Filtering rows based off of relation to date column

I am trying to find a good way to filter data based off of the date column (Column I in my actualy spreadsheet). I can copy the rows and all of that to another sheet just fine, but the date thing is new to me. Basically I need to know how to filter based on three situations:
If the current date is within 3-6 months of the date in Column I;
If the current date is within 0-3 months of the date in Column I;
If the current date is past the date in Column I.
Thank you for any assistance.
Sounds like you want to use the DATEDIFF VBA function. Here is an example: http://www.techonthenet.com/excel/formulas/datediff.php.
In addition to DateDiff you can use:
Date()
Year()
Month()
Day()
Functions to determine this information.