Formatting numbers according to input precision - sql-server

I have an SSRS report pulling data in from a column of type DECIMAL(9,3). I want to display these values to the number of decimal places used to enter the data into the system.
For example:
Data Output
------- -------
123.456 123.456
123.450 123.45
123.400 123.4
123.006 123.006
120.000 123
120.000 120
100.000 100
I know you can format numeric data to a specific number of decimal places using the Format property for the column (e.g. N3 for 3dp), and I know that in C# the format string "{0:###}" would achieve the desired result.
I feel like it should be easy to do, but so far I've not found the right syntax to combine the two concepts in SSRS. What am I missing?

You can use the 0.### mask. In this case, it will produce the following results:
0.510 -> 0.51
6.581 -> 6.581
9.10 -> 9.1
Just checked it, works on SSRS 2008/2008 R2.

Related

SSIS - stored procedure, decimal(18,2) columns with values of 0.00, leading zeros being dropped to csv flatfile

I'm really struggling to get 0.00 values saved down to a .csv as 0.00 and not .00 (opened in Notepad++, not Excel issue).
Other posts recommended changing all the outputs to string, this did not help leading zeros are truncated.
Converting to currency via DT_CY (Doen this on mapping and derived column) oddly I just got 0 rather than .00 or 0.00, a colleague believe he fixed it with his conversion.
I've used derived columns to format dates, but not helping to get my leading zeros out.
An older stackflow post suggested this method in a new derived string column
[Price] < 1 ? ([Price] >= 0 ? "0" + (DT_WSTR,18)[Price] : [Price] > -1 ? "-0" + (DT_WSTR,18)(-1 * [Price]) : (DT_WSTR,18)[Price]) : (DT_WSTR,18)[Price]
When I put that in, it complains about the conversion between integer and string.
That post was from way back in 2010. I'm using VS 2019 and all my stored procedure datatypes are decimal(18,2).
I can't really change the stored procedure at this stage so looking for an SSIS solution.
Any tips would be appreciated, thanks.
When I add a dataviewer between Derived Column > Flat File Destination it shows the column I'm working with as 0.0000 (converted to dt_Cy) writes to file as 0. All other values like 7.56 appear as they should. Perplexing...
Resolved it, made sure the source columns were output as numeric(18,2) then derive column to string using the expression I posted in question, output as string and set to string in flat file column output details (albeit slow when to action if you have tons of columns).
All works and happy :)

Convert varchar to integer from substring

using Microsoft SQL Server 2019
So I have parsed out several columns from a large text field and will need to use the parsed data as a multiplier to other fields
here is a single parsed column mostly for context
SUBSTRING(curv.curv_data,
CHARINDEX('PctUsage', curv.curv_data, CHARINDEX('||1', curv.curv_data)) + 9,
(CHARINDEX(')())', curv.curv_data, CHARINDEX('PctUsage', curv.curv_data, CHARINDEX('||2', curv.curv_data))) - 9) - CHARINDEX('PctUsage', curv.curv_data, CHARINDEX('||1', curv.curv_data) + 9)) PctUsage1
Here is the output.
PctUsage1
5
3.5
0.5
6.5
5
9
1
1.3
1.5
25.
4
0.0
1.2
so given there are 0s and 0.5 I am having a hard time turning this column into something useful. the end result I would like is to turn these into usable percentages for example 5 turns into .05 and so on.
I am going to be using the SQL above in a sub-select that will then be used as a multiplier for my working hours field here is an example of my multiplier field
Working_hours
51.600006
0.000000
20.799993
8.399999
28.799999
86.027731
5.199999
This is the line I would like to be able to use
PctUsage1*Working_hours curved_hours,
So working_hours is an Integer already but Pctusage1 is a varchar
Thank you in advance for the help
Okay so i think i have a solution, sorry for any of the trouble you guys have taken i guess writing out my problem really helped me work through it. i used a case statement with try_cast that helped filter out the different problems i was having with using only one solution. the last problem with this is making sure my multiplier is correct for that i had to use CHARINDEX with an imbedded case statement
case when try_cast(PctUsage1 as int) is null then case when CHARINDEX('.',PctUsage1) = 3 then cast(replace(PctUsage1, '.','') as Decimal(5,2))/100 when CHARINDEX('.',PctUsage1) = 2 then cast(replace(PctUsage1, '.','') as Decimal(5,2))/1000 end else try_cast(PctUsage1 as decimal(5,2))/100 end PctUsage1_converted,

SQL Server Arithmetic Overflow with Power() Function

