I have a char(1) column but I want select this column like this:
SELECT CAST(CASE WHEN [ENABLED] = 'Y' THEN 'Yes' ELSE 'No' END AS EnabledTitle)
FROM [ICS_USERS]
I am getting this error:
Conversion failed when converting the varchar value 'Yes' to data type bit.
Is there any way to show varchar values in char columns?
Thanks
You don't need to call CAST()
SELECT CASE WHEN [ENABLED] = 'Y' THEN 'Yes' ELSE 'No' END AS EnabledTitle
FROM [ICS_USERS]
CAST converts a value. Your conversion takes place on the CASE statement.
Related
I have a case statement to rectify one business logic in snowflake:
INSERT INTO DB.table_b
SELECT
CASE
WHEN UPPER(emp) <> LOWER(emp) THEN NULL
WHEN emp IS NULL THEN nullif(emp, 'NULL')
ELSE emp
END AS emp_no
FROM
DB.table_a;
The 'table_a' content as below :
emp
-------
ABCD
NULL
''
23
It contains character string, null, empty and numbers. So, the requirement is to take only numbers and empty values from the case statement since the column emp_no in 'table_b' is numeric type. In source table if the column value is string then we have to insert NULL value. But as the 'table_b' column is of type 'numeric' the null value is not getting inserted and getting following error
Numeric value '' is not recognized
Using TRY_TO_NUMBER:
A special version of TO_DECIMAL , TO_NUMBER , TO_NUMERIC that performs the same operation (i.e. converts an input expression to a fixed-point number), but with error-handling support (i.e. if the conversion cannot be performed, it returns a NULL value instead of raising an error).
INSERT INTO DB.table_b
SELECT TRY_TO_NUMBER(emp) AS emp
FROM DB.table_a;
you can not use IS_INTEGER but for VARCHAR(16777216) it isn't supported
So a regular expression would be better
INSERT INTO DB.table_b
SELECT
CASE
WHEN regexp_like(emp,'^[0-9]+$') THEN emp
ELSE NULL
END AS emp_no
FROM
DB.table_a;
As Lukasz mentions you should use the TRY_TO_x functions (TRY_TO_NUMERIC, TRY_TO_DOUBLE) as these safely handle parsing the types, and return NULL if the parse fails. The extra note I will add is that both NUMBER/NUMERICs and DOUBLEs will parse 0.1234 but get different results, which you didn't mention as caring about, but I think is worth noting, so I am adding an extra answer to point the difference out.
The CTE is just to get the values into the SQL:
WITH data(emp) as (
select * from values
('ABCD'),
(NULL),
(''),
('0.123'),
('23')
)
SELECT emp
,try_to_numeric(emp) as emp_as_num
,try_to_double(emp) as emp_as_float
FROM data
EMP
EMP_AS_NUM
EMP_AS_FLOAT
'ABCD'
null
null
null
null
null
''
null
null
'0.123'
0
0.123
'23'
23
23
You can test for amp being string and set the string to NULL. Only numeric values will go into the second case statement.
SELECT
CASE
WHEN IS_VARCHAR(emp) then NULL else
case WHEN UPPER(emp) <> LOWER(emp) THEN NULL ELSE emp end
end AS emp_no
when the numeric field is Null how to show a string value? Tried the syntax below but getting an error
'Error converting data type varchar to numeric':
Syntax:
select
case
when bill_area_id IS null then 'Unknown'
else bill_area_id end from Table_name
Use coalesce to return first non-null value :
select coalesce(bill_area_id, 'Unknown') from Table_name
Note that compatible data types is required. I.e. you may have to do :
select coalesce(cast(bill_area_id as varchar(15)), 'Unknown') from Table_name
If you are using sql server, you can also use this :
select isnull(cast(bill_area_id as varchar(15)), 'Unknown') from Table_name
The error is because you are mixing varchar with numeric output in the same column. For SQL Server for example:
select
case
when bill_area_id IS null then 'Unknown'
else CAST(bill_area_id as varchar) end from Table_name
Ensures that the output is uniformly varchar since I am casting bill_area_id as varchar, or selecting 'Unknown' (also a varchar).
There are other answers here using COALESCE/isnull that are just as valid, since those functions provide an alternative to NULL values.
I have a table with a column of varchar type. The column contains 568710 records of numeric (I mean the ISNUMERIC() returns 1) and 91 records of null values (i.e., the ISNUMERIC() returns 0). Now, I need to convert the column to FLOAT without losing the null records or replacing them with any other value. Is it possible in SQL?
When I use CONVERT(FLOAT, [Value]) conversion, I get the following error:
Msg 8114, Level 16, State 5, Line 48
Error converting data type varchar to float.
I read that the null can be converted to any type. So it should be possible.
You can use this
SELECT CONVERT(float, CASE WHEN ISNUMERIC(columnName) = 1 THEN columnName ELSE NULL END) FROM TableABC
Try :::
ALTER TABLE tablename
ADD NewFloatColumn FLOAT
UPDATE TableName
SET NewFloatColumn =
CASE WHEN ISNUMERIC(VarcharColumn) =1 THEN CAST (VarcharColumn AS float)
WHEN UPPER(VarcharColumn) = 'NULL' THEN null
END
Select (CASE WHEN ISNUMERIC(c) = 1
THEN (CASE WHEN c LIKE '%.%' THEN c ELSE c + '.00' END)
ELSE '0.00'
END)from table_name
I just spent some time scratching my head at the same problem. I was able to resolve this by converting first to int, then to float. Testing shows no data loss as my float column did not contain decimal, it simply had to match the dtype of another column for a later UNION operation.
This was completed on SQL Server 2016.
ALTER TABLE table1
ALTER COLUMN column1 int;
ALTER TABLE table1
ALTER COLUMN column1 float;
I have the below Case Statment and I'm inserting this into a new table. The column under the new table is Varchar but I need it as an INT. I have changed the data type using the Alter statment but I frequenctly delete and create the same table. Is there a way to have the new table create the data type of INT instead of varchar for the below syntax?
CASE WHEN F.END_DATE IS NOT NULL OR F.REASON IS NOT NULL THEN '0' ELSE '1' END Enrolled'
Get rid of the single quotes around 0 and 1:
CASE WHEN F.END_DATE IS NOT NULL OR F.REASON IS NOT NULL THEN 0 ELSE 1 END Enrolled'
Try Following:
CASE WHEN coalesce(F.END_DATE,F.REASON ,0)=0 THEN 0 ELSE 1 END Enrolled
the query below throws the error mentioned in the title :
SELECT
Set_To_Value_ID,
CASE
WHEN Set_To_Variable_ID IS NOT NULL THEN Set_To_Variable_ID
WHEN Changed_In_SP IS NOT NULL THEN Changed_In_SP
WHEN Comment_Input IS NOT NULL THEN ( Select Comment FROM Process_Instance WHERE Process_Instance_ID = #Process_Instance_ID )
END AS Manual_Value
FROM Variables
WHERE Some_Irrelevant_Value = #Some_Irrelevant_Value
I know the error comes from the Comment_Input case because that's the value that should be returned from there.
When using CASE expressions, all results must have the same data type.
Returns the highest precedence type from the set of types in
result_expressions and the optional else_result_expression.
http://msdn.microsoft.com/en-us/library/ms181765.aspx
I recommend converting your int data types into nvarchar so that SQL doesn't attempt to convert your nvarchar to int:
CASE
WHEN Set_To_Variable_ID IS NOT NULL THEN CAST(Set_To_Variable_ID as nvarchar(20))
WHEN Changed_In_SP IS NOT NULL THEN THEN CAST(Changed_In_SP as nvarchar(20))
WHEN Comment_Input IS NOT NULL THEN ( Select Comment FROM Process_Instance WHERE Process_Instance_ID = #Process_Instance_ID )
END AS Manual_Value
check for the dependencies on the table like trigger, views or relations with other tables.. they might have the int datatype for this particular column