Case, Is Null and a parameter - sql

I have a query that i'd like to have display "0" if the "Tip" is "NULL" otherwise just display the tip. This is my query:
SELECT t1.sName ,
t1.sLocationDesc ,
t1.sOtherNumber ,
t1.RegHours ,
t1.OTHours ,
CASE
WHEN t2.EmployeeHours / t3.DepartmentHrs * #Tip IS NULL THEN 0
ELSE t2.EmployeeHours / t3.DepartmentHrs * #Tip
END AS Tip
I receive an error:
Incoming Tabular data stream (TDS) remote procedure call (RPC)
protocal stream is incorrect. Parameter 3 ("#Tip") User-defined type
has an invalid user type specified..
Any help here?

Why do you want to use case
just try this
SELECT t1.sName,
t1.sLocationDesc,
t1.sOtherNumber,
t1.RegHours,
t1.OTHours,
ISNULL(t2.EmployeeHours / t3.DepartmentHrs * #Tip,0) AS 'Tip'
BTW I feel the error is not due to your case statement

Try this:
SELECT t1.sName, t1.sLocationDesc, t1.sOtherNumber, t1.RegHours, t1.OTHours,
CASE WHEN (#Tip IS NULL OR #Tip=0) THEN 0 ELSE
t2.EmployeeHours/t3.DepartmentHrs * #Tip END AS Tip

Related

DB2 Error executing sql statement: [SQL0104] Token ) was not valid

I am getting the following error from DB2, using custom driver: com.ibm.as400.access.AS400JDBCDriver
Error executing sql statement: [SQL0104] Token ) was not valid. Valid tokens: , FROM INTO.; Caused by: [SQL0104] Token ) was not valid. Valid tokens: , FROM INTO.
Narrowed it down to this bit which works in SQL server, we are a bit stumped as syntax appears fine.
SELECT CASE
WHEN Abs(ho.ohmisc) > 0
THEN
(
SELECT LEFT(s, Translate('%[^0-9]%', s) - 1)
FROM (
SELECT Substr(
ohunpo,
Translate('%[0-9]%', ohunpo),
Len(ohunpo)
) AS s
) t
)
ELSE odrord
END ODRORD
FROM qs36f.hoehead HO
QS36F.hoehead HO
You are missing a FROM
SELECT CASE
WHEN Abs(ho.ohmisc) > 0
THEN
(
SELECT LEFT(s, Translate('%[^0-9]%', s) - 1)
FROM (
SELECT Substr(ohunpo,
Translate('%[0-9]%', ohunpo),
Len(ohunpo)
) AS s
-- **You need a FROM here**
) t
)
ELSE odrord
END ODRORD
FROM qs36f.hoehead HO
QS36F.hoehead HO
But that's not your only issue...
This is not valid either, are you trying to join the table to itself?
FROM qs36f.hoehead HO
QS36F.hoehead HO
Your embedded selects likely aren't right either....but without knowing your table structure and seeing some example source data and what you're wanting as output, I can't say for sure.

SQL to get whole words till end from the keyword

