November 23, 2011 at 10:49 am
I have a variable of type uniqueidentifier. I would to include this in a varchar. I have tried CAST(@Var, NVARCHAR(50)) and CONVERT. But when I try to print the varchar it is empty. Any ideas?
SET @RulesName = 'Site: 1, RoleType: 2, Role: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
SET @RulesName = REPLACE(@RulesName, '1', @SiteName_Id);
SET @RulesName = REPLACE(@RulesName, '2', @RoleType_Id);
SET @RulesName = REPLACE(@RulesName, 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', CONVERT(NVARCHAR(50),@Var));
Thanks
Kevin.
November 23, 2011 at 10:58 am
Try with using Navarchar(MAX)
November 23, 2011 at 11:05 am
not sure where your issue likes, i'm able to convert to a varchar(40) no problem:
create table Example(id int identity(1,1) not null primary key,myUQ uniqueidentifier)
GO
insert into Example (myUQ) VALUES (newid())
GO 10 --insert tthe above 10 times!
SELECT id, myUQ,convert(varchar(40),myUQ) FROM Example
Lowell
November 23, 2011 at 11:11 am
You don't need an NVarchar for uniqueidentifier, it contains the characters A-F, 0-9 and - and is 36 characters long.
Try this:
SET @RulesName = 'Site: 1, RoleType: 2, Role: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
SET @RulesName = REPLACE(@RulesName, '1', @SiteName_Id);
SET @RulesName = REPLACE(@RulesName, '2', @RoleType_Id);
SET @RulesName = REPLACE(@RulesName, 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', CAST(@Var AS VARCHAR(36));
SELECT @RulesName
If that doesn't work, please post the values that @SiteName_Id, @RoleType_Id and @Var have at the beginning of that code block so I can test it. This is very likely a case of propagation of nulls. Check the values of @SiteName_Id, @RoleType_Id and @Var before that code block.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
November 23, 2011 at 6:51 pm
As a quick proof of concept, you could start with something like...
select cast(newid() as sysname)
-- OR --
print cast(newid() as sysname)
November 23, 2011 at 7:31 pm
kev43barrie (11/23/2011)
I have a variable of type uniqueidentifier. I would to include this in a varchar. I have tried CAST(@Var, NVARCHAR(50)) and CONVERT. But when I try to print the varchar it is empty. Any ideas?
It probably sounds like a stupid question but did you populate the variable with anything?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply