Solved

# Substring or Charindex?

Posted on 2009-05-11
517 Views
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
Question by:N R
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 6

Expert Comment

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

ID: 24358133
select substring(col_name, 12, charindex(';') - 12 )
0

LVL 143

Accepted Solution

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

LVL 6

Expert Comment

ID: 24358159
Try this way

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

LVL 6

Expert Comment

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

## Featured Post

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that witâ€¦
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
###### Suggested Courses
Course of the Month4 days, 11 hours left to enroll