Solved

SQL Cast statement error - varchar to uniqueidentifier

Posted on 2010-08-25
3
1,050 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
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

832 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