Selecting values from a query that are negative in Sybase - sql

Im running the query
SELECT ART_REF FROM tablename WHERE SEMESTER_NUM=28
This query returns both positive negative a 0 results in the forms of 8, -2, 0.
how can i just return the values that are negative (-8)?
I've tired using the LIKE operator but with no success.
Cheers

Try this way:
SELECT ART_REF
FROM tablename
WHERE SEMESTER_NUM=28
AND ART_REF <0

Related

How select the min between a SQL value and a fixed number

I have to run this query in SQL that will return the min between a fixed scalar (let's say 7) and the value in the column. I tried:
SELECT from theTable min(theTable.Column , 7) AS Label
I get an error saying that I am using the wrong number of arguments in a function on this expression. I cannot copy/paste because I am using Access.
Try this
SELECT
IIF(theTable.Column < 7, theTable.Column, 7) AS Label
FROM
theTable

How to find values with certain number of decimal places using SQL?

I'm trying to figure out a way, using SQL, to query for values that go out to, say, 5 or more decimal places. In other words, I want to see only results that have 5+ decimal places (e.g. 45.324754) - the numbers before the decimal are irrelevant, however, I still need to see the full number. Is this possible? Any help if appreciated.
Assuming your DBMS supports FLOOR and your datatype conversion model supports this multiplication, you can do this:
SELECT *
FROM Table
WHERE FLOOR(Num*100000)!=Num*100000
This has the advantage of not requiring a conversion to a string datatype.
On SQL Server, you can specify:
SELECT *
FROM Table
WHERE Value <> ROUND(Value,4,1);
For an ANSI method, you can use:
SELECT *
FROM Table
WHERE Value <> CAST(Value*100000.0 AS INT) / 100000.0;
Although this method might cause an overflow if you're working with large numbers.
I imagine most DBMSs have a round function
SELECT *
FROM YourTable
WHERE YourCol <> ROUND(YourCol,4)
This worked for me in SQL Server:
SELECT *
FROM YourTable
WHERE YourValue LIKE '%._____%';
select val
from tablename
where length(substr(val,instr(val, '.')+1)) > 5
This is a way to do it in oracle using substr and instr
You can use below decode statement to identify maximum decimal present in database table
SELECT max(decode(INSTR(val,'.'), 0, 0, LENGTH(SUBSTR(val,INSTR(val,'.')+1)))) max_decimal
FROM tablename A;

SQL Force show decimal values

I'm using a Firebird database and am trying the following sql but each time it returns 0, instead of 0.61538 (etc).
SELECT (COUNT(myfield)/26) totalcount
FROM mytable
Now when I remove the /26, the totalcount returns 16 as it should. But when I add the divided by 26 back in, the result shows as 0, but it should show as the full decimal value of 0.615384... Does anyone know why it's not returning the full value? I've even tried wrapping it in a CAST((count(myfield)/26) as double) totalcount but it still returns 0.
Thank you in advance for any suggestions!!!!
Try:
SELECT COUNT(myfield/26.0) totalcount
FROM mytable
Or:
SELECT COUNT(CAST(myfield as double)/26) totalcount
FROM mytable
Not familiar with Firebird, but in other implementations you have to cast/convert either the numerator or denominator as a decimal before division, as integer division returns an integer value.

SQL Server 2008 Calculation Bug

Here is the query i am running
Select (100/18)*18
The answer i get is 90. I have tried declaring each of the numbers as decimals also, and i get the value 100.0008, but when i calculate with variables as floats the answer is correct at '100' so does anyone have any idea why SQL calculates like this?
Because it will first evelute the paranthesis
So
Select (100/18)*18
will (100/18) = 5.555555 but it assume both number as int so it will cast the result as int so it will return 5
so now 5*18 = 90
If you want the correct result do this instead Select (100*18)/18
to get the desired result you should try something like this:
Select CEILING((100/18.0)*18)
when you do this,
Select (100/18)*18
sql server consider operation as integer division and take select 100/18 as 5 instaed of 5.55555....
Try to express the numders ac decimal numbers
SELECT (100.0/18)*18
and
SELECT ROUND((100.0/18)*18,2)
...
you can try this
select abs(18*100/18)

SQL-Doesn't return a value when the columns are multiplied

I have 3 columns in my db table, I need to multiply column, itm_count & itm_price and output the total for a given id(itm_id).
SELECT sum(itm_price * itm_count) as total FROM ods_temporder WHERE itm_id='11'
I tried to do this using the above sql query, but the result was null. What seems to be the issue here?
What do this give you?
SELECT itm_price, itm_count FROM ods_temporder WHERE itm_id='11'
Is either of itm_price or itm_count NULL? Do you have rows for itm_id = 11?
Try:
SELECT SUM(COALESCE(itm_price, 0) * COALESCE(itm_count, 0)) as total
FROM ods_temporder
WHERE itm_id='11'
COALESCE is an ANSI standard for dealing with NULL values - if itm_price or itm_count is null, zero will be used in this case as the value. Otherwise, either you don't have a row with a value of 11 for the itm_id column, or you're looking for the 11 value in the wrong column.
What does itm_price and itm_count contain when itm_id= 11?
Is this SQL Server? If so you can use ISNULL to handle whether itm_price or itm_count is null