Solved

Substring or Charindex?

Posted on 2009-05-11
5
515 Views
Last Modified: 2012-05-06
I'm trying to only pull the Invoice Number from this data:

InvoiceNo: 14307; Total Amount: $11.72
InvoiceNo: 10444; Total Amount: $40.91
InvoiceNo: 12281; Total Amount: $40.91
InvoiceNo: 8705; Total Amount: $40.91
InvoiceNo: 11503; Total Amount: $35.16
InvoiceNo: 10232; Total Amount: $75.83
InvoiceNo: 12609; Total Amount: $40.91
InvoiceNo: 13401; Total Amount: $15.00
InvoiceNo: 5991; Total Amount: $22.34
0
Comment
Question by:N R
5 Comments
 
LVL 6

Expert Comment

by:bull_rider
ID: 24358128
I would say SUBSTRING as well as CHARINDEX to get the invoice value. CHARINDEX to gethe index of the semicolon and substrng to retrieve all characters from 12th position to the charindex of the semicolon.
0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 24358133
select substring(col_name, 12, charindex(';') - 12 )
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24358138
this should do:
select substring( left(yourfield, charindex(';', yourfield)-1), charindex(' ', yourfield)+1, 100)

Open in new window

0
 
LVL 6

Expert Comment

by:bokist
ID: 24358159
Try this way

select ltrim(rtrim(substring(invoiceNo, charindex(':', invoiceNo) +1, charindex(';', InvoiceNo) -1)))
0
 
LVL 6

Expert Comment

by:bokist
ID: 24358261
Little bit too late, now I learned to refresh allways before posting
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question