Uniqueidentifier.

  • 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.

  • Try with using Navarchar(MAX)

  • 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


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • 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

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • As a quick proof of concept, you could start with something like...

    select cast(newid() as sysname)

    -- OR --

    print cast(newid() as sysname)

  • 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


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply