Sql ISNUMERIC() - sql

SELECT CASE WHEN ISNUMERIC('1a') =1 THEN 1 ELSE 'A' END
i'am getting this error !!
Conversion failed when converting the varchar value 'A' to data type int.

you are a victim of data type precedence.Taken from BOL:
When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence. If the conversion is not a supported implicit conversion, an error is returned. When both operand expressions have the same data type, the result of the operation has that data type
So in your case ,CASE is an expression and when two or more data types are combined in this expression,it returns data type of highest precedence..in your case it is INT..So change your query to below
SELECT CASE WHEN ISNUMERIC('1a') =1 THEN cast(1 as varchar) ELSE 'A' END

SELECT CASE WHEN ISNUMERIC('1a') =1 THEN 1 ELSE 2 END

SELECT CASE WHEN ISNUMERIC('1A') = 1 THEN '1' ELSE 'A' END
OR
SELECT CASE WHEN ISNUMERIC('1A') =1 THEN '1' ELSE SUBSTRING('1A',2,2) END

Related

type cast in Case statement

Segment
1
2
3
4
NUll
5
I want to impute 'Other' if the Segment value is null
expected output
Segment
1
2
3
4
Other
5
i have tried
select
case when segment is null then 'Other' else segment end as segment
from table;
It says invalid input syntax for type "numeric":Other
The case expression returns a single type. The problem is that segment is a number, but 'Other' is a string. The expression has to make a choice, and it chooses the numeric type (following standard SQL rules).
This is simple to fix. Just cast segment:
select (case when segment is null then 'Other' else segment::text end) as segment
from table;
It would be more natural to write this query using coalesce():
select coalesce(segment::text, 'Other') as segment
from table;
select
case when CAST(segment AS CHAR) IS NULL then 'Other' else CAST(segment AS CHAR) end as segment
from table

Error in SQL case statement when trying to create binary flag?

Here's my query where I'm testing my case structure:
SELECT TOP 1 CASE 130
WHEN '000000000000000' THEN '0'
WHEN '' THEN '0'
WHEN 'XXX' THEN '0'
WHEN 'RETIRED' THEN '0'
WHEN 'STUDENT' THEN '0'
ELSE '1'
END AS employed_flag
INTO #employedbeta
FROM CreditBureau.Experian
I'm just trying to make a new temporary table, but I'd like my case to work first. I keep getting the error:
Conversion failed when converting the varchar value 'XXX' to data type int.
In the database, the column 130 is a char, and I don't know why it thinks I want to make it a number. SQL server management studio, if it matters.
The column name is 130, I left the '1' off because I rewrote it here but I get the error regardless in my actual query.
130 is an integer literal. If that's really the column name, you'll have to escape it using double quotes. As a side note, you should probably return the same type (char) in the else branch too:
CASE "130"
WHEN '000000000000000' THEN '0'
WHEN '' THEN '0'
WHEN 'XXX' THEN '0'
WHEN 'RETIRED' THEN '0'
WHEN 'STUDENT' THEN '0'
ELSE '1'
END AS employed_flag
130 is a really bad column name. But, I would simplify the logic to:
SELECT TOP 1 (CASE WHEN [130] IN ('000000000000000', '', 'XXX', 'RETIRED', 'STUDENT')
THEN 0 ELSE 1
END) AS employed_flag
INTO #employedbeta
FROM CreditBureau.Experian;
Note that I also changed the employed_flag to a numeric value rather than a string. That makes more sense to me.

SQL Server 2005 - Converting failure

