Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Conversion failed when converting from a character string to uniqueidentifier Expand / Collapse
Author
Message
Posted Wednesday, July 28, 2010 9:28 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, July 28, 2010 9:25 AM
Points: 3, Visits: 19
Table has unique identifier column and newid() was inserted into the table.
When select query is executed like below,

select * from table where id = ' d65cafc-1435-45d3-acce-dc464f02c4b1'

error message is displayed.

Error : "Conversion failed when converting from a character string to uniqueidentifier"

Please help
Post #960132
Posted Wednesday, July 28, 2010 9:35 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, December 17, 2014 1:30 AM
Points: 2,386, Visits: 7,622
DECLARE @TABLE AS TABLE(
id UNIQUEIDENTIFIER)

INSERT INTO @TABLE
SELECT Newid()

SELECT *
FROM @TABLE
WHERE CONVERT(VARCHAR(255), id) = 'd65cafc-1435-45d3-acce-dc464f02c4b1'

How's that?



Not a DBA, just trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #960141
Posted Wednesday, July 28, 2010 2:27 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 2:12 PM
Points: 292, Visits: 1,625
Two things.

1. d65cafc-1435-45d3-acce-dc464f02c4b1 is not a proper UNIQUEIDENTIFIER. The first grouping only has 7 characters when there should be 8.

2. 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).
Post #960347
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse