?
Solved

Trim decimal places from Varchar field

Posted on 2013-10-31
7
Medium Priority
?
375 Views
Last Modified: 2013-10-31
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
Comment
Question by:JElster
7 Comments
 
LVL 27

Expert Comment

by:Shaun Kline
ID: 39614325
The ROUND function would work.
ROUND(<value>, 2, 1)
0
 
LVL 27

Accepted Solution

by:
Shaun Kline earned 440 total points
ID: 39614331
Sorry, forgot about the varchar.

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

Author Comment

by:JElster
ID: 39614337
Error converting data type varchar to float.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 27

Expert Comment

by:Shaun Kline
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

by:Jim Horn
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) 

Open in new window

This does beg the obvious question though, why is a column with numeric values a varchar data type?
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
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

by:Shaun Kline
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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

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…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question