0

Suppose I have this query.

SELECT 
    proj.refno [Reference No.], 
    proj.projname [NNNN], 
    TotalCost= '$' + CONVERT(NVARCHAR(100),cast(ROUND((cast(ship.volfinish as int) * data.price)/1000,2) as decimal(5,2)))
 FROM 
     projects proj
 INNER JOIN 
     projdata data ON proj.controlno = data.controlno 
 INNER JOIN 
     shipment ship ON data.ctrlno = ship.dctrlno 
 WHERE 
     proj.refno IN ('item1', 'item2','item3') 
 ORDER BY 
     proj.refno

with this output:

    Reference No.           NNNN                                                TotalCost
GR-NFS52  abc123             StudentsTitle123 (NNNN: xxxxxxxxxxxxx)                                $215.45
GR-PFS53  def456             StudentsTitle456 (NNNN: xxxxxxxxxxxxx)                                $259.55
GR-SSFS43   ghi789         StudentsTitle789 (NNNN: xxxxxxxxxxxxx)                           $242.35

How can I make the NNNN column used the substring function with this output. Cause I'm not into t-sql.

NNNN
xxxxxxxxxxxxx
xxxxxxxxxxxxx
xxxxxxxxxxxxx

2 Answers 2

1

Assuming you have pattern like NNNN: xxxxxxxxxxx) in your strings you can extract this number using some simple manipulation over the string value using charindex and substring:

declare @str nvarchar(max)
select @str = 'Students (NNNN: 9781410314291)'

select substring(@str, 
               charindex('ISBN:', @str) + 6, 
               charindex(')', @str, charindex('NNNN:', @str)) - charindex('NNNN:', @str) - 6)

Here we first find position of NNNN: substring, then position of first occurence of closing bracket ) after this substing and taking part of string between these positions - it is exactly number you need.

In your particular case you can use outer apply in select query in order to make it more readable by avoiding multiple copy-pasting the same charindex('NNNN:', proj.projname) expression:

select
    proj.refno [Reference No.], 
    substring(proj.projname, 
             CALC.pos_from, 
             charindex(')', proj.projname, CALC.pos_from) - CALC.pos_from - 6) as [NNNN],
    ....
FROM projects proj
  .....
   outer apply (select charindex('NNNN:', proj.projname) as pos_from) as CALC
Sign up to request clarification or add additional context in comments.

2 Comments

it displays 9781410314291) i try to use -1 after the ) but it doesn't work so I can remove the close parenthesis
@404notfound Oh, it was a typo in the answer. Actually it should be -6 there, since it is calculation of length of number to extract and we should decrease it by length of ISBN: prefix which is exactly 6. This is needed due to our start position determined at first step - is start position of ёШЫИТё prefix, not the number itself.
1

Try this:

DECLARE @str nvarchar(max) = 'Novels for Students, vol. 52 (ISBN: 9781410314291)'

SELECT 
  REPLACE(STUFF(@str, 1, PATINDEX('% '+REPLICATE('[0-9]', 13) + '%', @str), ''), ')', '')

Result:

9781410314291

3 Comments

thanks! I think I will now be studying stuff, patindex and replicate (Up vote for this)
Little thing - older ISBNs can be 10 characters instead of 13. It's safer to detect the length than hardcode them in this case.
@eftpotrm to be honest, I had no clue what ISBN was. This was a way to detect a pattern, I used 13, because that matched the test data. If data has a different length, it should be easy to adjust the script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.