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?
maverick0728Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

É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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
PortletPaulEE Topic AdvisorCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
T-SQL

From novice to tech pro — start learning today.