September 22, 2015 at 11:20 am
I have a field I am trying to bring into a SQL statement
,ISNULL(Convert(nvarchar(50),OPP1.OriginatingLeadId),'') AS 'OriginatingLeadId'
I get this error message
Conversion failed when converting from a character string to uniqueidentifier.
the field specs' originatingleadid is attached
I am new to SQL, been using MS Access for years, but just got a new job this year and doing a lot more SQL. I really thought I had this correct. Any help is appreciated.
September 22, 2015 at 5:28 pm
I can't see anything wrong with that expression, so maybe the problem is elsewhere.
Are you trying to insert that data into another uniqueidentifier column?
MM
select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);
September 22, 2015 at 9:17 pm
randyetheridge (9/22/2015)
I have a field I am trying to bring into a SQL statement,ISNULL(Convert(nvarchar(50),OPP1.OriginatingLeadId),'') AS 'OriginatingLeadId'
I get this error message
Conversion failed when converting from a character string to uniqueidentifier.
the field specs' originatingleadid is attached
I am new to SQL, been using MS Access for years, but just got a new job this year and doing a lot more SQL. I really thought I had this correct. Any help is appreciated.
In your ISNULL statement, you are building an empty string if the column is NULL. That is not valid for a UNIQUEIDENTIFIER data type.
DECLARE @t UNIQUEIDENTIFIER = ''
This will return the same error that you are seeing.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
September 22, 2015 at 9:46 pm
randyetheridge (9/22/2015)
I have a field I am trying to bring into a SQL statement,ISNULL(Convert(nvarchar(50),OPP1.OriginatingLeadId),'') AS 'OriginatingLeadId'
I get this error message
Conversion failed when converting from a character string to uniqueidentifier.
the field specs' originatingleadid is attached
I am new to SQL, been using MS Access for years, but just got a new job this year and doing a lot more SQL. I really thought I had this correct. Any help is appreciated.
Quick suggestion, use a pseudo null value for the uniqueidentifier (00000000-0000-0000-0000-000000000000), implement it by replacing the empty string in the ISNULL function to that value.
😎
,ISNULL(Convert(nvarchar(50),OPP1.OriginatingLeadId),'00000000-0000-0000-0000-000000000000') AS 'OriginatingLeadId'
If you need to get the null in a select where it's been replaced by the pseudo value use the NULLIF function
,NULLIF(CONVERT(VARCHAR(50),OriginatingLeadId,0),'00000000-0000-0000-0000-000000000000')
September 23, 2015 at 3:38 am
WayneS (9/22/2015)
In your ISNULL statement, you are building an empty string if the column is NULL. That is not valid for a UNIQUEIDENTIFIER data type.
DECLARE @t UNIQUEIDENTIFIER = ''
This will return the same error that you are seeing.
That shouldn't be an issue here should it ? (unless I am missing something 🙂 )
SELECT ISNULL(Convert(nvarchar(50), CONVERT(uniqueidentifier, NULL)),'')
AS 'OriginatingLeadId'
September 23, 2015 at 3:47 am
Kristen-173977 (9/23/2015)
WayneS (9/22/2015)
In your ISNULL statement, you are building an empty string if the column is NULL. That is not valid for a UNIQUEIDENTIFIER data type.
DECLARE @t UNIQUEIDENTIFIER = ''
This will return the same error that you are seeing.
That shouldn't be an issue here should it ? (unless I am missing something 🙂 )
SELECT ISNULL(Convert(nvarchar(50), CONVERT(uniqueidentifier, NULL)),'')
AS 'OriginatingLeadId'
Until one tries to inert the empty string into the uniqueidentifier column;-)
😎
September 23, 2015 at 4:40 am
Eirikur Eiriksson (9/23/2015)
Until one tries to inert the empty string into the uniqueidentifier column;-)😎
Indeed :-), and that is actually what the error message is saying of course:
"Conversion failed when converting from a character string to uniqueidentifier."
Slightly begs the question why the NULL cannot be left there and the Presentation APP/whatever deal with displaying "NULL as blank string" ...
... and a perennial problem when converting numbers, dates, GUIDs, etc. to strings and then assuming that the downstream APP/usage will be able to successfully parse them back again ...
September 23, 2015 at 6:32 am
Took some help from a friend but the answer turned out to be simple
isnull(cast([OriginatingLeadId] as VARCHAR(36)),'NA')
when checking for NULL in a uniqueidentifier field you have to cast it as VARCHAR and you must use the 36 length to match up to the original field size. worked like a charm.
September 23, 2015 at 10:02 am
randyetheridge (9/23/2015)
Took some help from a friend but the answer turned out to be simpleisnull(cast([OriginatingLeadId] as VARCHAR(36)),'NA')
when checking for NULL in a uniqueidentifier field you have to cast it as VARCHAR and you must use the 36 length to match up to the original field size. worked like a charm.
I can't see how that would behave any differently to your original
ISNULL(Convert(nvarchar(50),OPP1.OriginatingLeadId),'') AS 'OriginatingLeadId'
I did wonder when I saw the original if there was any point in the Nvarchar, rather than Varchar, but I can't see it having the side effect you mention.
Ditto 36 vs. 50 width, or '' blank string vs. 'NA' string.
But I'd be very interested to hear how any of those could constitute a difference.
September 23, 2015 at 1:09 pm
not sure I am technical enough to explain it correctly, but as it was explained to me, the ISNULL answer takes on the attributes of the original field. In this instance uniqueidentier, which is very specific field type. In order replace the NULL value with NA I had originally done this
isnull(cast([OriginatingLeadId] as VARCHAR(2)),'NA') (yes I know not my original post, but I had changed it after the post and some additional research)
I had set the VARCHAR to 2. because I only wanted to add NA. But not thinking, for those times when the field had a value, it of course would be a 36 character value. So the simple answer was to set it to VARCHAR to allow NA value, but to set the column width to 36 to allow for the original field length.
I hope that helps.
September 23, 2015 at 2:48 pm
randyetheridge (9/23/2015)
not sure I am technical enough to explain it correctly, but as it was explained to me, the ISNULL answer takes on the attributes of the original field. In this instance uniqueidentier, which is very specific field type. In order replace the NULL value with NA I had originally done this
Yup, I can see where you are coming from. But your code (originally posted here, maybe the code you were working on was somewhat different) had
ISNULL(CONVERT(nvarchar(50), YourGUID) ...
ISNULL is going to see the first parameter as being an NVarchar(50), and not a GUID, hence why I was questioning why the change made any difference.
ISNULL(CAST YourGUID AS NVarchar(50))
is equally going to see the first parameter as being an NVarchar(50), and not a GUID, so the two are interchangeable in that sense.
My concern is that the fix you have found is not actually in this statement, but somewhere else, and thus may only be serendipity, hence why I was questioning it.
However, I'm pleased you've got it going 🙂 just anxious in case the fix is not the final fix!
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply