Question regarding SQL arithmetic functions - sql-server

Can you tell me what am I doing wrong here? Trying to use subtraction here in SQL Server 2005 SELECT statement. There's some type of syntax error here.
isnull(dbo.udf_GetInventory(ga.sku,#date4),0) * costprice -
isnull(dbo.udf_GetInventory(ga.sku,#date3),0) * costprice as date2_diff

There is no syntax-error here if:
You put the statement in a SELECT ... from ga.
ga is a table that has columns sku and costprice of some numeric data type.
You have a scalar-valued function called udf_getinventory that takes two parameters.
The data type of the first parameter to udf_getinventory matches the data type of ga.sku.
The data type of the second parameter matches the data type of #date3.
udf_getinventory returns some numeric data type.
#Date3 is declared before the SELECT statement.
#Date4 is declared before the SELECT statement.

Not sure about a syntax error, but assuming udf_GetInventory is non-deterministic you're going to get 0 every time...

Related

Invalid argument types for function SQL compilation error when passing parameter values to Snowflake Function

I have create a function in Snowflake with two 'date'arguments:
CREATE OR REPLACE FUNCTION "fn_CreateHourLabels"(start_date date,end_date date)
RETURNS TABLE ...
When I attempt to use the function:
SELECT * FROM TABLE("fn_CreateHourLabels"('2021-03-01','2021-03-11'))
I get the following error:
SQL compilation error: error line 1 at position 20 Invalid argument types for function '"fn_CreateHourLabels"': (VARCHAR(10), VARCHAR(10))
I am assuming there is something simple I have missed. Is it because the I have declared the arguments as 'date' data types and the select statement is classifying my date values as strings?
Thanks in advance
Instead of
SELECT *
FROM TABLE("fn_CreateHourLabels"('2021-03-01','2021-03-11'))
Call it like this, making sure that Snowflake recognizes those strings as dates:
SELECT *
FROM TABLE("fn_CreateHourLabels"('2021-03-01'::date,'2021-03-11'::date))

SQL CAST with ISNULL results in conversionfailure

I've got a nullable column of numbers, which is unfortunately declared as a varchar in the database, and so I want to convert the NULL to 0. Doing this accomplishes that.
select ISNULL(col, '0') from table;
So now there are no null possibilities, and then I want to convert the column to an actual int value, so I tried to wrap that.
select CAST(ISNULL(col, '0') AS INT) from table
When that runs I get a conversion error, as shown below. I don't understand why, or how to get around this:
Conversion failed when converting the varchar value 'NULL' to data type int.
Try like this: Take your ISNULL statement outside
SELECT ISNULL(TRY_CAST(col AS INT),0) AS [col]
FROM SAMPLE_TAB;
If this does not help, then please comment your SQL version.

attribute key was not found - MS SQL Server

I got the following message on MS SQL Server (I'm translating from German):
"Table 'VF_Fact', column ORGUNIT_CD, Value: 1185. The attribute is
ORGUNIT_CD. Row was dropped because attribute key was not found.
Attribute: ORGUNIT_CD in the dimension 'Organization' from database
'Dashboard', Cube 'Box Cube'..."
I checked the fact table 'VF_Fact' and the column ORGUNIT_CD - there I was able to found the value '1185'. The column ORGUNIT_CD is defined as follows in the view:
CAST( COALESCE( emp.ORGUNIT_CD, 99999999 ) AS char(8)) AS ORGUNIT_CD,
In addition the view retrieves the column from L_Employee_SAP TABLE, where ORGUNIT_CD is defined as follows:
[ORGUNIT_CD] [char](8) NOT NULL,
AND the value I find here is not '1185' but '00001185'.
The Fact table 'VF_Fact' is connected with the table L_ORG in which the column ORGUNIT_CD is defined as follows:
[ORGUNIT_CD] [char](8) NOT NULL,
This table hast the following value in the ORGUNIT_CD column: '00001185'.
Can anyone please explain, why am i getting this error, and how to remove it?
From this answer:
COALESCE:
Return Types
Returns the data type of expression with the highest data type precedence. If all expressions are nonnullable, the result is typed
as nonnullable.
(Emphasis added). int had a higher precedence than varchar, so
the return type of your COALESCE must be of type int. And obviously,
your varchar value cannot be so converted.
As another answer noted, ISNULL() behaves differently: rather than return the data type with the highest precedence, it returns the data type of the first value (thus, #Aleem's answer would solve your issue). A more detailed explanation can be found here under the section "Data Type of Expression."
In your specific case, I'd actually recommend that you encase the alternative string in single quotes, thus tipping SQL Server off to the fact that you intend this to be a character field. This means your expression would be one of the following:
CAST (ISNULL( emp.ORGUNIT_CD, '99999999' ) as char(8))
CAST (COALESCE( emp.ORGUNIT_CD, '99999999' ) AS char(8))
The advantage of using quotes in this situation? If you (or another developer) comes back to this down the line and tries to change it to COALESCE() or do any other type of modification, it's still going to work without breaking anything, because you told SQL Server what data type you want to use in the string itself. Depending on what else you're trying to do, you might even be able to remove the CAST() statement entirely.
COALESCE( emp.ORGUNIT_CD, '99999999' )
COALESCE function is dropping the leading zeroes. If you are checking for nulls you can do this and it will keep the zeroes.
CAST (ISNULL( emp.ORGUNIT_CD, 99999999 ) as char(8))

how to check for a null value in t-sql

I want to check for a value in a table to see if it's null.
If so, I just want output to be '0.000'; otherwise, I'd like the actual value which must be converted to a varchar field.
The definition of the column in the database is decimal(10,3).
When I run my below query (pertinent part of the query), I get an error message:
Error converting data type varchar to numeric.
Here is my t-sql syntax that is getting the error. How do I correct it?
isnull(nullif(Ryder_Price,'0.000'),convert(varchar(max),Ryder_Price))
EDIT
Here is the full SELECT statement:
Select '4'+','
+','+Case when Country_Code= 1 then 'USA' else 'CAN' End
+','+
+','+isnull(nullif(Ryder_Price,'0.000'),convert(varchar(max),Ryder_Price))
+','+isnull(nullif(Ryder_Price,'0.000'),convert(varchar(max),Ryder_Price))
+','+isnull(nullif(Ryder_Price,'0.000'),convert(varchar(max),Ryder_Price))
+','+
+','+'01/01/1900'
+','+'01/01/2099'PartMaster_String
,4 ord,part_no
, RIGHT('000'+convert(varchar(3),ATA_SYSCode),3)+'-'+RIGHT('000'+convert(varchar(3),ATA_Asmcode),3)+'-'+RIGHT('000'+convert(varchar(3),ATA_Partcode),3) taskstring
From tbl_CPM_PARTS_MASTER_PTC
The reason for the error message is that it is possible for the line you posted to simply have Ryder_Price as a value, which is a decimal type. And then you are trying to concatenate it to a string.
To acheive your stated goal:
I want to check for a value in a table to see if it's null.
If so, I just want output to be '0.000'; otherwise, I'd like the
actual value which must be converted to a varchar field.
Try this:
convert(varchar(max),isnull(Ryder_Price,'0.000'))

Encountering SQL Error not having to do with my query... what can I do to get to my data?

I'm trying to run this query:
select * from activity where id = 9348927
And I receive this error:
Conversion failed when converting the varchar value '04 30' to data type int.
Note that I still receive this error when I change * to id, which doesn't make any sense to me. I don't receive the error when I run this:
select top 32 * from activity where id = 9348927
I'm pretty sure this means that some of the data on the 33rd row is in a format that SQL Server doesn't like. Is there any way I can get it to ignore the error and display my data anyway?
EDIT: id is varchar(10)
This usually means that the id column is not a number datatype, but varchar.
9348927 is a number (int) and datatype precedence means the the string value '04 30' is converted to int. Standard SQL behaviour.
Try this:
select * from activity where id = '9348927'
Incidently, the implicit conversion means any index on id will not be used. Compare query plans.
The best way to "work around" this is to fix your data and column type. Otherwise, do the comparison as string which is what id most likely is.

Resources