我从我的sql server数据库创建了一个临时表,如下所示。临时表有列标题& Descripton和其他一些专栏:
Title Description ABC Ford Toyota
Ford Car This is a Car A
Car This is a Toyota car B
Toyota Truck This is a truck C
Car This is a ford car D
我需要创建一个输出,如果标题或描述中有一个单词“Ford”或“Toyota”。那个地方我需要一个“是”。我需要结果如下所示:
*
Title Description ABC Ford Toyota
Ford Car This is a Car A Yes No
Car This is a Toyota car B No Yes
Toyota Truck This is a truck C No Yes
Car This is a ford car D Yes No
car ford toyota E Yes Yes
*
您能否告诉我这是如何运作的?有什么指针吗?我有点被困在这里..
答案 0 :(得分:2)
使用CASE
表达式:
UPDATE tbl
SET Ford = CASE WHEN Title LIKE '%Ford%' OR Description LIKE '%Ford' THEN 'YES' ELSE 'NO' END,
Toyota = CASE WHEN Title LIKE '%Toyota%' OR Description LIKE '%Toyota' THEN 'YES' ELSE 'NO' END
答案 1 :(得分:0)
您可以自行更新表格,我的查询提供了您所描述的示例:
CREATE TABLE #Temp (
Title NVARCHAR(20) NULL,
Description NVARCHAR(50) NULL,
ABC NVARCHAR(1) NULL,
Ford NVARCHAR(3) NULL,
Toyota NVARCHAR(3) NULL)
INSERT INTO #Temp VALUES
('Ford Car','This is a Car','A',NULL,NULL),
('Car','This is a Toyota car','B',NULL,NULL),
('Toyota Truck','This is a truck','C' ,NULL,NULL),
('Car','This is a ford car','D' ,NULL,NULL)
UPDATE #Temp
SET Ford = IIF(#Temp.Title LIKE '%Ford%' OR #Temp.Description LIKE '%Ford%','Yes','No')
,Toyota = IIF(#Temp.Title LIKE '%Toyota%' OR #Temp.Description LIKE '%Toyota%','Yes','No')