|
Hi,
I'm using ROUND in my SQL statement but I'm not getting the end result that I want. Using MS Access db with C#
Here is the statement
SELECT Price - ROUND(Price * Percent ,3)
Price = 28.50
Percent = 0.25
The end result I would like is: 28.50 - 7.13 = 21.37
What I'm getting is 21.38, it's like the ROUND is taking the whole statement and rounding the final result 21.375. Shouldn't it just ROUND what is in the ()?
Thanks for any help you can give,
Ron
|
|
|
|
|
SQL Round function rounds to the nearest even value.
select 28.50 - ROUND(28.50 * 0.25, 3) returns 21.375 (which will round up to 21.38)
select Round(28.50 - ROUND(28.50 * 0.25, 3),2) returns 21.38 (rounds up to 21.38)
If the precision in the rounding is chaged to 2 for the 'price*percent' term:
select 28.50 - ROUND(28.50 * 0.25, 2) returns 21.37
select Round(28.50 - ROUND(28.50 * 0.25, 2),2) returns 21.37
We need to graduate from the ridiculous notion that greed is some kind of elixir for capitalism - it's the downfall of capitalism. Self-interest, maybe, but self-interest run amok does not serve anyone. The core value of conscious capitalism is enlightened self-interest.
Patricia Aburdene
|
|
|
|
|
Thanks Rob, I really appreciate the info!
It looks like I shouldn't use ROUND, in my SQL because I'm wanting cash register type of rounding (always round up). It's too bad they don't have parameter options to select the type of rounding you want.
I have one other issue if you can help. I notice that if I use SUM(), it truncates to the decimal place the db columns are set to. Sometimes the SQL statement will calculate a value over the two decimal places. I need the SUM to give me at least 3 decimal places, so I can round up the value calculated. I can set one of the columns in the db (to 3 decimals) to achieve this, but I was wondering if there was a way to do this at the SQL statement?
Thanks!
Ron
|
|
|
|
|
myNameIsRon wrote: It looks like I shouldn't use ROUND, in my SQL because I'm wanting cash register type of rounding (always round up). It's too bad they don't have parameter options to select the type of rounding you want.
Actually, SQL servers Round( a,b,c) funtion is parameterized to select between "round up" (c==0) and truncate (C!=0)
select Round(1.5,0,0) -> 2.0 round up .5+
select round(2.5,0,0) -> 3.0 round up 2.5 and greater
select round(2.4,0,0) -> 2.0 round down 2.0 to 2.5 exclusive
select Round(1.5,0,1) -> 1.0
select round(2.5,0,1) -> 2.0
I think the problem with your example was just the wrong choice for the b ( # decimals)
parameter - you used 3, when what you wanted was 2 (assuming you wanted to round .005
and larger to .01 and .004 and smaller to 0.00. If what you realy wanted was to round
.11+ TO .12 , then use Ceiling(x *100)/100 - round any fraction to next highest integer
a good discussion of the varous rounding schemes can be found here.[^]
I think your observation on SUM() is correct, I know of no other solution.
We need to graduate from the ridiculous notion that greed is some kind of elixir for capitalism - it's the downfall of capitalism. Self-interest, maybe, but self-interest run amok does not serve anyone. The core value of conscious capitalism is enlightened self-interest.
Patricia Aburdene
-- modified at 18:31 Sunday 7th May, 2006
|
|
|
|
|
Thanks again for the great info Rob!
Yes, I should have been using 2 for the decimal place to round.
It apprears that with MS Access, I can't use the "c" parameter (I get an error that I'm using too many parameters).
I really don't want Bankers Rounding (even # to round up):
Round(0.005,2) = 0.00
Round(7.125,2) = 7.12
I want old school rounding )
Round(0.005,2) = 0.01
Round(7.125,2) = 7.13
Do you think I'll have to round the SQL result using something like this:
Floor(x * 100 + 0.5m)/100
to get what want?
thanks,
Ron
|
|
|
|
|
myNameIsRon wrote: really don't want Bankers Rounding (even # to round up):
SQL server uses "round up" not Banker's rounding (I was wrong about that in the first post). I think if you just change the 3 to a 2 you'll get what you want from SQL. Access may be a diffewrent matter, check the link in the previous post: it details rounding for all microsoft products (they are very inconsistent).
We need to graduate from the ridiculous notion that greed is some kind of elixir for capitalism - it's the downfall of capitalism. Self-interest, maybe, but self-interest run amok does not serve anyone. The core value of conscious capitalism is enlightened self-interest.
Patricia Aburdene
|
|
|
|
|
Hi Rob,
I did a bunch of tests and unfortunately Access does use the Banker's rounding when using ROUND. I was able to solve both my issues (SUM truncating and ROUND up only on even) though by using FORMAT on the calculations that could cause values to go past the second decimal place. I didn't think I could SUM on a FORMAT because I thought FORMAT converts the value to a string... but it works ok.
SUM ( FORMAT( price * percent, 'Standard' ) )
I really appreciate all your help, I'm new to programming and find almost everything a challenge… but a lot of fun too ).
Say, I have a different question about pulling data from a database...
Sometimes I want to use \n to give me a new line in a MessageBox.Show().
This works ok as a string to use:
string info = "Name\nAddress\nPhone";
but when I try to use a string that has been pulled from a database, the \n does not give me a new line (just shows up). Is there a way to change the string to work with \n again?
Thanks again,
Ron
|
|
|
|
|
Yea, "\n" is an "escape sequence" that the compiler understands to replace with the non-printing newline character(s), but embedded in a db string it is just \n...
You might be able to pass the db string through a format function and get the newlines replaced. If you're using C# try String.Format(dbString). C++ CString.Format(dbstring).
I haven't tried this, and I'm just guessing, but that is what I would try first.
We need to graduate from the ridiculous notion that greed is some kind of elixir for capitalism - it's the downfall of capitalism. Self-interest, maybe, but self-interest run amok does not serve anyone. The core value of conscious capitalism is enlightened self-interest.
Patricia Aburdene
|
|
|
|
|
|
Hey, "Formula Fields" in Reporting Service (SQL 2005), is it in VBScript?
Norman Fung
|
|
|
|
|
I have a table on Microsoft Access and I need to export it to a CSV file when I press a button on an Access form. I'm using some Transact-SQL code inside VBA, but nothing yet. Any commentaries?
|
|
|
|
|
Microsoft Access does not use TRANSACT-SQL. That dialect is specific to SQL server.
If you post the relevant parts of the VBA code, someone might be able to help...otherwise you're asking us to guess, which most of us won't wate our time on
We need to graduate from the ridiculous notion that greed is some kind of elixir for capitalism - it's the downfall of capitalism. Self-interest, maybe, but self-interest run amok does not serve anyone. The core value of conscious capitalism is enlightened self-interest.
Patricia Aburdene
|
|
|
|
|
ok...let me re-phrase the question...this is the statement that I'm using to export the table to a excel file...
DoCmd.TransferSpreadsheet acExport,8, "Table2", "C:\Table2", True
This is after making this query...
DoCmd.RunSQL "SELECT [Fields] INTO Table2 FROM Table1;"
All I want is to export it to csv, or using the excel file that I already have...thanks for your time
|
|
|
|
|
Using SQL Server 2000, does the SQL Reporting tools allow you to output in RTF format?
|
|
|
|
|
I'm looking for a function in SQL Server to find the first occurence of a given character within a given string and return the position as int.
Really don't feel like re-inventing the wheel today, help would be appreciated!
you can't forget something you never knew...
|
|
|
|
|
You can use Patindex or Charindex in sqlserver
|
|
|
|
|
Hello Friends,
I have a field of description which holds data like this
F001 - xyz
H001 - abcd
FG0003 - lmn
...
...
...
...
I want to remove that par of data before this hyphen sign including hyphen "-" and want to represent data after hyphen sign.
Help will be appriciated.
Naveed Kamboh
|
|
|
|
|
query
use pubs
select substring (phone,charindex('-',phone)+1, Len(phone)-charindex('-',phone)+1)
from
authors
|
|
|
|
|
Thank you verey much its 100%.
Naveed Kamboh
|
|
|
|
|
I have to create stored procs for insertions and updates for a new project with lots of parameter (around 20 to 35), i wanted to know the advantange and disadvantages of the two methods below
Method 1. --- the usual way
Create Procedure SaveNewSettings
@param1 varchar(20),@param2 varchar(20),@param3 varchar(20),
@param4 varchar(20),....etc
---- insert into table values (
Method 2. -- the xml way
Create Procedure SaveNewSettings
@XMLParam As varchar(200)
AS
EXEC sp_xml_preparedocument @hDoc OUTPUT , @XMLParam
--- Do the openxml stuff and update
Which method of using parameters is better wrt performance and usability.
Please suggest.
I am using SQL Server 2000 & ASP.Net (VB.net as code behind) for my project.
|
|
|
|
|
I would like to know if I can install both MSDE and MS SQL 2005 Express together such that I have an MSDE Database and also a MS SQL 2005 database to use.
|
|
|
|
|
Kuira,
You can run both side by side.
Paul
|
|
|
|
|
Can you SQL gurus help me craft an UPDATE statement that will insert a modified clone of a set of rows into a table? For example, my table FIELDS_IN_VIEW has 2 columns VIEW and FIELD and contains several thousand rows:
Row VIEW FIELD
--- ---- -----
1 23 100
2 23 701
4 23 459
...
6500 99 804
I'd like to clone all the rows that apply to view 23 for a new view (eg: 24 ). View 24 is guaranteed to not already exist in the table.
Thanks,
/ravi
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
|
|
|
|
|
try it
insert into FIELDS_IN_VIEW select 24, field from FIELDS_IN_VIEW where view = 23
|
|
|
|
|