I am attempting to generate a table containing values of 10^(existing_field_value / 10). I can recreate the problem with the following simplified table:
Date, Time, Timer, Overall_Spectra, 6_3_Hz, 20_Hz
10/23/2018, 12:24:13, 0:19:59, 69.7, -17.4, 8.9
10/23/2018, 12:24:14, 0:19:58, 70.8, -31.1, 4.4
10/23/2018, 12:24:15, 0:19:57, 70.7, -28.9, 4.8
10/23/2018, 12:24:16, 0:19:56, 69.0, -27.0, 5.9
The data was imported from a flat text file with the following data types:
Date, date
Time, time(0)
Timer, time(0)
Overall_Spectra, decimal(3,1)
6_3_Hz, decimal(3,1)
20_Hz, decimal(3,1)
Using the T-SQL statement below, I was able to comment out 2 of the 3 referenced fields at a time. Doing so, the desired results are returned for the 6_3_Hz and 20_Hz fields, but for the Overall_Spectra field SSMS returns:
select
POWER(10.000000000000000000000000000000000000,Overall_Spectra/10)
POWER(10.000000000000000000000000000000000000,"6_3_Hz"/10)
Power(10.000000000000000000000000000000000000,"20_Hz"/10)
from sound;
Msg 8115, Level 16, State 6, Line 1
Arithmetic overflow error converting float to data type numeric.
I've referenced the following MS documentation in an attempt to understand what's going on (specifically the Return Types table,) but to no avail:
https://learn.microsoft.com/en-us/sql/t-sql/functions/power-transact-sql?view=sql-server-2017
I'm confused about the point at which SQL employs the float data type, as that seems to be in some way the cause of the error. Any insight into what SQL is doing, or what bone-headed thing I'm doing, would be greatly appreciated!!
The maximum PRECISION on a DECIMAL at least in SQL Server 2014 is 38. You are using POWER(10), any number which the POWER produces a number with more than 38 digits will cause the overflow.
So for instance this: POWER(10.000000000000000000000000000000000000, 19.9 / 10)
Will work as the number produced has <= 38 digits
If you up the number to POWER(10.000000000000000000000000000000000000, 20.9 / 10) you get a number with more than 38 digits and then the overflow error
Please see this as reference to Precision and Scale:
(https://learn.microsoft.com/en-us/sql/t-sql/data-types/precision-scale-and-length-transact-sql?view=sql-server-2017)

Pervasive PSQL Control Centre / Currency data type

Having issues updating a Pervasive PSQL table using Pervasive Control Centre and wonder if anyone can point me in the right direction. I'm struggling to update a field in the table whose type is '254-VB Currency'.
Sample query:
Update TABLE set "remBal" = 100.00 where 'Posting' = 215288;
The value that ends up in the remBal field is 463673729135463.6288
Pervasive version is v10.30. Updating via e.g. VAccess control works fine. It's just Pervasive Control Centre that doesn't.
The VAccess control supports more data types than the standard PSQL engine does. The VB Currency data type is not one that's natively supported in PSQL.
According to MSDN, the Currency data type is defined as:
Currency variables are stored as 64-bit (8-byte) numbers in an integer
format, scaled by 10,000 to give a fixed-point number with 15 digits
to the left of the decimal point and 4 digits to the right. This
representation provides a range of -922,337,203,685,477.5808 to
922,337,203,685,477.5807.
What I would suggest, is enter 100.00 to the database using VAccess, then look at the value in Control Center. You can then use that value in your SQL statement. It's not pretty but it might work.

Truncate (using ROUND) has stopped working after changing column data type

I use the following query on the SQL Server 2008 database of a 3rd party product to generate some reports.
SELECT ROUND(SUM(Price),0,1) AS SumNetPrice FROM Transactions
Eg
SUM(Price): 1.2345678
ROUND(SUM(Price),0,1): 1
This has worked fine until now and removed all of the decimal places. In fact curiously the result was truncated even if I didn't specify the 3rd parameter as per the MSDN information http://msdn.microsoft.com/en-us/library/ms175003.aspx on the ROUND function.
The 3rd party company has now changed the data type of the Price column from 'real' to decimal(22, 7). Unfortunately this now means that I always get 7 decimal places even when I use the truncate option of the ROUND function. So now I get:
ROUND(SUM(Price),0,1): 1.0000000
Shouldn't the ROUND(expression,0,1) truncate the result so I don't get any decimal places? How can I remove these decimal places from the result in the SQL query?
In order to make it working the way it worked earlier is to Convert the type from decimal to real. Here is your updated query to get desired output.
select Cast(ROUND(SUM(Price),0,1) as Real) AS SumNetPrice FROM Transactions
SELECT CAST(ROUND(SUM(Price),0,1) AS DECIMAL(22,0)) AS SumNetPrice FROM Transactions

Resources