[英]How to concatenate two columns values from another table in the insert statement?
I have a table Comment
and I need to insert the values to the table Comment
from another table ProComment
as shown here: 我有一个表
Comment
,我需要将值插入另一个表ProComment
的表Comment
,如下所示:
INSERT INTO Comment (id, insertdate, commenttext, reviewdate)
VALUES (1, GETDATE(), 'This is the new review period from ', GETDATE())
But I want to get the reviewstartdate
and reviewenddate
from ProComment
and need to append with commenttext
in the Comment
table, like this: 但是我想从
ProComment
获取reviewstartdate
和reviewenddate
,并需要在Comment
表中添加commenttext
,如下所示:
INSERT INTO Comment (id, insertdate, commenttext, reviewdate)
VALUES (1, GETDATE(),
'This is the new review period from ' + (SELECT reviewstartdate, 'to ', reviewenddate FROM ProComment WHERE id = 1),
GETDATE())
Expected results will be "This is the new review period from 2018-05-05 to 2019-05-05" on the comment section 预期结果将在评论部分为“这是从2018-05-05到2019-05-05的新审查期”
You need to get rid of the Table Value Constructor values
and put a regular select statement including CONCAT()
function or +
operator to concatenate the strings : 您需要摆脱表值构造函数的
values
并放置一个包含CONCAT()
函数或+
运算符的常规select语句来连接字符串:
insert into Comment( id, insertdate, commenttext, reviewdate)
select 1, getdate(),
concat('This is the new review period from ', reviewstartdate,' to ',reviewenddate),
getdate()
from ProComment
where id = 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.