I'm selecting values from a table. Inside the table there is a column called value. I'm trying to select that value with a 'g' at the end of the value when the value is a number. However, when i try to do this i get the following error (see below).I think this is because i'm using ISNUMERIC, and since the values are either a string representation of a number or the string value 'None' (i know weird, but i have to have this way), i get a convert failure. Anyone know how to fix this issue in SQL Server 2005
Error
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value 'None' to data type int.
Query
SELECT a.student_assignment_id,
a.student_id,
a.location_id,
a.assignment_type,
(
Case
WHEN a.assignment_type = 'HW' THEN
CASE a.value
WHEN ISNUMERIC(a.value) THEN RTRIM(a.value)+'g'
ELSE a.value
END
ELSE a.value
END
) as value
,
a.start_date,
a.end_date,
a.course,
a.created_by,
a.creation_date,
a.modified_by,
a.modified_date,
You need to use CASE WHEN ISNUMERIC(a.value) = 1 instead of CASE a.value WHEN ISNUMERIC(a.value)
CASE WHEN a.assignment_type = 'HW'
THEN
CASE WHEN ISNUMERIC(a.value) = 1
THEN RTRIM(a.value)+'g'
ELSE a.value
END
ELSE a.value
END
ISNUMERIC() returns a boolean 0 (not numeric) or 1 (numeric), so your current query is attempting to compare the string a.value to an int (0 or 1)

String data right truncation DB2 error

I am receiving the error "String data right truncation" on db2 when I use this query
SELECT BILL_NUMBER, 'PAPERWORK BUT NOT COMPLETE', 'NONE', NULL, '00000',NULL,NULL,TOTAL_CHARGES, NULL FROM TLORDER WHERE
CURRENT_STATUS NOT IN ('COMPLETE','APPRVD','PAPERWISE','BILLD','EDIBILLED','CANCL') AND BILL_TO_CODE NOT LIKE CASE WHEN :INCLUDE_DED = 'No' THEN 'ROCD%' ELSE '1234kkh5656' END
AND EXISTS (SELECT 1 FROM LIST_CHECKIN_AUDIT A WHERE A.BILL_NUMBER = TLORDER.BILL_NUMBER FETCH FIRST 1 ROW ONLY)
AND SITE_ID = :SITE AND DELIVER_BY_END >= CURRENT TIMESTAMP - 3 MONTHS AND COALESCE(PICK_UP_DRIVER,'') = '' AND '00000' =:DRIVER_ID
However when I suppress this line I do not get the error.
AND BILL_TO_CODE NOT LIKE CASE WHEN :INCLUDE_DED = 'No' THEN 'ROCD%' ELSE '1234kkh5656' END
Thanks in advance!
I'd venture to guess that this happens when the value of the :INCLUDE_DED host variable exceeds 2 bytes in length. You do not supply the variable data type, so the query compiler derives it from the right side of the comparison, where the literal 'No' has the length of 2 bytes. If you then assign a value like 'Yes' to the host variable it has to be truncated.
Consider adding an explicit type information to the host variable reference, e.g.:
...WHEN CAST(:INCLUDE_DED AS VARCHAR(10)) = 'No'...
Use the data type appropriate for the range of possible values.
I would first check the datatype of the bill_to_code. You are returning '1234kkh5656' that may exceed the length of the datatype.

select case statement error in mssql

SELECT top 1
case
when VR = -99999.99
then 0
else cast((VR*1.732) as decimal(38,3))
end
FROM pseb.dbo.datasource
where FeederID=5003
order by datetime desc
The above query is working fine, but I need to return varchar value '--' instead of returning 0
if I do like that
SELECT top 1
case
when VR = -99999.99
then '--'
else cast((VR*1.732) as decimal(38,3))
end
FROM pseb.dbo.datasource
where FeederID=5003
order by datetime desc
means it returns the following error:
Msg 8114, Level 16, State 5, Line 1 Error converting data type varchar
to numeric.
please help me to solve it
The problem is that you are returning two different data types from the same column. The rule with SQL Server that numeric types take precedence over string types, i.e. in a situation like yours a string gets converted to a number, not the other way around.
So to solve this you can cast your number to a string.
One option is to do something like this:
SELECT top 1
case when VR = -99999.99 then '--'
else
cast
(
cast((VR*1.732) as decimal(38,3)
)
as varchar(50))
end
FROM pseb.dbo.datasource where FeederID=5003 order by datetime desc