Consider I have text from the field (notes) as below:
Please check http://example.com
I want a SQL query which fetches the link part only. That is to find keyword http and print till the last.
Output:
http://example.com
Also if the text field doesnt have any link, can we print NA?
CASE WHEN sys like '%Clo%'
THEN RIGHT( i.notes,LEN(i.notes) - CHARINDEX('http',i.notes,1)+1)
ELSE "No Link Available" END AS Cl_Link
For SQL Server you can consider this below logic-
DECLARE #T VARCHAR(MAX) = 'Please check http://example.com'
SELECT RIGHT(#T,LEN(#T) - CHARINDEX('http:',#T,1)+1)
For MySQL-
SET #T = 'Please check http://example.com';
SELECT RIGHT(#T,LENGTH(#T) - POSITION("http:" IN #T)+1)
In case of select query using table, queries will be-
-- SQL Server
SELECT RIGHT(column_name,LEN(column_name) - CHARINDEX('http:',column_name,1)+1) FROM your_table_name
-- MySQL
SELECT RIGHT(column_name,LENGTH(column_name) - POSITION("http:" IN column_name)+1) FROM your_table_name
To apply 'NA' when no link available, please use the below logic-
DECLARE #T VARCHAR(MAX) = 'Please check http://example.com'
SELECT
CASE
WHEN CHARINDEX('http:',#T,1) >= 1 THEN RIGHT(#T,LEN(#T) - CHARINDEX('http:',#T,1)+1)
ELSE 'NA'
END
Below is the query for your reference, executed on mysql:
SELECT substr(columnname,locate('http',columnname)) as link
FROM `tablename` where column=value
For MySQL try this:
select REGEXP_SUBSTR(text_column, 'http\:\/\/([\\w\\.\\S]+)') from mytest

Case select another column AS400 query

I have simple query which is not working:
SELECT BATCH_0002.CREATOR, BATCH_0002.GLEXR,
case when BATCH_0002.GLIVD >'0' THEN BATCH_0002.GLIVD ELSE BATCH_0002.date end
as tarih FROM BATCH_0002
Error is:
ERROR: [IBM][System i Access ODBC Driver][DB2 for i5/OS]SQL0581 - The
results in a CASE expression are not compatible. Error Code:
-581
but when I change column name to something different than GLIVD, it is working.
SELECT BATCH_0002.CREATOR, BATCH_0002.GLEXR,
case when BATCH_0002.GLIVD >'0' THEN BATCH_0002.GLEXR ELSE BATCH_0002.date end
as tarih FROM BATCH_0002
Is there something wrong with my case statement?
In Case statement result datatype should be same. Here Using int and date as result in case that's why error showing for incompatible data
you should cast one datatype to another
SELECT BATCH_0002.CREATOR, BATCH_0002.GLEXR, case when BATCH_0002.GLIVD >'0' THEN cast(BATCH_0002.GLIVD as char) ELSE cast(BATCH_0002.date as char) end as tarih FROM BATCH_0002
thank you for all answers regarding data types. worked on this information and now solved, this works very well:
SELECT BATCH_0002.CREATOR, BATCH_0002.GLEXR,
case when BATCH_0002.GLIVD >'0' THEN
DATE(CHAR(1900000 + BATCH_0002.GLIVD))
ELSE BATCH_0002.date end as tarih FROM BATCH_0002

Conversion error when using case statement - Sql Server

I have the following query:
select *,
case when prod='CPU' and quan>1000 then 1000
when prod='MOUSE' and quan>1024 then 1024
when prod='MONITOR' and quan<2 then 2 else quan end as quan
from products_test;
I'm getting the error: Conversion failed when converting the varchar value 'CPU' to data type int.
UPDATE: In light of the following comment from author of question
I changed the code code as described by you, but it's still throwing
an error. "Conversion failed when converting the varchar value
'23787.1' to data type int"
It looks like quan in varchar type rather than numeric and also it with the change in error it has been confirmed that prod is defined as integer.
select *,
case when CAST(prod AS VARCHAR(MAX))='CPU' and CAST(quan as DECIMAL(10,4))>1000 then 1000
when CAST(prod AS VARCHAR(MAX))='MOUSE' and CAST(quan as DECIMAL(10,4))>1024 then 1024
when CAST(prod AS VARCHAR(MAX))='MONITOR' and CAST(quan as DECIMAL(10,4))<2 then 2 else CAST(quan as DECIMAL(10,4)) end as quan
from products_test;
Old answer: Please try this modified query :
select *,
case when CAST(prod AS VARCHAR(MAX))='CPU' and quan>1000 then 1000
when CAST(prod AS VARCHAR(MAX))='MOUSE' and quan>1024 then 1024
when CAST(prod AS VARCHAR(MAX))='MONITOR' and quan<2 then 2 else quan end as quan
from products_test;
if this works successfully, the Prod is in fact defined as int and
you can never get the then (true case) values in CASE statements.
You probably need to compare codes or foreign keys from master table which has values of 'CPU', 'MOUSE' and 'MONITOR' with prod.

SQL SYNTAX - data type error. How to cast?

I have the following code:
CASE WHEN {internalid} = {test} THEN 1 ELSE 0 END;
But when I run it, i received the following error:
Your formula has an error in it. It could resolve to the wrong datatype, use an unknown function, or have a syntax error. Please go back, correct the formula, and re-submit.
Is there a way I can cast it so it will have same data type no matter what?
Thanks
use the cast function
-- CAST ( { expression | NULL } AS data_type [(length)] )
CASE
WHEN CAST ({internalid} as VARCHAR(20)) = CAST({test}as VARCHAR(20))
THEN 1
ELSE 0
END;