0

Overall question:

I have a table in my SQL Server with 3 columns (ID, Value, Decimal). So what I want to do in theory is SELECT the column Value and it should be formatted to the number of decimals specified in that same row.

What I've tried:

SELECT CONVERT(Decimal(18,s.Decimal), s.Value ) FROM Table s

and similar queries with CAST and FORMAT

What I want:

Table:

ID  Value        Decimal
1   500.754254   2

When I run the query it must return Value as '500.75'

Extra info: I am displaying the result in a WPF (C#) datagrid, and if it will be easier to format there, I don't mind. I just don't know how to.

6
  • What is the datatype of Value? Commented Oct 9, 2018 at 8:38
  • In a comment below you state: I am working with medical risk categories and every category has got a different number of decimals. If I get this correctly you should not use a numeric type at all. What happens if your value is 500.756 and you get it rounded as 500.76? I suggest to use a string type and store the part before the dot and the part behind in two different columns. Combine them only for your output... Commented Oct 9, 2018 at 8:44
  • That is a very valid point. For all calculations we are using the full value, in the table it is a [Decimal(18,6)] so what happened was it was displaying for some columns 123.120000 instead of just the required amount of decimals which is also specified. Commented Oct 9, 2018 at 8:57
  • @SalmanA it is an Int datatype Commented Oct 9, 2018 at 8:58
  • @Hancs you mean decimal(18, 6)? Since it is for display purposes you could use the format function in SQL server or format it in the application. Commented Oct 9, 2018 at 9:10

2 Answers 2

1

This is a presentation issue, and as such, should be handled by the presentation layer. c# have very simple ways to format the output, but as you wrote in your question, you would like to know how to do it in T-SQL - so here is one option, using cast, charindex and left.

First, create and populate sample table (Please save us this step in your future questions):

DECLARE @T AS TABLE
(
    id int identity(1,1),
    [Value] decimal (18,10),
    [Decimal] int
)

INSERT INTO @T ([Value], [Decimal]) VALUES
(500.754254, 2),
(50.75636, 3),
(12345.7254, 1),
(1.424, 4)

The query:

SELECT  ID, 
        Value, 
        [Decimal],
        LEFT(CAST([Value] As varchar(18)), CHARINDEX('.', CAST([Value] As varchar(18))) + [Decimal]) As [Left] 
FROM @T

Results:

ID  Value               Decimal Left
1   500,7542540000      2       500.75
2   50,7563600000       3       50.756
3   12345,7254000000    1       12345.7
4   1,4240000000        4       1.4240
Sign up to request clarification or add additional context in comments.

Comments

1

We can try using FORMAT here:

SELECT FORMAT(123.123456, '##.' + LEFT('##########', Decimal))
FROM yourTable;

Demo

The trick above is that the width of the decimal component of the mask is determined by the value of the Decimal column. If you expect to need more than 10 decimal places, then just widen the ### string literal.

Another option might be using ROUND:

SELECT ROUND(Value, Decimal)
FROM yourTable;

However, this sort of formatting is usually best handled in your presentation layer. So, I would probably be looking to WPF/C# for a solution.

9 Comments

Thanks for helping, it is not returning the exact answer expected but far better than I have gotten link (see the image of result set)
Maybe I should just explain why it should show the exact decimal, I am working with medical risk categories and every category has got a different number of decimals
If you can show me what is wrong with my answer, I'm happy to edit. Please do not paste a bunch of data here as comment. Instead, maybe setup a demo and paste the link.
Check the link in my first comment. That is the result set from your query
@Hancs Your image link is not readable at all, and I don't understand what those numbers. Check this demo to see that my first option with FORMAT is working.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.