我目前正在SQL Server中开发一个报告。假设我有3列。
FirstName | LastName | URL's
----------+----------+------------------------------------------------
Tiffany | Smith | www.example.com/accountname=%3Dtiffany%7Lsmith
网址格式始终为:
www.example.com/accountname=%3Dname%7Lname
如何使用FirstName
和LastName
中的值填充网址列,如上例所示?
答案 0 :(得分:0)
你可以做这样的事情
declare @firstname varchar(10) = 'hello'
declare @lastname varchar(10) = 'there'
select 'www.example.com/accountname=%3D'+@firstname+'%7L'+@lastname as url
返回的网址是
www.example.com/accountname=%3Dhello%7Lthere
只需使用列名替换变量
答案 1 :(得分:0)
使用连接:
create table #temp
(
firstname varchar(50),
lastname varchar(50)
)
insert into #temp (firstname, lastname) values
('tiffany', 'smith'),
('john', 'doe')
select
firstname
, lastname
, concat('www.example.com/accountname=%3D', firstname, '%7L', lastname) as 'url'
from #temp
答案 2 :(得分:0)
;With cte(FirstName , LastName , [URL's])
AS
(
SELECT 'Tiffany', 'Smith', 'www.example.com/accountname=%3D%7L' Union all
SELECT 'john', 'doe',Null
)
SELECT FirstName
,LastName
,ISNULL(STUFF(STUFF([URL's], CHARINDEX('%3D', [URL's]) + 3, 0, FirstName),
CHARINDEX('%7L', STUFF([URL's], CHARINDEX('%3D', [URL's]) + 3, 0, FirstName)),
LEN(LastName), '%7L' + LastName), 'NA') AS [URL's]
FROM cte
输出
FirstName LastName [URL's]
---------------------------------------------------------------------
Tiffany Smith www.example.com/accountname=%3DTiffany%7LSmith
john doe NA