Sub String

Hi,
I am trying to extract number from rows in a table
I used
select substring(Note,Charindex('Num'.Note,1)+3,3)
from MyTable
but it is not perfect.
For eq.
MyTable

ID                             Note
---------                     ------------------------------------------------------------
1                              fname=san,lname=jj,Num=12,address=abcdef
2                              fname=san,lname=jj,address=abcdef
3                              fname=san,lname=jj,address=abcdef,Num=123

Result
----------
ID                            Num#
-------                       -----------------------
1                              12
2                                0
3                               123

Thanks
SanPrgAsked:
Who is Participating?
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.

Kent DyerIT Security Analyst SeniorCommented:
First thing, locate the records that only have Num..
select substring(Note,Charindex('Num'.Note,1)+3,3)
from MyTable where Note like '%Num%'

Open in new window


Unfortunately, SQL Server does not have a native SPLIT Function like VB/VBA/etc.  Did find the following -

SELECT PARSENAME(REPLACE('Hello John Smith', ' ', '.'), 2)

Open in new window

This would be good information to start with..

{url removed from this post, see admin note below}
0
PortletPaulfreelancerCommented:
I'm suggesting use of "cross apply" here because it reduces the number of time you need to use charindex. The results of the cross apply may then be used in a case expression to produce (I believe) the wanted result.
    | ID |   NUM# | POS1 | POS2 | COLUMN_4 |
    |----|--------|------|------|----------|
    |  1 |     12 |   24 |   26 |        2 |
    |  2 | (null) |    4 |   19 |       15 |
    |  3 |    123 |   39 |   19 |      -20 |

Open in new window

    SELECT
           id
         , CASE
             WHEN pos1 > 4 and pos2 > pos1 THEN substring(Note,pos1,pos2-pos1)
             WHEN pos1 > 4 and pos1 > pos2 THEN substring(Note,pos1,3)
             ELSE NULL 
           END AS "Num#"

      /* only included to inspect 'how' */
      , pos1
      , pos2
      , pos2-pos1

    FROM MyTable
    CROSS apply (
                  SELECT Charindex('Num=',Note,1) + 4, Charindex(',address',Note,1)
                ) AS ca1 (pos1, pos2)
;

    CREATE TABLE MyTable
    	([ID] varchar(9), [Note] varchar(60))
    ;
    	
    INSERT INTO MyTable
    	([ID], [Note])
    VALUES
    	('1', 'fname=san,lname=jj,Num=12,address=abcdef'),
    	('2', 'fname=san,lname=jj,address=abcdef'),
    	('3', 'fname=san,lname=jj,address=abcdef,Num=123')
    ;


  [1]: http://sqlfiddle.com/#!3/c6b2a/12

  [2]: http://sqlfiddle.com/#!3/c6b2a/12/0

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
SanPrgAuthor Commented:
Thanks  PortletPaul,
It is Awesome.
0
PortletPaulfreelancerCommented:
Thanks, all the best, Paul
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
Microsoft SQL Server

From novice to tech pro — start learning today.