Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert GUID to varchar(32)

How can I convert a GUID which is 36 characters to a VARCHAR(32)?

I'm trying to copy data from one table to another. There are two similar columns from these two tables.

  1. Table1.colx is a GUID so it is 36 characters in length in total due to the hyphens
  2. The corresponding column is table2.colx but it is a VARCHAR(32)

I am looking for a way to convert a GUID to VARCHAR, but I've got to remove the hyphens. So far I have been unsuccessful in my attempts to find a way to do this.

like image 517
Jfabs Avatar asked Apr 25 '13 18:04

Jfabs


People also ask

What's the difference between Nvarchar and varchar?

The key difference between varchar and nvarchar is the way they are stored, varchar is stored as regular 8-bit data(1 byte per character) and nvarchar stores data at 2 bytes per character. Due to this reason, nvarchar can hold upto 4000 characters and it takes double the space as SQL varchar.

What is Uniqueidentifier in SQL Server?

Uniqueidentifier is a Microsoft SQL Server data type that is used to store Globally Unique Identifiers (GUIDs). It can store 16 bytes of data. The Developer tool treats the Uniqueidentifier data type as String. To move or change Uniqueidentifier data, connect the Uniqueidentifier column to a String column.

How do you do a lowercase in SQL?

LOWER() function in SQL Server This function in SQL Server helps to convert all the letters of the given string to lowercase. If the given string contains characters other than alphabets, then they will remain unchanged by this function. Parameters : str – The string which will be converted to lowercase.


1 Answers

I assume this is SQL Server, from the SSMS tag.

Convert the GUID to a string, then replace the hyphens with empty strings:

REPLACE(CAST(table1.colx AS VARCHAR(36)),'-','')
like image 148
Esoteric Screen Name Avatar answered Sep 20 '22 15:09

Esoteric Screen Name