Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL conversion from varchar to uniqueidentifier fails in view

I'm stuck on the following scenario. I have a database with a table with customer data and a table where I put records for monitoring what is happening on our B2B site.

The customer table is as follow:

  • ID, int, not null
  • GUID, uniqueidentfier, not null, primary key
  • Other stuff...

The monitoring table:

  • ID, int, not null
  • USERGUID, uniqueidentifier, null
  • PARAMETER2, varchar(50), null
  • Other stuff...

In PARAMETER1 are customer guids as wel as other data types stored.

Now the question came to order our customers according their last visit date, the most recent visited customers must come on the top of a grid.

I'm using Entity Framework and I had problems of comparing the string and the guid type, so I decided to make a view on top of my monitoring table:

SELECT        
   ID, 
   CONVERT(uniqueidentifier, parameter2) AS customerguid, 
   USERguid, 
   CreationDate
FROM            
   MONITORING
WHERE        
   (dbo.isuniqueidentifier(parameter2) = 1) 
   AND 
   (parameter1 LIKE 'Customers_%' OR parameter1 LIKE 'Customer_%')

I imported the view in EF and made my Linq query. It returned nothing, so I extracted the generated SQL query. When testing the query in SQL Management Studio I got the following error: Conversion failed when converting from a character string to uniqueidentifier.

The problem lies in the following snippet (simplified for this question, but also gives an error:

SELECT *,
    (
        SELECT 
            [v_LastViewDateCustomer].[customerguid] AS [customerguid]
        FROM [dbo].[v_LastViewDateCustomer] AS [v_LastViewDateCustomer]
        WHERE c.GUID = [v_LastViewDateCustomer].[customerguid]
    )

FROM CM_CUSTOMER c

But when I do a join, I get my results:

SELECT *
FROM CM_CUSTOMER c
    LEFT JOIN
    [v_LastViewDateCustomer] v
on c.GUID = v.customerguid

I tried to make a SQL fiddle, but it is working on that site. http://sqlfiddle.com/#!3/66d68/3

Anyone who can point me in the right direction?

like image 522
CyclingFreak Avatar asked Dec 09 '13 15:12

CyclingFreak


People also ask

How do I fix conversion failed when converting from a character string to Uniqueidentifier?

Don't rely on implict conversions. CAST the GUID string to a UNIQUEIDENTIFIER and compare it to your column. Don't CAST the column to a string as SQL cannot seek on the index (assuming there is one). Ensure that the Column you are Converting OR Casting do not contain BLANK '' Values.

How do you convert Uniqueidentifier?

The following example converts a uniqueidentifier value to a char data type. DECLARE @myid uniqueidentifier = NEWID(); SELECT CONVERT(CHAR(255), @myid) AS 'char'; The following example demonstrates the truncation of data when the value is too long for the data type being converted to.

How insert values into Uniqueidentifier column in SQL?

If the table you want to edit participates in replication or offline mapping or contains a GUID, you must insert a unique value to the global ID or GUID column when you insert a new record to the table using SQL. To do this, you can use the newid() function.


1 Answers

Use

TRY_CONVERT(UNIQUEIDENTIFIER, parameter2) AS customerguid

instead of

 CONVERT(UNIQUEIDENTIFIER, parameter2) AS customerguid

Views are inlined into the query and the CONVERT can run before the WHERE.

For some additional discussion see SQL Server should not raise illogical errors

like image 140
Martin Smith Avatar answered Oct 27 '22 12:10

Martin Smith