Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Trim decimal places from Varchar field

Posted on 2013-10-31
Medium Priority
373 Views
Hi.. I have varchar field with values like this
00.1231556
123.99999999
56.12344455

Only need to 2 decimal place

00.12
123.99
56.123

what's the syntax ?
0
Question by:JElster
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 27

Expert Comment

ID: 39614325
The ROUND function would work.
ROUND(<value>, 2, 1)
0

LVL 27

Accepted Solution

Shaun Kline earned 440 total points
ID: 39614331

So CAST(ROUND(CAST(<value> AS decimal), 2, 1) AS varchar)
0

LVL 1

Author Comment

ID: 39614337
Error converting data type varchar to float.
0

LVL 27

Expert Comment

ID: 39614342
Another option would be to use CHARINDEX to find the position of the period, add 2 to that value and then use LEFT:

LEFT(<value>, CHARINDEX('.', <value>) + 2)
0

LVL 66

Assisted Solution

Jim Horn earned 440 total points
ID: 39614345
Shaun_Kline is correct.   Slight correction though, adding percision and scale to the decimal
``````Declare @str varchar(10) = '123.456'

SELECT CAST(ROUND(CAST(@str AS decimal(5,2)), 2, 1) AS varchar)
``````
This does beg the obvious question though, why is a column with numeric values a varchar data type?
0

LVL 53

Expert Comment

ID: 39614361
As you have posted this in javascript, I assume you want the javascript to pre-format for you and the code for that is:

var twoPlacedFloat = parseFloat(yourString).toFixed(2);

Cd&
0

LVL 27

Expert Comment

ID: 39614362
Based on the error you posted, I'm guessing you have non-numeric data (maybe empty strings or NULLs) in that field as well. You can use the ISNUMERIC function to filter out records that does not contain numbers in your field.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that witâ€¦
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
###### Suggested Courses
Course of the Month9 days, 22 hours left to enroll