Conversion failed when using nvarchar, but not varchar - sql-server

I've run into a situation where I'm getting an error if I run the same query on the same table, the difference being the column type. The query used is:
SELECT [name] FROM [demo] WHERE [name] = 1111111
If the name column is varchar(7) then it runs no problem.
If the name column is nvarchar(7) then it gives an error:
Conversion failed when converting the nvarchar value 'BBBBBBB' to data type int.
The error makes sense - I can see why the integer value can't be directly compared to the string value. I also realise that I can resolve it with a cast to a string for the condition.
However, what I'm not getting is why the behaviour is different for nvarchar and varchar. This chart appears to suggest that they should have the same behaviour for type conversion.
Does anyone know why this is happening?

If you using nvarchar column your query should look like:
SELECT [name] FROM [demo] WHERE [name] = N'1111111'
N at the begin of string to make sure that your string is unicode string (nvarchar)

Related

Conversion failed when converting from a character string to uniqueidentifier in SQL SERVER select statement

When I execute the query below, I get the error
Conversion failed when converting from a character string to uniqueidentifier.
SELECT * FROM TBL_IncidentDetails where IncidentId = '1822CBE-4616-4ABE-B562-D3CC925D68ED'
Below is my column data
Appreciate assistance
Error is pretty clear, that value isn't a uniqueidentifier.
If you check against a proper GUID (i.e. SELECT NEWID() -> 919053E6-7CE6-4324-9A58-A2EACA5E0F5F) notice that the first "block" has 8 characters (919053E6). Yours, however, has 7 characters (1822CBE).
On consideration, this is probably simply a typographical error.

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.

Error converting data type nvarchar to numeric - Collation

Error converting data type nvarchar to numeric.
SELECT HCI.variationID, CAST(isnull(HCI.ContractRef, 0) as numeric(32,2)) as
targethrs
FROM pcms.Variation_New HCI
Quite a simple error but I have tried numerous things without joy. this may help
Column Name Type Collation
ContractRef nvarchar(50) Latin1_General_CI_AS
I need a numeric outcome
As Smor mentioned, try_convert() or try_cast() will not throw an error if the conversion fails... instead it will return a NULL.
SELECT HCI.variationID
, isnull( try_convert(numeric(32,2),HCI.ContractRef), 0) as targethrs
FROM pcms.Variation_New HCI
The real issue is that you have data that can not be converted. To identity these records for correction:
Select *
From pcms.Variation_New HCI
Where try_convert(numeric(32,2),HCI.ContractRef) is null
and HCI.ContractRef is not null

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