SSRS Format a multi-value field - sql

I have a number field in my ssrs report that has multiple values separated by a comma. I want to convert the number to currency but I get an error when the field has more than on value. I'm converting the text box to currency and added the (*) operator to multiply by .01 so my numbers are not inflated. I'm sure the comma is what causing the error as BIDS is expecting a number, but I'm not sure how to write the expression so that it only modifies the numbers in the string.
=Fields!field_A.Value * .01
Field
Number1 = converts to currency($Number1)
Number1, Number2 = #Error
number1, number2, number3 = #error

You can use embedded code in the report to do the processing of the string. This allows you to use the full VB.NET language. From the report menu in SSRS select Report Properties and select the Code section on the left. (This is in VS2010, I'm not sure what version of SSRS you're building in.)
Then you can use a VB.NET function like the one below to do the actual string processing. I'm not sure exactly how you want the output so you'll want to read the MSDN page on the FormatNumber() VB.NET function.
Function ConvertCashString(orig As String)
Dim newString As String = ""
For Each s As String In orig.Split(",")
Dim parsedString As Double
If (Double.TryParse(s, parsedString)) Then
newString += "$" + FormatNumber(parsedString * 0.01, 2, TriState.True, TriState.UseDefault, TriState.False) + ", "
End If
Next
Return newString
End Function
Then in the report itself you can call the function like so:
=Code.ConvertCashString(Fields!dbString.Value)
You should also read the MSDN page: Add Code to a Report (SSRS)

Related

Chinese characters in Access SQL Query

After populating the recordsource the next action is clicking on one of the fields populated to "activate" the record. When clicking this, the goal is that the SEC_ID (A GUID, Number Data Type) is stored as a tempvar and used in future queries. This GUID is also placed in a text box just for a visual debug. However it doesn't put the GUID, it puts random Chinese characters. I've tried to place it into a MsgBox just to also see and it spits out "???????".
My code to populate the rowsource:
Dim componentListSQL As String
If FCSUtilities.AssessmentUoM = "Metric" Then
componentListSQL = "SELECT DISTINCT [100b_Working].SEC_SYS_COMP_ID, [100b_Working].SEC_ID, [110b_RO_Material_Category].MAT_CAT_DESC, [110b_RO_Component_Type].COMP_TYPE_DESC, [110b_RO_Material_Category].MAT_CAT_ID, [110b_RO_Component_Type].COMP_TYPE_ID, [100b_Working].ID_Number, [100b_Working].Model, [100b_Working].Serial_Number, [100b_Working].Capacity, [100b_Working].Manufacturer, [100b_Working].SEC_YEAR_BUILT, ROUND([100b_Working].SEC_QTY, 0) AS SEC_QTY, [100b_Working].UOM_MET_UNIT_ABBR, [100b_Working].UOM_ENG_UNIT_ABBR, [100b_Working].Equipment_Make, [100b_Working].UOM_CONV " _
& "FROM (110b_RO_Units_Conversion INNER JOIN (110b_RO_Component_Type INNER JOIN (110b_RO_Material_Category INNER JOIN 110b_RO_CMC ON [110b_RO_Material_Category].MAT_CAT_ID = [110b_RO_CMC].CMC_MCAT_LINK) ON [110b_RO_Component_Type].COMP_TYPE_ID = [110b_RO_CMC].CMC_CTYPE_LINK) ON [110b_RO_Units_Conversion].UOM_ID = [110b_RO_CMC].CMC_UoM) INNER JOIN 100b_Working ON [110b_RO_CMC].CMC_ID = [100b_Working].SEC_CMC_LINK " _
& "WHERE ((([100b_Working].SEC_SYS_COMP_ID) = [Forms]![200a_MainWindow]![txtDebugCompSysID]) And (([100b_Working].SEC_ID) Is Not Null)) " _
& "ORDER BY [110b_RO_Component_Type].COMP_TYPE_DESC;"
Me![210_ComponentList].Form.RecordSource = componentListSQL
End If
The OnClick event:
Private Sub txtMaterialCategory_Click()
Me.txtActiveSecID.Value = Me.txtSecID.Value
End Sub
The txtSecID appears as a GUID as it should but it's in the txtActiveSecID that it becomes Chinese characters even if I attempt to put it as a tempvar then set it into the txtActiveSecID.
I'm not exactly sure what is going on. Looking at different stacks, it points that it's due to long/memo field but as I said previously, the SEC_ID field data type is Number.
Per MS documentation https://learn.microsoft.com/en-us/office/vba/api/Access.Application.StringFromGUID:
The Microsoft Access database engine stores GUIDs as arrays of type Byte. However, Access can't return Byte data from a control on a form or report. To return the value of a GUID from a control, you must convert it to a string. To convert a GUID to a string, use the StringFromGUID function. To convert a string back to a GUID, use the GUIDFromString function.
StringFromGUID(Me.txtSecID.Value)
However that results in output like:
{guid {2786C27B-CB7C-4DEA-8340-1338532742DE}}
That should still work as filter critera but could do further processing to extract GUID from that string. Use string manipulation functions to remove the {guid header and trailing }. Review Access - GUID value stored in textbox, can't be used in SELECT statements

Vba - Convert number to decimal when using SQL on Ms Access

