SQL Server convert Guid to String

Midwest
Midwest used Ask the Experts™
on
This would work but I am getting an error because my Users.Id (guid) is different from Users.Username (varchar).  Can I use CAST somehow to compare these as 2 strings?

DELETE S FROM [DatabaseName].[dbo].[Sessions] as S
	INNER JOIN [DatabaseName].[dbo].[Users] as U on S.UserId = U.Id
    WHERE U.Id = U.Username
GO

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
DELETE S FROM [DatabaseName].[dbo].[Sessions] as S
      INNER JOIN [DatabaseName].[dbo].[Users] as U on S.UserId = U.Id
    WHERE cast(U.Id as char(36)) = U.Username
GO

Author

Commented:
Very nice!

Commented:
...
    WHERE CONVERT(varchar(255), U.Id) = U.Username
...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial