Sql server 如何在SQL SERVER中仅从表列中选择部分数据
我已经创建了一个SQL SERVER脚本。首先请检查脚本Sql server 如何在SQL SERVER中仅从表列中选择部分数据,sql-server,sql-server-2014,Sql Server,Sql Server 2014,我已经创建了一个SQL SERVER脚本。首先请检查脚本 SELECT W.RequesterName + ' ' + +'on ' + CONVERT(varchar(20), W.Requested, 111) + ' ' + CONVERT(varchar(20), W.Requested, 108) + ' ' + (CASE WHEN DATEPART(HOUR, W.Requested) > 12 THEN 'PM' ELSE 'AM' END) AS
SELECT
W.RequesterName + ' ' + +'on ' + CONVERT(varchar(20), W.Requested, 111) + ' ' + CONVERT(varchar(20), W.Requested, 108) + ' ' + (CASE
WHEN DATEPART(HOUR, W.Requested) > 12 THEN 'PM'
ELSE 'AM'
END) AS 'Request Detail',
W.TakenByName,
W.Reason,
CONVERT(varchar(20), W.TargetDate, 111) + ' (' + CONVERT(nvarchar(max), CAST(W.TargetHours AS float)) + ')' + ' hr' AS targetWorkDetails,
W.PriorityDesc + '/' + W.TypeDesc AS priorityDesc,
W.SupervisorName,
W.ShopID,
W.RepairCenterName,
W.RequesterPhone,
A.Location3Name,
A.ParentLocation,
A.ParentLocationAll,
W.AssetName,
W.AssetName + '(' + CONVERT(nvarchar(20),w.AssetID,101) + ')' AS AssetDetails
FROM WO W
INNER JOIN AssetHierarchy A
ON W.AssetPK = A.AssetPK
WHERE w.WOPK = 10144 --INNER JOIN AssetHierarchy ON WO.AssetPK=AssetHierarchy.AssetPK
现在请检查我的输出
现在请检查突出显示的属性“ParentLocationAll”。我的任务是在标记开始之前获取ParentLocationAll数据。例如,在ParentLocationAll一栏中,我们有“Wood Buffalo Main Building”的值,我只想选“Wood Buffalo”。我想再举一个例子:假设“ParentLocationAll”列中有值“calgeryabcd”。然后我必须只取“calgery”而不是整个值。
我已经尽力了,但解决不了这个问题。请帮我拿些好的
建议。提前感谢。将PATINDEX与LEFT结合使用可能会在这里起作用:
WITH yourTable AS (
SELECT 'Wood Buffalo<br>Main Building' AS ParentLocationAll
)
SELECT
LEFT(ParentLocationAll d, PATINDEX('%<%>%', ParentLocationAll) - 1) AS ParentLocation
FROM yourTable;
Wood Buffalo <-- output
这是否在所有情况下都有效取决于您的数据。例如,如果您的父位置列有随机符号,而这些符号不出现在HTML标记的上下文中,则我的解决方案可能会失败。将PATINDEX与LEFT结合使用可能会在以下情况下奏效:
WITH yourTable AS (
SELECT 'Wood Buffalo<br>Main Building' AS ParentLocationAll
)
SELECT
LEFT(ParentLocationAll d, PATINDEX('%<%>%', ParentLocationAll) - 1) AS ParentLocation
FROM yourTable;
Wood Buffalo <-- output
这是否在所有情况下都有效取决于您的数据。例如,如果您的父位置列有随机符号,并且没有出现在HTML标记的上下文中,则我的解决方案可能会失败。严格根据下面提供的输入数据,代码片段将完成以下工作-
create table #table (ParentLocationAll nvarchar(1000))
insert into #table (ParentLocationAll) select 'Wood Buffalo Main Building'
insert into #table (ParentLocationAll) select 'calgery abcd'
select
case when len(ParentLocationAll) - len(replace(ParentLocationAll, ' ', '')) = 1
then LEFT(ParentLocationAll, charindex(' ', ParentLocationAll) - 1)
else SubString(ParentLocationAll, 1, CharIndex(' ', ParentLocationAll, CharIndex(' ', ParentLocationAll) + 1)) end as ParentLocationAll
from #table
因此,这里的代码只是检查字符串中的空格数“”,如果计数为1,它将提取字符串,直到找到第一个空格,否则它将在主字符串中找到第二个空格,并提取字符串,直到找到为止。严格根据下面提供的输入数据,代码片段将完成以下工作-
create table #table (ParentLocationAll nvarchar(1000))
insert into #table (ParentLocationAll) select 'Wood Buffalo Main Building'
insert into #table (ParentLocationAll) select 'calgery abcd'
select
case when len(ParentLocationAll) - len(replace(ParentLocationAll, ' ', '')) = 1
then LEFT(ParentLocationAll, charindex(' ', ParentLocationAll) - 1)
else SubString(ParentLocationAll, 1, CharIndex(' ', ParentLocationAll, CharIndex(' ', ParentLocationAll) + 1)) end as ParentLocationAll
from #table
因此,这里的代码只是检查字符串中的空格数“”,如果计数为1,则它将提取字符串直到找到第一个空格,否则它将在主字符串中找到第二个空格并提取字符串直到找到为止。为了回答这个问题,我们需要特定的,明确的方法来了解你如何辨别要拆分的内容的规则,而不是简单的例子。为了回答这个问题,我们需要具体的,明确的方法来了解如何区分要拆分的内容,而不是简单的示例。在我的情况下,HTML标记将始终用于ParentLocationAll列。@Tim在我的情况下,HTML标记将始终用于ParentLocationAll列。@Tim