简体   繁体   English

在SQL中修剪输出

[英]Trim output in sql

I have this query 我有这个查询

 Select distinct 
    tag 
 from 
     (SELECT  *
      FROM [piarchive].[picomp2]
      WHERE tag like '%CPU_Active' 
        and time between '2014/10/01 10:13:08' and '2014/10/01 10:18:37'
        and value = -524289 
      order by time desc) as t1

With this I am getting output 有了这个我得到输出

RSV23.VMS_CPU01_1_0.CPU_Active

I need to trim this output to 我需要将此输出修剪为

RSV23.VMS_CPU1_1_0

And need to use this trim output in different query like below 并且需要在如下所示的不同查询中使用此修剪输出

 SELECT  *
 FROM [piarchive].[picomp2]
 WHERE tag like 'RSV23.VMS_CPU01_1_0%' and tag not like '%CPU_Active' and  time   
 between '2014/10/01 10:13:08'and'2014/10/01 10:18:37'  order by time desc

How can I trim first out and use that output in second query? 如何修剪掉并在第二个查询中使用该输出?

I just got the answer. 我才得到答案。

 SELECT top 6 *
 FROM [piarchive].[picomp2]
 WHERE tag like 
 (
   Select distinct left(tag,19) + '%' 
   from (SELECT  *
   FROM [piarchive].[picomp2]
   WHERE tag like '%CPU_Active' and  time between '2014/10/01 10:13:08'and'2014/10/01 10:18:37'
   and value=-524289 order by time desc) as t1
 )  
   and tag not like '%CPU_Active' and tag not like '%Program%' and time between '2014/10/01  
   10:13:08'and'2014/10/01 10:18:37'  order by time desc

You should find last index of dot (.) in query result and chop the string after that index. 您应该在查询结果中找到点(。)的最后一个索引,并在该索引之后截断字符串。

declare @result nvarchar(100)
set @result = (Select distinct tag from (SELECT  * FROM [piarchive].[picomp2] WHERE tag like '%CPU_Active' and time between '2014/10/01 10:13:08' and '2014/10/01 10:18:37' and value = -524289 order by time desc) as t1)

declare @LastIndex int 
set @LastIndex = len(@result) - charindex('.', reverse(@result))

substring(@result,0,@LastIndex) // this will give you the required result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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