February 11, 2015 at 10:32 am
I have the following CASE statement:
SELECT CASE WHEN [TBL_whole_rock_geochem].au = 0 THEN '<' + [TBL_ActLab_codes].[Au] ELSE [TBL_whole_rock_geochem].[Au] END AS [Au det]
It errors on the highlighted code.
Error converting data type nvarchar to float
the code should return :
<0.5
<1
I have tried '<' + ISNUMERIC([TBL_ActLab_codes].[Au]) but received this error
Conversion failed when converting the varchar value '<' to data type int.
Does anyone know how I can concatenate the < with a float value?
February 11, 2015 at 10:37 am
The problem is that SQL server is trying to convert from varchar to float/int and you need to do it the other way (float to varchar).
Use this example:
SELECT CASE WHEN [TBL_whole_rock_geochem].au = 0 THEN '<' + CAST( [TBL_ActLab_codes].[Au] AS varchar(20)) ELSE CAST( [TBL_whole_rock_geochem].[Au] AS varchar(20)) END AS [Au det]
February 11, 2015 at 11:16 am
I have tried that as well you just get Error converting data type varchar to float.
February 11, 2015 at 1:57 pm
I don't believe you. You're doing something else as my code should work without problems.
SELECT CASE WHEN [TBL_whole_rock_geochem].au = 0
THEN '<' + CAST( [TBL_ActLab_codes].[Au] AS varchar(20))
ELSE CAST( [TBL_whole_rock_geochem].[Au] AS varchar(20)) END AS [Au det]
FROM (SELECT CAST(342.156 as float) AS Au)TBL_ActLab_codes
CROSS
JOIN (SELECT CAST(3657.627 as float) AS Au
UNION ALL
SELECT CAST(0 as float) AS Au)TBL_whole_rock_geochem
Results from a CASE statement can have only one data type and to decide which is going to be used, the rules of data type precedence apply.
February 11, 2015 at 10:32 pm
cm62597 (2/11/2015)
I have tried that as well you just get Error converting data type varchar to float.
Quick question, can you post the full query and the DDL for the relevant tables? Luis's code works perfectly for the snipped you posted but if it doesn't work at your end then you aren't telling us the full story.
😎
February 12, 2015 at 4:28 am
When you say this
SELECT CASE WHEN [TBL_whole_rock_geochem].au = 0
What is the data type of column [TBL_whole_rock_geochem].au column. because following can also generate the error. Consider the following example.
Declare @table Table ( cola nvarchar(20))
insert into @table
select '1' union all
select '.1' union all
select '0' union all
select ''
select * from @table
where cola = 0
Otherwise, Luis snippet should resolve your issue unless you are not telling the complete story.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply