某些字符前后的SQL字符串

时间:2017-12-04 19:21:24

标签: sql-server tsql

SELECT NAME 
FROM SERVERS  

返回:

SDACR.hello.com  
SDACR 
SDACR\AIR
SDACR.hello.com\WATER

我需要SELECT查询以下结果:

SDACR  
SDACR  
SDACR\AIR
SDACR\WATER

请帮忙!我尝试使用LEFT和RIGHT函数,如下所示,但无法正确组合输出:

 SELECT 
     LEFT(Name, CHARINDEX('.', Name) - 1) 
 FROM 
     SERVERS     

SELECT 
    RIGHT(Name, LEN(Name) - CHARINDEX('\', Name)) 
FROM
    SERVERS

4 个答案:

答案 0 :(得分:1)

看起来您只是尝试REPLACE列中字符的子字符串。你应该试试这个:

SELECT REPLACE(Name,'.hello.com','') AS ReplacementName
FROM SERVERS

答案 1 :(得分:0)

在tsql中,您可以使用CONCAT()连接值,或者只需将字符串与+一起添加。

SELECT LEFT(Name, CHARINDEX('.',Name)-1) + RIGHT(Name,LEN(Name)-CHARINDEX('\',Name)) from SERVERS

另外,请注意使用CHARINDEX()进行算术运算。没有'。'的值或者'\'将返回NULL,您将收到错误。

答案 2 :(得分:0)

您可以使用LEFT来选择第一个句点(点)之前的所有内容,并添加最后一个\之后的所有内容

declare @servers table ([NAME] varchar(64))
insert into @servers
values
('SDACR.hello.com '),
('SDACR'), 
('SDACR\AIR'),
('SDACR.hello.com\WATER')

select 
    left([NAME],case when charindex('.',[NAME]) = 0 then len([NAME]) else charindex('.',[NAME]) -1 end) + 
    case when charindex('\',left([NAME],case when charindex('.',[NAME]) = 0 then len([NAME]) else charindex('.',[NAME]) -1 end)) = 0 then right([NAME],charindex('\',reverse([NAME]))) else '' end
from @servers

答案 3 :(得分:0)

把我的帽子扔进去......展示如何使用值和APPLY来获得更干净的代码。

<table class="table table-bordered">
  <thead>
    <tr><th>Status</th> <th>Total Price</th> <th>Time</th></tr>
  </thead>
  <tbody>
  <?php
        $cons_no="DELIVERED";
  $result = mysqli_query($conn, "SELECT SUM(totalprice) AS value_sum FROM orders WHERE status = '$cons_no' AND date BETWEEN (CURRENT_DATE() - INTERVAL 2 MONTH) AND CURRENT_DATE()");
        $row = mysqli_fetch_assoc($result); 
        $sum = $row['value_sum'];
        //$query2 = "SELECT * FROM orders WHERE status = '$cons_no' AND YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)";
        $query2 = "SELECT * FROM orders WHERE status = '$cons_no' AND date BETWEEN (CURRENT_DATE() - INTERVAL 2 MONTH) AND CURRENT_DATE()";
        $results2 = $conn->query($query2);
        if($results2){
            $currentDate = false;
            while($row2 = $results2->fetch_assoc()) {
    ?>
    <tr>
        <td><?php echo $row2['status']; ?></td>
        <td><?php echo $row2['totalprice']; ?></td>
        <td><?php echo $row2['date']; ?></td>
    </tr>
    <?php
          }

        }
    ?>

        <td><?php echo "$".number_format($sum); ?></td>
    </tbody>

<强>结果

-- sample data in an easily consumable format
declare @yourdata table (txt varchar(100));
insert @yourdata values 
('SDACR.hello.com'),
('SDACR'),
('SDACR\AIR'),
('SDACR.hello.com\WATER');

-- solution
select 
  txt, 
  newTxt = 
  case 
    when loc.dot = 0 then txt
    when loc.dot > 0 and loc.slash = 0 then substring(txt, 1, loc.dot-1)
    else substring(txt, 1, loc.dot-1) + substring(txt, loc.slash, 100)
  end
from @yourdata
cross apply (values (charindex('.',txt), (charindex('\',txt)))) loc(dot,slash);