Solved

Update the Price field and format digits after decimal point

Posted on 2011-09-23
4
396 Views
Last Modified: 2012-06-27
I have a Price field that has values like :
12.18, 45.78, 9.66, etc...
I want to Update the Price field and format digits after decimal point.

I want the last 2 digits after the decimal point to be rounded like this :

If :  .00 to .29  =  .29

If :  .30 to .49  =  .49

If :  .50 to .69  =  .69

If :  .70 to .99  =  .95

So if  the price is "29.14", it would be "29.29", . . . "18.78" would be "18.99", etc.

UPDATE Nop_ProductVariant
SET Price =

How can I do this? thanks
0
Comment
Question by:MikeMCSD
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
Bhavesh Shah earned 500 total points
ID: 36587783
Hi,

check out this.
It might helps.


- Bhavesh
declare @var numeric(30,2)
set @var = 15.78
select case when @var-floor(@var) >= 0.70 then floor(@var) + 0.95 
		else case when @var-floor(@var) >= 0.50 then floor(@var) + 0.69
		else case when @var-floor(@var) >= 0.30 then floor(@var) + 0.49
		else case when @var-floor(@var) >= 0 then floor(@var) + 0.29
		end end end end as Price

Open in new window

0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 36588087
You can also try this and see if it helps:

update Nop_ProductVariant
set price = case when price between 19.00 and 29.14 then LEFT(CONVERT(VARCHAR(20),price),2)+'.29' end)

If that works, then add more conditions like:

when price between 0.00 and 18.78 then LEFT(CONVERT(VARCHAR(20),price),2)+'.99' end)




 
0
 
LVL 16

Author Comment

by:MikeMCSD
ID: 36588410
thanks Brichsoft  .. that's perfect.

How do I use it in the Update statement ?

UPDATE Nop_ProductVariant
SET Price =

select case when @var-floor(@var) >= 0.70 then floor(@var) + 0.95
            else case when @var-floor(@var) >= 0.50 then floor(@var) + 0.69
            else case when @var-floor(@var) >= 0.30 then floor(@var) + 0.49
            else case when @var-floor(@var) >= 0 then floor(@var) + 0.29
            end end end end as Price
0
 
LVL 16

Author Comment

by:MikeMCSD
ID: 36590196
I'm not good with the CASE statement, but I think this worked :

UPDATE Nop_ProductVariant
SET Price =
CASE WHEN Price-floor(Price) >= 0.70 THEN floor(Price) + 0.95 ELSE
CASE WHEN Price-floor(Price) >= 0.50 THEN floor(Price) + 0.69 ELSE
CASE WHEN Price-floor(Price) >= 0.30 THEN floor(Price) + 0.49 ELSE
CASE WHEN Price-floor(Price) >= 0 THEN floor(Price) + 0.29
END END END END

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Help 27 45
SQL - Update field defined as Text 6 17
sqlserver get datetime field and create a string 5 18
syntax sql error 2 14
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

803 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