• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 44
  • Last Modified:

T-SQL extract characters to the left of specific character

I have a string that looks like this:
NOTE - 2 PIECES REQUIRED #02021336173R0 1.50 X 83.25 QTY=2

I need to find the position where there is a " X " and then get the number before this position.  So in this case I need to extract 1.50

How would I do this in t-sql?
0
maverick0728
Asked:
maverick0728
1 Solution
 
Éric MoreauSenior .Net ConsultantCommented:
something like this (better create a function to do it!):
DECLARE @x VARCHAR(100) = 'NOTE - 2 PIECES REQUIRED #02021336173R0 1.50 X 83.25 QTY=2'

SELECT @x, LTRIM(RTRIM(REVERSE(SUBSTRING(REVERSE(RTRIM(SUBSTRING(@x, 1, CHARINDEX(' X ', @x)))), 1, CHARINDEX(' ', REVERSE(RTRIM(SUBSTRING(@x, 1, CHARINDEX(' X ', @x)))))))))

Open in new window

0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
What's happen if there is more than one 'X'? How do you know which one is the one to be used?
0
 
maverick0728Author Commented:
The first instance of the ' X ' would be used
0
 
Éric MoreauSenior .Net ConsultantCommented:
have you tried my query?
0
 
PortletPaulfreelancerCommented:
If you broke-up that string into individual pieces using a function, then you could may get more value from it.

Below I use the SQL Sever 2016 introduced function SPLIT_STRING
Data:
CREATE TABLE Table1
    ([ID] int, [Column1] varchar(200))
;
  
INSERT INTO Table1
    ([ID],[Column1])
VALUES
    (101,'NOTE - 2 PIECES REQUIRED #02021336173R0 1.50 X 83.25 QTY=2')
;

Open in new window

Queries:
SELECT
        id, value
FROM (
      SELECT
              id
            , lead(value) over(order by (select 1))  next_value
            , value
      FROM Table1
      CROSS APPLY STRING_SPLIT ( Column1 , ' ' )  
    ) d
where next_value = 'X'
;

      SELECT
              id
            , lead(value) over(order by (select 1))  next_value
            , value
      FROM Table1
      CROSS APPLY STRING_SPLIT ( Column1 , ' ' )  

Open in new window

Results:
|  id | value |
|-----|-------|
| 101 |  1.50 |
        

|  id |     next_value |          value |
|-----|----------------|----------------|
| 101 |              - |           NOTE |
| 101 |              2 |              - |
| 101 |         PIECES |              2 |
| 101 |       REQUIRED |         PIECES |
| 101 | #02021336173R0 |       REQUIRED |
| 101 |           1.50 | #02021336173R0 |
| 101 |              X |           1.50 |
| 101 |          83.25 |              X |
| 101 |          QTY=2 |          83.25 |
| 101 |         (null) |          QTY=2 |

Open in new window

also see this working at: http://sqlfiddle.com/#!18/5cf32/2
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now