Friday, February 17, 2012

Data type conversion .. Varchar to GUID

Hi,

I have a parameter @.name varchar(50)

but the table has a field with datatype GUID

how I can convert varchar to guid before insertion...

Thanks

As long as the format of the guid is correct, it should be no problem:

create table test
(
columnName uniqueidentifier
)
go
declare @.guid varchar(50) --char(36) would be better
set @.guid = 'E5F61BB0-38BA-4116-841D-C7E5AAA137A2'

insert into test
select @.guid
go

select *
from test

columnName

E5F61BB0-38BA-4116-841D-C7E5AAA137A2

declare @.guid varchar(50) --char(36) would be better
--invalid guid format:
set @.guid = 'E5F61BB038BA4116841DC7E5AAA137A2'

--failure
insert into test
select @.guid

Msg 8169, Level 16, State 2, Line 6
Conversion failed when converting from a character string to uniqueidentifier.

No comments:

Post a Comment