0

I'm new to SQL Server so please excuse me for maybe stupid question.

I have a database that I need to generate random id which I achieve by using newid()

insert into Teams (team_code, team_name, team_short) 
values (newid(),test,test)

But the problem is that team_code id need to be in curly brackets and I don't know how to do it.

Do you have any suggestions?

Thanks ^

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Greg
  • 1
  • 1

3 Answers3

0

Try this way because newid() direct not set curly brackets

DECLARE @ID varchar(max);
    SET @ID = newid();
select @ID
insert into Teams (team_code,team_name,team_short) values ('{'+@ID+'}',test,test)

AND also you try as

insert into Teams (team_code, team_name, team_short) values ('{'+Convert( VARCHAR(max),NEWID())+'}',test,test)
Hiral Nayak
  • 1,062
  • 8
  • 15
0

You can do the casting inline:

INSERT INTO Teams (team_code, team_name, team_short) 
VALUES ('{' + CAST(NEWID() AS VARCHAR(255)) + '}',test,test)
How 'bout a Fresca
  • 2,267
  • 1
  • 15
  • 26
-1

So you want to add a leading { and a trailing } to the created unique identifier string? This is SQL Server, right? Then you concatenate string with + as shown here:

insert into Teams (team_code, team_name, team_short) 
values ('{' + newid() + '}', test, test);
Thorsten Kettner
  • 89,309
  • 7
  • 49
  • 73