SQL SSRS add if condition into ssrs expression - sql

I am creating a simple report in SSRS
One of the fields is an address, which has this expression value
=First(Fields!BILL_CITY.Value) & ", " & Fields!BILL_PROV.Value & " " & Fields!BILL_ZIP.Value
What I want to do, is within that expression within SSRS, I want to add a condition.
If the Fields!BILL_ZIP.Value is only 5 digits, do nothing.
If the value is more than 5 digits, add a dash after the fifth digit.
So if customer sends the shorthand version of the zipcode it will appear as
45040 but if he sends the long version, it will be 45040-8999
Does anybody know how to do this? Creating the condition probably wouldn't be too hard, but how do I put it within the SSRS expression format?

Try:
= IIF(LENGTH(Fields!BILL_ZIP.Value) = 5, Fields!BILL_ZIP.Value & "-", Fields!BILL_ZIP.Value)

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") & "'")

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

Access 2007 VBA: Returning data from SQL statements that use multiple variables

I am working on a form that is meant to analyze financial data ahead of an insurance policy renewal. It needs to pull various premium and claim $$ totals from the tables, and insert them into the form. From there, it will run some calculations with them, but that should be the easy part. Where I'm struggling is the SQL statements to get the data, in the first place.
After narrowing it down a bit, I've found that the problem is the code is putting the SQL statement into the field on the form, instead of the answer the SQL statement should be providing. I have tried multiple things I've seen online, and can't figure out how to resolve this.
One of the SQL statements reads like this:
L12W = "SELECT Sum(tblActPrem.APWrit) AS SumOfAPWrit FROM tblActPrem " _
& " WHERE tblActPrem.EntID = '" & Me.ctlActEntID & "' " _
& " AND tblActPrem.PolNum = '" & Me.ctltblRnwlTrack_PolNum & "'" _
& " AND tblActPrem.APDate BETWEEN #" & L12M & "# AND #" & Me.ctlRnwAnalysisDt & "#;"""
It should be totalling premium data from the table, where the policy number and account number match what's on the form, and between the selected dates, and putting that total into the field on the form. Instead, I get this on the form:
SELECT Sum(tblActPrem.APWrit) AS SumOfAPWrit FROM tblActPrem WHERE tblActPrem.EntID = '1235' AND tblActPrem.PolNum = 'Policy1' AND tblActPrem.APDate BETWEEN #1/1/2014# AND #1/1/2015#;"
That is exactly how the statement should be running, but can anyone tell me how to make the leap from the statement, to the data?
Thank you!
Consider using DLookup or DSum in the control source of the form's field.
DLookup Solution
First, change your VBA SQL query into a stored SQL Query (notice quotes and # symbols are not necessary when referencing form controls):
SELECT Sum(tblActPrem.APWrit) AS SumOfAPWrit
FROM tblActPrem
WHERE tblActPrem.EntID = Forms!<yourformname>!ctlActEntID
AND tblActPrem.PolNum = Forms!<yourformname>!ctltblRnwlTrack_PolNum
AND tblActPrem.APDate BETWEEN Forms!<yourformname>!L12M
AND Forms!<yourformname>!ctlRnwAnalysisDt
And then in the form field textbox's control source use the DLookUp function (no criteria argument is needed since this should return only one value).
= DLookUp("SumOfAPWrit", "<yournewqueryname>")
DSum Solution
Alternatively, you can change the entire SQL statement into a DSum but notice how long the criteria (WHERE statement) would have to be.
= DSum("APWrit", "tblActPrem", "EntID = Forms!<yourformname>!ctlActEntID
AND PolNum = Forms!<yourformname>!ctltblRnwlTrack_PolNum
AND APDate BETWEEN Forms!<yourformname>!L12M AND Forms!<yourformname>!ctlRnwAnalysisDt")

VB.NET -> SQL Exporting String "901-0656" to not be 245 (901 minus 656)

I am trying to add new records to a Access database through VB.NET. The table "Quantities" has three columns:
PartNumber (string), PadsPerStrip (integer), and Verified (boolean)
The format that we use for PartNumber is ###-####-### (ie 901-0656-000). When I run my code everything is added correctly but math is performed on Part number so that the - is treated as a minus sign even though it is a string. Here is my sql command:
cmdInsert.CommandText = "INSERT INTO Quantities (PartNumber, PadsPerStrip, Verified) VALUES ( " & partNum & ", " & updatingPPS.ToString() & ", No);"
When viewing the command in a MsgBox it shows up as:
INSERT INTO Quantities (PartNumber, PadsPerStrip, Verified) VALUES (901-0656-000, 3, No);
Is there a way to make it skip the math operator when exporting the part number?
Make sure to enclose the part number with quotes. Without that, the value is not considered as a string when run in the database.

How do I save the result of an SQL COUNT query with VBA in Access 2007?

I'm trying to count the number of records in a table that meet a certain criteria. My preference is to use SQL, not Dcount, as I want to get better at SQL. Here's my current code below:
Dim countString As String
Dim count
countString = "SELECT COUNT(*) FROM `Engagement Letters` WHERE 'Client ID' = " & Me.cboSelectClient
count = CurrentDb.OpenRecordset(countString).Fields(0).Value
Yeah I know, I've used spaces in my tables and field names - I will change that. Though I think I should still be able to run this query as is, so I will leave it as is for now.
When I run the above, I get runtime error 3464 - data type mismatch in criteria expression. I've had the below dcount function work fine:
count = DCount("[Engagement Letter ID]", "Engagement Letters", "[Client ID] = " & Me.cboSelectClient)
And also the below COUNT query without the WHERE works fine:
"SELECT COUNT(*) FROM `Engagement Letters`"
My knowledge of SQL is very minimal, and my knowledge of more advanced VBA is also quite minimal, so I'm not sure where I'm going wrong. Can anyone help me with this?
Try building your string like this.
countString = "SELECT COUNT(*) FROM [Engagement Letters]" & vbCrLf & _
"WHERE [Client ID] = " & Me.cboSelectClient
Debug.Print countString
Use square brackets around object (table and field) names which include spaces or any characters other than letters, digits, and the underscore character.
For the table name, you used `Engagement Letters`, and the backticks appear to work the same as square brackets. Perhaps they always work equally well, but I don't know for sure because I use the brackets exclusively. And brackets instead of backticks might help you avoid this mistake ...
WHERE 'Client ID' = " & Me.cboSelectClient
... that was asking the db engine to compare the literal string, "Client ID", to the numerical value (?) you pulled from cboSelectClient.
I used vbCrLf between the 2 parts of the SELECT statement because I find that convenient when examining the completed string (via Debug.Print).