Format date to concatenate with text - excel-2016

I need to format a invoice number that needs to be in a specific format (yyyymm(Textvalue)
=YEAR(D5) &""& MONTH(D5) & "MarCall" = currently returns: 20165MarCall
Need to check for two digit month.

This should help you:
=IF(MONTH(D5)<10, "0" & MONTH(D5), MONTH(D5))

Related

Using DCount in Access with multiple criteria

I am trying to count the number of records in a table which satisfy criteria for two different fields. Both fields are string values.
The first field is what type of Test appears e.g. 'Manometry'. I can get this field to work on it's own.
I experience a problem when trying to add the second criteria.
The second field is the TestID, which is in the format A_155_19, where 155 is the investigation number and 19 identifies the year it took place.
I would like to count all the manometry tests which occur in the current year.
DCount("[Test]", "Visits", "[Test] = 'Manometry'" & "[TestID] = *Right((Year(Date)), 2)'")
I am currently getting the error message 3075, which is missing syntax.
Any help would be greatly appreciated.
What about:
DCount("[Test]", "Visits", "[Test] = 'Manometry' And [TestID] Like " & SomeExpression)
You can use Format:
DCount("*", "Visits", "[Test] = 'Manometry' And [TestID] Like '*_' & Format(Date(), "yy") & "'")

SSRS lookup with datediff Incorrect result error showing in fields

I have to write again this problem. I am not an expert in SSRS.
I have been trying to resolve this issue. Even though the fields are all the same datatype am still getting error and incorrect time difference in the filed in some fields.
I do not know what am doing wrong. This is the code. Please find below screen shot also.
enter code =Datediff(DateInterval.Minute, Fields!health_start_date.Value, Lookup(Fields!flight_date.Value & Fields!register_number.Value & DATEPART(DateInterval.Hour,Fields!health_start_date.Value) , Fields!FL_DATE.Value & Fields!REG.Value & DatePart(DateInterval.Hour, Fields!ATD.Value), Fields!ATD.Value, "DataSetAIMS") ) MOD 60 & " mins "
here
The scenario is that the two fields Health start date and ATD have two different dataset which shows time of flight differently but the time difference is always in minutes. What am trying to do is to compare the two table with their hour and date using lookup to find difference in minutes
Okay, I think I see the problem: you're really close and this stuff trips us all up on occasion. In the LOOKUP function, I think you are joining the two data sets based on:
flight_date = FL_DATE, and
register_number = REG, and
HOUR(health_start_date) = HOUR(ATD)
Which is fine, but the ampersand (&) just "concatenates" strings together, and (a) your strings don't actually match, and (b) HOUR is a number, not a string.
(a) The "date" in the first data set is DDMMYYYY, and in the 2nd it is DDMMYY. I.e., "05222018" is not the same as "052818", so your lookup will not work. It's possible that they are stored correctly as date fields in the database and so the comparison will work fine, but better to force them to match. For that I'd recommend formatting each of the date strings into a standard format, like "FormatDateTime(Fields!flight_date.Value, DateFormat.VBShortDate)".
(b) Should be easy, just add ".ToString" after the parentheses, like "DATEPART(DateInterval.Hour,Fields!health_start_date.Value).ToString()".
(c) I'm assuming the register_number and REG are formatted the same.
Given all that, your new LOOKUP function might look something like this:
=Datediff(DateInterval.Minute,
Fields!health_start_date.Value,
Lookup(
FormatDateTime(Fields!flight_date.Value, DateFormat.VBShortDate) & Fields!register_number.Value & DATEPART(DateInterval.Hour,Fields!health_start_date.Value).ToString() ,
FormatDateTime(Fields!FL_DATE.Value, DateFormat.VBShortDate) & Fields!REG.Value & DatePart(DateInterval.Hour, Fields!ATD.Value).ToString()
, Fields!ATD.Value
, "DataSetAIMS")
).ToString() & " minutes"
However, I think you'd make your life easier by just doing that sort of calculation in the database layer, in the report query, something like:
SELECT
d1.FlightDate
, d1.RegisterNumber
, d1.HealthStartDate
, d2.ATD
, DiffInMinutes = DATEDIFF(MINUTE, d1.HealthStartDate, d2.ATD)
FROM DataSet1 d1
JOIN DataSetAIMS d2
ON CAST(d1.FlightDate AS DATE) = CAST(d2.FL_DATE AS DATE)
AND d1.register_number = d2.REG
AND DATEPART(HOUR, d1.health_start_date) = DATEPART(HOUR, d2.ATD)
This code certainly isn't exact, but hopefully it will get you pointed in the right direction!
Thanks Russel. I have accepted your answer. What I did was to remove the formatdate from the expression code and modify the query design code to convert to the same time date : the code below
=Datediff(DateInterval.Minute, Fields!health_start_date.Value, Lookup(Fields!register_number.Value & DATEPART(DateInterval.Hour,Fields!health_start_date.Value).ToString() , Fields!REG.Value & DatePart(DateInterval.Hour, Fields!ATD.Value).ToString(), Fields!ATD.Value, "DataSetAIMS") ).ToString() MOD 60 & " mins "
or the below as well works :
=Datediff(DateInterval.Minute,
Fields!health_start_date.Value,
Lookup(
Format(Fields!flight_date.Value, "ddMMyy") & Fields!register_number.Value & DATEPART(DateInterval.Hour,Fields!health_start_date.Value).ToString() ,
Format(Fields!FL_DATE.Value, "ddMMyy") & Fields!REG.Value & DatePart(DateInterval.Hour,Fields!ATD.Value).ToString()
, Fields!ATD.Value
, "DataSetAIMS")
).ToString() MOD 60 & " minutes

SQL: Compare Date Range in a String text field

I have an MS Access db. I am writing an application in C# to access it. I have a field "FileName" of type "Short Text in MS Access. Data in FileName field is like "Test 11-12-2004 15.11.15".
Using a Date Range, I got to search records based on FileName field. I am not able to get - How do I compare the date of this format and retrieve the records ? FileName is a Text type and date is a substring of it. Retrieving only the date part and comparing with >= beginDate && <= endDate seems like a puzzle to me.
Can anyone suggest how do I write SQL query to perform this date range comparision and retrieve those records - "Select * from TestHead where FileName......" ????
Any help is appreciated.
Thanks a lot,
In your C# code, as you are going through the records, I'd split the string like this:
char[] delimiters = {' '};
string[] FileNameParts = FileName.Split(delimiters);
This will result in an array FileNameParts, the second element of which will contain the date, which you can convert to an actual date for use in the query:
DateTime FileNameDate = Convert.ToDateTime(FileNameParts(1))
Something along the lines of:
sSQL = "SELECT * FROM Table WHERE " & beginDate & " <= " & FileNameDate
I see this as preferable to adding a column to your table that contains the date substring of the FileName field, because then you constantly need to be updating that column whenever existing records are modified or new records are added. That means more clutter on the C# side, or an UPDATE query on the Access side which at least needs to get called periodically. Either way it would be more communication with the database.

Access: Passing Parameters from Form to Query

I'm writing a query in Access 2013. The query will use the parameters from a form, pass those parameters to the query, and then display the results. I almost have it working. The problem with this form is that there are multiple date fields. There are two text fields (txtFromDate and txtToDate).
The idea is that user can enter date range one of two ways:
the user can click the date picker and select a date range to pass to the query;
the user can choose the Fiscal Year from a combobox.
I have created a separate table that is setup as follows:
Table: tbl_FiscalYear
Columns: FiscalYr_ID, FiscalYearName, FromDate, ToDate
In my query, the results work great (all parameters passed to query) if the user enters the manual dates in the txt fields. However, if they bypass the manual date entry and use the Fiscal Year combobox, the query results ignore all previous parameters and the results only reflect the appropriate date range per the Fiscal Year date setup in the fiscal year table.
I think the culprit is the OR statement at the end. Is there a way to incorporate both date search parameters into the form, and allowing the use of one or the other? Is my code not correct since it doesn't allow passing null values from the text date fields?
SELECT tbl_QUOTE.QUOTE_ID,
tbl_QUOTE.QUOTE_DATESTART,
tbl_QUOTE.QUOTE_DATEEND,
tbl_QUOTE.QUOTE_lookup_POC_ID,
tbl_QUOTE.QUOTE_lookup_LOC_ID,
tbl_QUOTE.QUOTE_lookup_TRAINER_NAME,
tbl_QUOTE.QUOTE_lookup_STATUS_ID,
tbl_QUOTE.QUOTE_lookup_TRAINER_ID,
tbl_QUOTE.QUOTE_lookup_MODS_ID,
tbl_POC.POC_AGENCY_lookup,
tbl_QUOTE.QUOTE_FINANCIAL_CD,
tbl_FISCALYEAR.FISCALYEARNAME
FROM tbl_fiscalyear, tbl_QUOTE
INNER JOIN tbl_POC ON tbl_QUOTE.QUOTE_lookup_POC_ID = tbl_POC.POC_ID
WHERE Nz([QUOTE_lookup_STATUS_ID],"") Like [Forms]![frm_QuoteReport]![cboStatusLookup] & "*"
AND Nz([tbl_POC].[POC_AGENCY_lookup],"") Like [Forms]![frm_QuoteReport]![cboAgency] & "*"
AND Nz([QUOTE_lookup_POC_ID],"") Like [Forms]![frm_QuoteReport]![cboPOC] & "*"
AND Nz([QUOTE_lookup_MODS_ID],"") Like [Forms]![frm_QuoteReport]![cboCourse] & "*"
AND Nz([QUOTE_lookup_LOC_ID],"") Like [Forms]![frm_QuoteReport]![cboLocation] & "*"
AND Nz([QUOTE_lookup_Trainer_ID],"") Like [Forms]![frm_QuoteReport]![cboTrainer] & "*"
AND Nz([Fiscalyr_ID],"") Like [Forms]![frm_QuoteReport]![cboFY] & "*"
AND Nz([Quote_Financial_Cd],"") Like [Forms]![frm_QuoteReport]![chkFundCd] & "*"
AND [QUOTE_DATESTART] Between [Forms]![frm_QuoteReport]![txtFromDate]
And [Forms]![frm_QuoteReport]![txtToDate]
OR [QUOTE_DATESTART]
BETWEEN (SELECT [tbl_fiscalyear].[fromdate]
FROM tbl_fiscalyear
WHERE fiscalyr_ID = [Forms]![frm_QuoteReport]![cboFY])
AND (SELECT [tbl_fiscalyear].[todate]
FROM tbl_fiscalyear
WHERE fiscalyr_ID = [Forms]![frm_QuoteReport]![cboFY]);
I believe I resolved it. I edited the final lines as follows:
OR [QUOTE_DATESTART] Between [Forms]![frm_QuotesReports]![txtFromDate]
And [Forms]![frm_QuotesReports]![txtToDate] <br/> AND [QUOTE_DATESTART]
BETWEEN (SELECT [tbl_fiscalyear].[fromdate] FROM tbl_fiscalyear WHERE
fiscalyr_ID = [Forms]![frm_QuotesReports]![cboFY]) AND (SELECT
[tbl_fiscalyear].[todate] FROM tbl_fiscalyear WHERE fiscalyr_ID = [Forms]!
[frm_QuotesReports]![cboFY])

How do I format the date to include the week number?

I am using Microsoft Business Intelligence Development Studio 2012 trying to create a calculated field that will give me the Month and Week number of a field with the name 'createdon'.
I am new to SQL and after some research this is the code that I have come up with.
=Format(Fields!createdon.Value, "MMMM") & " Week " & (Int(DateDiff("d",DateSerial(Year(Fields!createdon.Value),Month(Fields!createdon.Value),1), Fields!FullDateAlternateKey.Value)/7)+1).ToString
But I am getting the error:
"The Field expression for the dataset 'Dataset1' refers to the field
'FullDateAlternateKey'. Report item expressions can only refer to
fields within the current datasetscop or, if inside an aggregate, the
specified dataset scope. Letters in the names of fields my use the
correct case.
I know this means that "FullDateAlternateKey" is not a valid field name, but I do not know what field name to substitute instead to correct this expression. Does anyone with more experience have any ideas as to how I can correct this?
Ideally, I would like it formatted to have a column that shows "week ending in ".
So any dates from 9/26/2015 to 10/2/2015 would say "Week of 10/2/2015"
Try this:
="Month: " & Datepart("m", Fields!createdon.Value) & " Week: " &
Datepart(DateInterval.WeekOfYear,Fields!createdon.Value)
You will get the following: Month: 10 Week: 43
Edit your question and add a example of the desired result in order to help you with the specific format.
Update:
Try this:
="Today: " & Format(Fields!createdon.Value,"dd/M/yyyy") & "Week Of: " & Format(
DATEADD("d" ,7-DATEPART(DateInterval.WeekDay,Fields!createdon.Value,FirstDayOfWeek.Monday),Fields!createdon.Value),
"dd/M/yyyy")
It will show: Today: 19/10/2015 Week Of: 25/10/2015