SQL Syntax - Error Converting character String to a unique Identifier

Hi there, I am trying to copy a column in a table to another column, but the source column is a varchar(50) and the destination column is a unique identifier.  Of course, I routinely get the error that:
Msg 8169, Level 16, State 2, Line 1
Conversion failed when converting from a character string to uniqueidentifier.

Can I convert this somehow?
Thanks!
Convert(uniqueidentifier,column_name_original)

Open in new window

robthomas09Asked:
Who is Participating?
 
Dale BurrellConnect With a Mentor DirectorCommented:
As the error says, one or more of your string values is either not a GUID or is not formatted corrected as per my first post. This is common problem when trying to move untyped data to typed data. You need to build some queries to find the bad data and then build you query to correct it. If you take a look a the MSDN reference for the 'LIKE' statement you'll see you can have it match a pattern which should help you to identify those that don't.
0
 
Dale BurrellDirectorCommented:
You can only convert it if it really is a GUID in string form. You can find out the format it must be in by running 'select convert(varchar(38),newid())'. If you can't express your existing string in that exact format you'll need to find another solution.
0
 
robthomas09Author Commented:
Thanks Dale - I have confirmed that it is a GUID in string form.

Thanks
0
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
Dale BurrellDirectorCommented:
In that case you code snippet will work.
0
 
robthomas09Author Commented:
Well maybe I spoke too soon - I still receive the error when I try this:

SELECT  CAST(src_patient_id AS UNIQUEIDENTIFIER)
from ncs_conv_GENappt_recalls

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

Thoughts?
0
 
robthomas09Author Commented:
I found em- thanks Dale!
0
All Courses

From novice to tech pro — start learning today.