I am able to run the query and update the days from the date it was created. However I would like to exclude weekend days, just Monday thru Friday.
Example:
UPDATE [Table1] SET [Table1].Days = DATE() - [Table1].DateCreated WHERE [Table1].Password = True AND [Table1].Complete = FALSE;
This will update the days to the value from the created date to current date, which would be 1,2,3, etc. However I need this to exclude the weekends. I don't run queries very often but I would like create this query.
Please help, thank you.
It you will be running your query from within an Access session, you can use a VBA user-defined function to compute the number of weekdays between two dates.
For example, consider the WorkingDays() function from the Access Web: Calculate Number of Working Days. If I understand your query correctly, I think this may do what you want:
UPDATE [Table1]
SET [Table1].Days = WorkingDays([Table1].DateCreated, DATE())
WHERE [Table1].Password = True AND [Table1].Complete = FALSE;
On that same page, you can find a WorkingDays2() function which would allow you exclude holidays from the workdays count if that's useful. You would need to set up a table for your holidays though.
If you will be doing this from outside an Access session, like from classic ASP or Dot.Net, you won't be able to use a user-defined function in the query.
Related
I have a working query which takes too long to run. It is being used to populate an Access form field with a list of distinct contracts ordered by their start date.
The following query returns a list of distinct contract start dates for multiple commodities (which share contract start dates) where the contract start date (DELSTART) is greater or equal to the current date (PRICEDATE).
The function returndelivery returns a date attribute of the contract converted to a double, the function returnnumericdate just returns a double from a date (yyyymmdd).
SELECT DISTINCT (tblFuturesPrices.Period),
returnnumericdate(returndelivery([PERIOD],"S")) AS DELSTART,
ReturnNumericDate(Date()) AS PRICEDATE
FROM tblFuturesPrices
WHERE returnnumericdate(returndelivery([PERIOD],"S")) >= ReturnNumericDate(Date())
GROUP BY PERIOD
ORDER BY returnnumericdate(returndelivery([PERIOD],"S"));
Ideally I would like to refer to the variables DELSTART and PRICEDATE in the where clause but Access prompts for a variable value when I do so. I think the query takes longer than it should because I'm having to use my user defined functions numerous times.
The source table (tblFutures) contains prices for each commodity / contract for working days going back 6 months.
Thanks in advance.
I think the query takes longer than it should because I'm having to
use my user defined functions numerous times.
That's it. But you can reduce those functions:
returnnumericdate(returndelivery([PERIOD],"S")) >= ReturnNumericDate(Date())
will be no different from
returndelivery([PERIOD],"S") >= Date()
Don't know what returndelivery does.
Addendum:
Filter on the raw data and create a new function returndeliverydate that does the same as returndelivery except returns a Date value:
SELECT
tblFuturesPrices.Period,
returndeliverydate([PERIOD],"S") AS DELSTART,
Date() AS PRICEDATE
FROM
tblFuturesPrices
WHERE
returndeliverydate([PERIOD],"S") >= Date()
Save this query and use it as source in a new query:
Select Distinct
DELSTART,
PRICEDATE
FROM
YourQuery
I've tried limiting data on monthly basis in spotfire and it's working fine.
Now I'm trying to do like getting the records from the current date to month start date.
For suppose if the current date is Sept 21, then i should get the records from Sept 21 to Sept-01(dynamically).
I have a property control to input the number of months.
The easiest way to do this is with Month and Year. For example, in your visualization:
Right Click > Properties > Data > Limit Data Using Expressions (Edit)
Then, use this expression:
Month([TheDate]) = Month(DateTimeNow()) and Year([TheDate]) = Year(DateTimeNow())
This will limit the data to only those rows with the current Year/Month combination in your data column. Just replace [TheDate] with whatever your date column name is.
In other places, you can wrap this in an IF statement if you'd like. It's redundant in this case, but sometimes helps with readability.
IF(Month([TheDate]) = Month(DateTimeNow()) and Year([TheDate]) = Year(DateTimeNow()),TRUE,FALSE)
#san - Adding to #scsimon answer. If you would like to precisely limit values between 1st of the current month to current date, you could add the below expression to 'Limit data using expression' section.
[Date]>=date(1&'-'&Month(DateTimeNow())&'-'&year(DateTimeNow())) and [Date]<=DateTimeNow()
I have the following Access query (see above) to give me the date, X number of weeks out from the actual WorkDte that exists in the table. But, the date these expressions calculate out may not actually exists in the “CIB_Results” table due to a bank holiday etc. Is there a way to generate the same sort of data but say if it’s running the “Wk1” calculation and it calculates out 1/1/2016 (which does not exists due to the new year holiday), instead of doing [WorkDte]-7 it will move on to [WorkDte]-14 and so on, until it finds an actual date that exists in the “CIB_Results” table? I’m wanting to apply the same logic to all the fields in the query…that way they will all self adjust based off of the actual dates that exist in the “CIB_Results” table. Any help with this would be greatly appreciated!
Have a function IsHoliday(SomeDate) that looks up SomeDate in your holiday table and returns True if found.
Then create a loop:
Public Function PreviousWorkWeekday(ByVal SomeDate As Date) As Date
Do
SomeDate = DateAdd("ww", -1, SomeDate)
Loop Until Not IsHoliday(SomeDate)
PreviousWorkWeekday = SomeDate
End Function
Now use function PreviousWorkWeekday in your query.
I have a date of birth DATE column in a customer table with ~13 million rows. I would like to query this table to find all customers who were born on a certain month and day of that month, but any year.
Can I do this by casting the date into a char and doing a subscript query on the cast, or should I create an aditional char column, update it to hold just the month and day, or create three new integer columns to hold month, day and year, respectively?
This will be a very frequently used query criteria...
EDIT:... and the table has ~13 million rows.
Can you please provide an example of your best solution?
If it will be frequently used, consider a 'functional index'. Searching on that term at the Informix 11.70 InfoCentre produces a number of relevant hits.
You can use:
WHERE MONTH(date_col) = 12 AND DAY(date_col) = 25;
You can also play games such as:
WHERE MONTH(date_col) * 100 + DAY(date_col) = 1225;
This might be more suitable for a functional index, but isn't as clear for everyday use. You could easily write a stored procedure too:
Note that in the absence of a functional index, invoking functions on a column in the criterion means that an index is unlikely to be used.
CREATE FUNCTION mmdd(date_val DATE DEFAULT TODAY) RETURNING SMALLINT AS mmdd;
RETURN MONTH(date_val) * 100 + DAY(date_val);
END FUNCTION;
And use it as:
WHERE mmdd(date_col) = 1225;
Depending on how frequently you do this and how fast it needs to run you might think about splitting the date column into day, month and year columns. This would make search faster but cause all sorts of other problems when you want to retrieve a whole date (and also problems in validating that it is a date) - not a great idea.
Assuming speed isn't a probem I would do something like:
select *
FROM Table
WHERE Month(*DateOfBirthColumn*) = *SomeMonth* AND DAY(*DateOfBirthColumn*) = *SomeDay*
I don't have informix in front of me at the moment but I think the syntax is right.
I am trying to user the AddDays function in LINQ to SQL inside the Where clause.
I need to return all the results that are greater than the current date plus a user defined range.
Here is my code.
Where (DateTime.Now >= DateTime.Now.AddDays(UserEnteredDaysToAdd)
What am I doing wrong? or is it even possible?
Thank you,
Edit:
Sorry I made a mistake. I want to get all the records that are in between the current date and an number of days the user enters or is stored in the db.
So it should read, Where (StockOrderDate >= DateTime.Now.AddDays(UserEnteredDaysToAdd))
Lets have a look at your query:
Where (DateTime.Now >= DateTime.Now.AddDays(UserEnteredDaysToAdd))
Apart from not having the right body, you're checking if today is greater or equal to today plus x days. That won't ever be true if you do not allow negative values for UserEnteredDaysToAdd.
Your data structure (result) must have some date property you want to check for, like ResultDate, then your query would look like
IEnumerable<ResultType> results;
...
var resultsAfterDate = results.Where(r => r.ResultDate >= DateTime.Now.AddDays(UserEnteredDaysToAdd));