简体   繁体   中英

SQL Server: update column with special characters

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 ^

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)

You can do the casting inline:

INSERT INTO Teams (team_code, team_name, team_short) 
VALUES ('{' + CAST(NEWID() AS VARCHAR(255)) + '}',test,test)

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM