how to check for a null value in t-sql - sql-server

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'))

Related

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.

T-SQL converting string into float

In a stored procedure on my SQL Server, I am trying to convert values from a varchar column into a float format.
The values into the varchar column are numbers with a sign at the beginning and a '.' before decimals.
Examples: '+0000000000000044.09' or '-0000000000114995.61'
If I try this: convert(float,mystring), it doesn't work.
I have:
Error converting data type varchar to float
Is this kind of conversion possible?
Or is there another way to convert a string with a sign and a '.' into a float?
As your examples both work, I'd guess there's another value somewhere in your table that's causing the problem. In recent versions of SQL Server (2012 onwards), the TRY_CONVERT function can be useful for tracking down this kind of issue.
TRY_CONVERT will not throw an exception on a conversion failure, but instead return a NULL value, so you can figure out which values are causing the problem like this:
SELECT * FROM your_table WHERE TRY_CONVERT(FLOAT, your_column_name) IS NULL
If any rows are returned, those are the rows with problem values that can't be converted to FLOAT.
You can try using CAST(mystring as float) or TRY_CONVERT(float,mystring).
Thuough convert(float,mystring) also should work fine. I would suggest checking your data.

Conversion failed when using nvarchar, but not varchar

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)

Date Time Variable and the blank or Null Values in a Date Column

When I have an execute sql task where I have the
Select '' AS [CancelledDate] from TableX
I map this column to a data time variable called CancelledDate, however when I run the Execute Sql Task I get this Error:
Error: 0xC002F309 at Sp Get Parameter for SP2, Execute SQL Task: An error occurred while assigning a value to variable "CancelledDate": "String was not recognized as a valid DateTime.".
Task failed: Sp Get Parameter for SP2
If I replace the '' with '01-JAN-1978' it works. This must have a problem with Null and '' in the columns.
I tried to put NULL(DT_DATE) in the variable and set evaluate as expression to True but that did not work either!
Well, first of all - empty string is not the same as NULL. NULL (no value) can be "converted" to any type, 'cause each type may be in NULL state while empty string is not nothing and it may or may not be successfully converted to the type you need.
Second - column cannot have no type. Which type is your column CancelledDate? It is varchar. I guess, varchar(1). It contains empty string. Is this a valid datetime? No.
Always explicitly specify type for such a virtual column. And for logical NULL use NULL itself instead of empty string or zero or something else.
Here is sample code that returns datetime column with NULL:
select cast(NULL as datetime) AS [CancelledDate]
from TableX

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