Home > OS >  How trim() works in SQL Server
How trim() works in SQL Server

Time:01-03

I have a column SysTraNo with some specific data like

HO/20-21/DRP/0001
215/21-22/AGP/0003

I want to trim that whole column and only take 20-21 or 21-22 of that data. How can I do this?

CodePudding user response:

Using the base string functions we can try:

SELECT val,
       SUBSTRING(val,
                 CHARINDEX('/', val)   1,
                 CHARINDEX('/', val, CHARINDEX('/', val)   1) -
                     CHARINDEX('/', val) - 1) AS nums
FROM yourTable;

screen capture from demo link below

enter image description here

  • Related