如何从SQL中的字符串中获取特定部分

时间:2016-12-21 15:46:03

标签: sql sql-server

我需要从字符串中获取特定部分。

在以下示例中,字段POSITION包含A-块,M-0000000359块,最后是/右侧的块。

我现在需要的是/右侧的完整数字,如果只有,,则只有逗号的完整数字。

因此,如果POSITION的下一个输出为A-M-0000000359/10A-M-0000000359/10,10,那么我现在需要的结果是10

SQL

SELECT POSITION
  ,SUBSTRING((REPLACE(POSITION, SUBSTRING((POSITION), 1, CHARINDEX('/', (POSITION), 1)), '')), 1, CHARINDEX('/', (POSITION), 0)) AS TRIM_A
  ,SUBSTRING((REPLACE(POSITION, SUBSTRING((POSITION), 1, CHARINDEX('/', (POSITION), 1)), '')), 0, CHARINDEX(',', ((REPLACE(POSITION, SUBSTRING((POSITION), 1, CHARINDEX('/', (POSITION), 1)), ''))), 1)) AS TRIM_B
  ,*
FROM ORDER

输出

POSITION             |TRIM_A|TRIM_B
---------------------|------|------|
A-M-0000000359/1     |1
---------------------|------|------|
A-M-0000000359/1,10  |1,10   1

3 个答案:

答案 0 :(得分:3)

您可以使用CASE语句完成此操作。更改@position变量以测试它。

declare @position varchar(64)= 'A-M-0000000359/1111,10'

select 
    case 
        when patindex('%,%',@position) > 0 
        then substring(substring(@position,CHARINDEX('/',@position) + 1,len(@position) - CHARINDEX('/',@position)),1,patindex('%,%',substring(@position,CHARINDEX('/',@position) + 1,len(@position) - CHARINDEX('/',@position))) - 1)
        else substring(@position,CHARINDEX('/',@position) + 1,len(@position) - CHARINDEX('/',@position))
    end

答案 1 :(得分:1)

也许是一个更轻松的选择

Declare @YourTable table (Position varchar(50))
Insert Into @YourTable values
('A-M-0000000359/1,10'),
('A-M-0000000359/1'),
('A-M-0000000359')


Select A.*
      ,Trim_A = case when charindex('/',Position)=0 then '' else substring(Position,charindex('/',Position)+1,50) end
      ,Trim_B = case when charindex(',',Position)=0 then '' 
                     else substring(Position,charindex('/',Position)+1,charindex(',',Position)-charindex('/',Position)-1) 
                     end
 From  @YourTable A

返回

Position             Trim_A   Trim_B
A-M-0000000359/1,10  1,10     1
A-M-0000000359/1     1  
A-M-0000000359      

答案 2 :(得分:1)

请你试试这个,我发现它很简单易懂,我们可以使用public class NewCircle extends Circle { public NewCircle (double x, double y , double radius, Color colore){ super(x,y,radius); this.setFill(colore); this.setOnMousePressed(circleOnMousePressedEventHandler); this.setOnMouseDragged(circleOnMouseDraggedEventHandler); } double orgSceneX, orgSceneY; double orgTranslateX, orgTranslateY; EventHandler<MouseEvent> circleOnMouseClickedEventHandler = new EventHandler<MouseEvent>(){ @Override public void handle(MouseEvent t ){ } }; EventHandler<MouseEvent> circleOnMousePressedEventHandler = new EventHandler<MouseEvent>(){ @Override public void handle(MouseEvent t){ orgSceneX = t.getSceneX(); orgSceneY = t.getSceneY(); Node source = (Node) t.getSource(); orgTranslateX = ((Circle) (t.getSource())).getTranslateX(); orgTranslateY = ((Circle) (t.getSource())).getTranslateY(); ((Circle)t.getSource()).toFront();; } }; EventHandler<MouseEvent> circleOnMouseDraggedEventHandler = new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent t) { Node source = (Node) t.getSource(); Bounds sceneBounds = source.getScene().getRoot().getLayoutBounds(); Bounds localBounds = source.getBoundsInLocal(); double offsetX = t.getSceneX() - orgSceneX; double offsetY = t.getSceneY() - orgSceneY; double newTranslateX = orgTranslateX + offsetX; double newTranslateY = orgTranslateY + offsetY; // restirct x movement to scene bounds if (offsetX >= 0) { if (localBounds.getMaxX() + newTranslateX > sceneBounds.getMaxX()) { newTranslateX = sceneBounds.getMaxX() - localBounds.getMaxX(); } } else { if (localBounds.getMinX() + newTranslateX < 0) { newTranslateX = -localBounds.getMinX(); } } // restrict y movement to scene bounds if (offsetY >= 0) { if (localBounds.getMaxY() + newTranslateY > sceneBounds.getMaxY()) { newTranslateY = sceneBounds.getMaxY() - localBounds.getMaxY(); } } else { if (localBounds.getMinY() + newTranslateY < 0) { newTranslateY = -localBounds.getMinY(); } } source.setTranslateX(newTranslateX); source.setTranslateY(newTranslateY); } }; }

来做到这一点
CASE