select power(1.005,4) [Power]
gives 1.020
select 1.005*1.005*1.005*1.005 [Manual]
gives 1.020150500625
i need the latter result but don't want to do manually. 4th Power in this case but will be variable.
please advise. thanks
Based on your syntax, I assume you are using SQL Server. As explained in the documentation for power():
Returns the same type as submitted in float_expression. For example,
if a decimal(2,0) is submitted as float_expression, the result
returned is decimal(2,0).
SQL Server interpets numeric inputs as decimals, not floats. So, if you want the full value, convert the value before calling the function:
select power(convert(float, 1.005), 4) as [Power]
Here is a Rextester comparing the different approaches.
Related
I am trying to convert the Oracle script to SQL Server script. But it seems my MIN() function is not working as expected as it is being executed in Oracle.
For SQL Server :
SELECT MIN(v)
FROM (VALUES ('20013E17587A1_2'), ('20013E17587_2')) AS value(v);
Result: 20013E17587_2
However,
For ORACLE :
SELECT MIN(t.value)
FROM tab t;
Result: 20013E17587A1_2
I am getting this as a result. Can somebody explain why is this difference and what can be done to have same result?
Different sort rules. You're asking the database whether it considers _ to come before or after A. By default, Oracle uses a binary sort (and the code point 65 belonging to A is less than code point 95 belonging to _) while SQL Server uses the default collation of the database, which will be a linguistic ordering where _ is considered to precede any letter. If you want SQL Server to exhibit identical behavior, use something like
SELECT MIN(v COLLATE Latin1_General_BIN2)
FROM (VALUES ('20013E17587A1_2'), ('20013E17587_2')) AS value(v);
Actual correct placement of the COLLATE depends on your "real" query, which I presume this isn't -- you may want to change the collation of the column itself in the CREATE TABLE, for example.
Because Oracle looks for the ASCII value for each character during the comparison of each respective character ordered within the string while performing alphanumeric sorting. This is called binary sort which's default for Oracle DB.
ASCII('A') equals 65, and ASCII('_') equals 95. If the string was 20013E17587.2 instead of 20013E17587_2, then you'd get 20013E17587.2 as result, since ASCII('.') equals 46 which is less than 65.
I am trying to extract only the numeric values from a column that contains cells that are exclusively numbers, and cells that are exclusively letter values, so that I can multiply the column with another that contains only numeric values. I have tried
SELECT trim(INTENT_VOLUME)
from A
WHERE ISNUMERIC(INTENTVOLUME)
and also
SELECT trim(INTENT_VOLUME)
from A
WHERE ISNUMERIC(INTENTVOLUME) = 1
and neither works. I get the error Function ISNUMERIC(VARCHAR) does not exist. Can someone advise? Thank you!
It highly depends on DBMS.
in SqlServer you have a limited built-in features to do it, so the next query may not work with all variants of your data:
select CAST(INTENT_VOLUME AS DECIMAL(10, 4))
from A
where INTENT_VOLUME LIKE '%[0-9.-]%'
and INTENT_VOLUME NOT LIKE '%[^0-9.-]%';
In Oracle you can use regex in a normal way:
select to_number(INTENT_VOLUME)
from A
where REGEXP_LIKE(INTENT_VOLUME,'^[-+]?[0-9]+(\.[0-9]+)?$');
MySQL DBMS has also built-in regex
Try this, which tests if that text value can be cast as numeric...
select intent_volume
from a
where (intent_volume ~ '^([0-9]+[.]?[0-9]*|[.][0-9]+)$') = 't'
An old dbf have been succesfully ODBC-connected to Excel (2010). The following SQL query (through Microsoft Query) works as expected giving a single result.
Microsoft Query (Excel 2010) code
SELECT c.NDELNUM AS deliverynote, SUM(c.NQTY*a.CPREDEF2) AS Total
FROM DelCusL AS c, Article AS a
WHERE c.CREF = a.CREF AND ((c.NDELNUM=?))
GROUP BY c.NDELNUM
I am interested in getting also single value, but the following retuns NULL:
SELECT c.NDELNUM AS deliverynote, SUM(c.NQTY*(a.CPREDEF2+c.CPROP2)) AS Total
FROM DelCusL AS c, Article AS a
WHERE c.CREF = a.CREF AND ((c.NDELNUM=?))
GROUP BY c.NDELNUM
I guess this does not work because empty values are encountered in either a.CPREDEF2 or c.CPROP2. When an empty value is encounterd, I'd like it to be treated as 0. I have tried casting value functions available in Microsoft Query to not much avail.
Any idea on converting EMPTY values to 0s so that the operation is successful?
NQTY is a number and always non-empty. CPREDEF2 is treated as VARCHAR and can be EMPTY: when EMPTY it seems to be treated as NULL from other tests; CPROP2 is an alternative value to CPREDEF2 and can also be EMPTY, if filled it can be understood as number(like CPREDEF2). Both CPREDEF and CPROP2 are treated as VARCHAR rather than numerical values but when on their own are correctly multiplied and aggregated as numbers. (It fails when I try to add them together before the aggregate function).
Try to CAST before you add the values:
... * (CAST(a.CPREDEF2 AS DECIMAL(10,5))+CAST(c.CPROP2 AS DECIMAL(10,5))) ...
I am trying to connect a Filemaker DB to Firebird SQL DB in both ways import to FM and export back to Firebird DB.
So far it works using the MBS Plug-in but FM 13 Pro canot handle NULL.
That means that for example Timestamp fields that are empty (NULL) produce a "0" value.
Thats means in Time something like 01.01.1889 00:00:00.
So my idea was to simply ignore fields containing NULL.
But here my poor knowlege stops.
First I thought I can do this with WHERE, but this is ignoring whole records sets:
SELECT * FROM TABLE WHERE FIELD IS NOT NULL
Also I tried to filter it later on like this:
If (IsEmpty (MBS("SQL.GetFieldAsDateTime"; $command; "FIELD") ) = 0 ; MBS("SQL.GetFieldAsDateTime"; $command; "FIELD"))
With no result either.
This is a direct answer to halfbit's suggestion, which is correct but not for this SQL dialect. In a query to provide a replacement value when a field is NULL you need to use COALESCE(x,y). Where if X is null, Y will be used, and if Y is null then the field is NULL. Thats why it is common for me to use it like COALESCE(table.field,'') such that a constant is always outputted if table.field happens to be NULL.
select COALESCE(null,'Hello') as stackoverflow from rdb$database
You can use COALESCE() for more than two arguments, I just used two for conciseness.
I dont know the special SQL dialect, but
SELECT field1, field2, value(field, 0), ...FROM TABLE
should help you:
value gives the first argument, ie, your field if it is NOT NULL or the second argument if it is.
We have legacy table where one of the columns part of composite key was manually filled with values:
code
------
'001'
'002'
'099'
etc.
Now, we have feature request in which we must know MAX(code) in order to give user next possible value, in example case form above next value is '100'.
We tried to experiment with this but we still can't find any reasonable explanation how DB2 engine calculates that
MAX('001', '099', '576') is '576'
MAX('099', '99', 'www') is '99' and so on.
Any help or suggestion would be much appreciated!
You already have the answer to getting the maximum numeric value, but to answer the other part with regard to 'www','099','99'.
The AS/400 uses EBCDIC to store values, this is different to ASCII in several ways, the most important for your purposes is that Alpha characters come before numbers, which is the opposite of Ascii.
So on your Max() your 3 strings will be sorted and the highest EBCDIC value used so
'www'
'099'
'99 '
As you can see your '99' string is really '99 ' so it is higher that the one with the leading zero.
Cast it to int before applying max()
For the numeric maximum -- filter out the non-numeric values and cast to a numeric for aggregation:
SELECT MAX(INT(FLD1))
WHERE FLD1 <> ' '
AND TRANSLATE(FLD1, '0123456789', '0123456789') = FLD1
SQL Reference: TRANSLATE
And the reasonable explanation:
SQL Reference: MAX
This max working well in your type definition, when you want do max on integer values then convert values to integer before calling MAX, but i see you mixing max with string 'www' how you imagine this works?
Filter integer only values, cast it to int and call max. This is not good designed solution but looking at your problem i think is enough.
Sharing the solution for postgresql
which worked for me.
Suppose here temporary_id is of type character in database. Then above query will directly convert char type to int type when it gives response.
SELECT MAX(CAST (temporary_id AS Integer)) FROM temporary
WHERE temporary_id IS NOT NULL
As per my requirement I've applied MAX() aggregate function. One can remove that also and it will work the same way.