Removing numbers that are negative from SQL query - sql

How do you remove negative numbers from SQL query result.
Example:
Select acquisitionprice-salesprice
From Han.trans
I'm trying to find out the Profit by subtracting two columns from a table and would like to have the negative numbers removed from the result

WHERE salesprice > acquisitionprice
probably is treated as the same as giorgos-betsos commented
WHERE acquisitionprice-salesprice >= 0
but reads a little simpler to me.

Related

Searching on Last Decimal Place

In the SQL (SQL Server 2019) database I am working, the vendor has introduced a rounding bug and I'm trying to track down all instances of this issue.
On a decimal field with 10 decimal places eg. 42.5500000001, I want to search where only the last decimal place is not equal to zero. I'm struggling with the code on this one.
Any assistance is appreciated.
Select the last value in your column from the RIGHT and check to see if it’s not equal to zero.
select * from test_table where right(test_column, 1) <> 0
db<>fiddle here.

Percentage in SQL Server

I want to find the percentage between two columns and I tried this :
select
(count(id)* 100.0 /(select count(id) FROM db.table where log = '40')) as percentage
from db.table;
And the result is this:
418659.800405426477
Why the output number is like that?
Also is there any way to make it look better and help end users to understand the percentage?
The output is like that because of the rules that SQL Server uses when doing arithmetic on numbers -- the precision and scale are rather details (note that 1.0 is a numeric constant).
The explanation for this is in the documentation. Note that even I don't bother trying to understand the rules for division.
I just want to point out that you can simplify the query:
select avg(case when log = '40' then 100.0 else 0 end) as percentage
from db.table;
This only scans the table once, so it should be faster. But it should produce a similar result.
If you want a given scale/precision then use round(), floor(), ceil(), str() or convert to a numeric.

SQL Server : aggregate function doesn't work?

I have imported a price list from a csv to my SQL Server database. That has worked fine. But now some weird stuff. Table is named PRICE which includes a column (and some more) Endprice and a total of 761 rows. All datatypes are varchar(50).
SELECT MAX(Endprice)
FROM PRICE
When I want this simple SQL statement to show the highest price in the column, I get a wrong result. I don't know why.
I get 98,39 as a result, but that's definitively wrong, it must be 100,73.
Here you can see a part of the data:
And now the wrong MAX() result:
BUT when I'm using the MIN function I get the highest one!? The min is somewhere at ~50 (not shown in the screenshot part).
`
The resultset of SELECT Endprice FROM PRICE is correct. I am at my wit's end.
This is because your column is a varchar, so it is determining the min or max based on characters. The column should be a decimal or money type, so it sorts by the value of your number (instead of an alphabetic sort like you are getting now).
Alphabetic sort: 9 is more than 1, thus 98.39 is the max.
The reason is because price is a varchar().
Here are two solutions:
order by len(price), price
This works assuming that all the price values have the same structure.
Or:
order by cast(price as float)
If you could have non-numeric values (always a danger when storing numbers in the wrong data type):
order by (case when isnumeric(price) = 1 then cast(price as float) end)
Or better yet:
alter table alter column price money
Then you don't have to worry about having the wrong type for the column.
Your problem is Endprice columns is varchar(50), therefore it is comparing strings not numbers, which means that a 9>1 no matter what cames next of the first digit. You have to convert it to a number before the max!
Also you really should consider in doing what #a_horse_with_no_name suggested change your column into a number like column type.
This is a example on how you solve your actual problem
select max(cast(endprice as money)) from sample
See it here: http://sqlfiddle.com/#!3/767f6/1
Note that I used . as a decimal separator it will depend on your database language setup.

SQL Server - Select between two number ranges in a varchar field with text and numbers

I have a table with ranges of numbers from 0-2000000. I can use a between statement to get the records of things if I do a BETWEEN 50 AND 100. That works.
But the problem I'm running into is since this column is a varchar if there are any records with a something in the string as "1FD32", I can't select between it. SQL server can't convert the "BETWEEN 50 AND 100" because it is looking for an int. How can i get all the records between these ranges, and ignore any with letters in them?
If you want to do a numeric comparison, the only way to guarantee no error is to use the CASE statement. This is the only SQL statement that guarantees the order of valuation (when not used with aggregation functions).
So, here is code that will work:
select *
from t
where (case when isnumeric(col) = 1 then cast(col as bigint) end) between 20 and 100
Note that if your numbers contain decimal points, then you will want to use DECIMAL or FLOAT instead.

using SUM and presenting result as absolute (ABS)

I have both positive and negative numbers (money) in a column and need to:
SUM the total ie. SUM(myColumn) based on if the numbers are +/-
Present the result as an absolute ie. even though the result is -1234 it should be presented as 1234
SQL is not my trade as you probably notice but we've solved most other issues but this one so any help is appriciated. Keep in mind my skill level is very low
You will have to use a combination of the sum and abs aggregate functions in SQL. Since you want the absolute value of the sum, the sum function will need to be called inside the call to abs:
select abs(sum(columnName)) from table
That should work for both SQL Server, MySQL, and Oracle.
Try one (or more) of these
SELECT SUM(moneyColumn) FROM MyTable
SELECT SUM(ABS(moneyColumn) FROM MyTable
SELECT ABS(SUM(moneyColumn) FROM MyTable