Solved

SQL Cast statement error - varchar to uniqueidentifier

Posted on 2010-08-25
3
1,056 Views
Last Modified: 2012-05-10
Hello all,

I am trying to insert a 36 character string into a test table as shown below, but I still get the famous "Conversion failed when converting from a character string to uniqueidentifier."  Can someone point out the obvious flaw in my query:

create table test3(
id uniqueidentifier
)

declare @id2 varchar(36)
set @id2 = '9D9CB28D-1918-42DB-A7A4-C8A1FC89209Q'

insert into test3
(id)

values(
cast(@id2 as uniqueidentifier)

)

Thanks in advance!
0
Comment
Question by:robthomas09
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 19

Accepted Solution

by:
Bhavesh Shah earned 300 total points
ID: 33524774
0
 
LVL 22

Expert Comment

by:pivar
ID: 33524782
Hi,

You have a 'Q' last in the string, that should be an illegal  char.

/peter
0
 
LVL 16

Assisted Solution

by:vdr1620
vdr1620 earned 200 total points
ID: 33524953
Try the below Sample query..there are no flaws in the Syntax nor the query..Its just that SQL is not able to convert the string into a unique identifier


create table #test3(
id uniqueidentifier
)

declare @id2 varchar(36)
set @id2 = '6F9619FF-8B86-D011-B42D-00C04FC964FF'

insert into #test3
(id)

values(
cast(@id2 as uniqueidentifier)

)

Open in new window

0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question