Trying to convert a number value to decimal, with two decimals (,00), in vba when selecting value from MS Access table.
The Quantity column is of type number and the code Im using to format it is
Dim rstBody As DAO.Recordset
Dim orderBodyQuery As String
orderBodyQuery = "SELECT distinct CONVERT(Decimal(9,2), Quantity) FROM " + mainTable + " WHERE [" + uniqOrderColumn + "] = """ + order + """"
Set rstBody = CurrentDb.OpenRecordset(orderBodyQuery, dbOpenSnapshot)
This results in the error:
Undefined function 'CONVERT' in expression
As the error describes Im guessing Im using the wrong syntax here (SQL Server) but I can't find how to do this.
Please help
For display (text) it would be:
Format([Quantity], "0.00")
To retrieve numeric values rounded by 4/5 to two decimals, it would be:
CCur(Format([Quantity], "0.00"))
To set the Format property, use:
"0.00", or just "Standard".
Surprise! Access doesn't use T-SQL.
I think you want the FORMAT() function.
What are differences between access sql
techonthenet.com/access/functions/

SQL - MS Access - search text between two known text string

I'm looking to extract 3 text part of below string..
Requested Ship Date: 05/31/2016 Please charge shipping to: ups 1234 PO Number: po 1234
05/31/2016
ups 1234
po 1234
Knowing that Requested Ship Date: and Please charge shipping to: and PO Number: never change.. but the character length of the second and third can change..
I'm not very good in SQL Access. I know that in Excel it would be easy with the right and find option.. but in Access?
Is there a way to find what's right of each : and then subtract the result?
Any ideas that can help me?
Using Split() it would be easy to split the input string into pieces and extract the pieces you need. Here is an example from the Access Immediate window.
YourString = "Requested Ship Date: 05/31/2016 Please charge shipping to: ups 1234 PO Number: po 1234"
astrPieces = Split(YourString, " ")
? astrPieces(3)
05/31/2016
? astrPieces(9)
1234
? astrPieces(13)
1234
However, Split() can't be used directly in Access SQL. But, since your query will only be run within an Access session, you could create a VBA function which uses Split() and use that function in your query.
Here is an Immediate window example of such a function in action.
? GetShipData(YourString, "date")
05/31/2016
? GetShipData(YourString, "ups")
1234
? GetShipData(YourString, "po")
1234
So your query could be similar to this:
SELECT
GetShipData([YourFieldName], "date") AS ship_date,
GetShipData([YourFieldName], "ups") AS ups,
GetShipData([YourFieldName], "po") AS po
FROM YourTable;
Here is the function I tested in the Immediate window example above:
Public Function GetShipData(ByVal pInput As String, ByVal pWhichItem As String) As String
Dim astrPieces() As String
Dim strReturn As String
astrPieces = Split(pInput, " ")
Select Case pWhichItem
Case "date"
strReturn = astrPieces(3)
Case "ups"
strReturn = astrPieces(9)
Case "po"
strReturn = astrPieces(13)
Case Else
strReturn = "fail" '<- figure out what you want here
End Select
GetShipData = strReturn
End Function
You will probably want to refine that function in case any of your input strings don't match the pattern of your question's sample string.

does SSRS have a limit on number of fields in the dataset?

I have a data set:
select * from table1 --approximately 100 fields
join table2
on...
join table3
on...
The query works when I test it in SSMS.
However, when I try to run a report with this query as the dataset, I am getting:
I thought that perhaps I'm incorrectly capturing the names of the fields, so instead of doing select * i did select field1, field2...field100 but still getting the same result.
What am I doing wrong?
Please note that I have indeed made sure that all the field names are unique by doing a unique filter in excel.
I ran into the same problem. This was my solution.
1)First test your Reporting boundaries, try to determine the maximum amount of fields you can display.
2)Take your main report and make it a sub report.
3)Pass the remaining values through parameters as a concatenated string
4)Use your new sub-report to parse the parameters string.
Here is some VB code to help. Paste this in your report properties cod section.
'***************************************************************************************************************************
Public Function ListToString(myList As String, Delimiter As String, Optional index As Integer = 0) As String
'-----------------------------------------------------------------------------------
'Purpose:
'----This function splits a list and allows one to access the split list like a programmable array
'Description:
'----Input:
'--------myList: String containing the list created in SSRS
'--------Delimiter: what you used to seperate/ delimit each element
'--------index: the index you want you access
'----Output:
'--------ReturnString: returns Name in the format of "FirstName LastName"
'Version Control log: (Date - Name: Description)
'----xx/xx/xxxx Adrian Williams Creation of function
'-----------------------------------------------------------------------------------
Dim returnString As String = ""
Dim myArray As String()
myArray = myList.split(delimiter)
returnString = trim(myArray(index))
Return returnString
End Function
'***************************************************************************************************************************

How to convert an string to a Char(20) sql datatype

I am trying to insert a string into a char(20) column in my SQL Server database.
I set the string to be equal to a value and then add it to a parameter
thisstring = "1914"
cmd.Parameters.AddWithValue("#code", thisstring)
However every time I try it ends up looking like this in the database
I need it to look like the 1913, 000000, 000001 ones.
When I try to pad the string with spaces
thisstring= thisstring.PadLeft(20, " ")
or
thisstring = " " & thisstring
I am getting
String or binary data would be truncated
even if the field wasn't 20 characters total
What am I doing wrong?
Edit*** here is the column in SQL Server
http://imgur.com/BbV6VQv
I am not absolutely sure, but I think the problem lies in the AddWithValue.
While convenient this method doesn't allow to specify the exact datatype to pass to the database engine and neither the size of the parameter. It pass always an nvarchar parameter for a C# UNICODE string.
I think you should try with the standard syntax used to build a parameter
Dim p = new SqlParameter("#code", SqlDbType.Char, 20)
p.Value = thisstring.PadLeft(20, " ")
See this very interesting article on MSDN