[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Parse a substring of characters to the left of a period

Posted on 2004-10-29
7
Medium Priority
?
367 Views
Last Modified: 2006-11-17
I am trying parse a substring of characters to the left of a period. I need to perfom a select statement that will parse a substring of data to the left of a period. The sql statement is:
SELECT     dbo.OrderHeader.PrimaryCustomerID, dbo.OrderHeader.OrderNumber, dbo.OrderLine.EventDateToDeliver, dbo.LineDetail.Weight,
                      dbo.OrderHeader.Remarks
FROM         dbo.OrderHeader INNER JOIN
                      dbo.OrderLine ON dbo.OrderHeader.ID = dbo.OrderLine.OrderHeaderID INNER JOIN
                      dbo.LineDetail ON dbo.OrderLine.ID = dbo.LineDetail.OrderLineID
where dbo.LineDetail.PO = '929477'

The output is:
PrimaryCustomerID    OrderNumber                     EventDateToDeliver             Weight       Remarks
102                          6061 - EXP 2.1014074546      2004-10-19 21:00:00.000          96.0        NULL
102                          6061 - EXP 2.1014074546      2004-10-19 21:00:00.000          148.0       NULL

The field that I need to parse is OrderNumber. The only thing that is constant is the period.
0
Comment
Question by:infutech
[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
  • Learn & ask questions
7 Comments
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 12448069
For the chars before the '.':

SELECT ..., LEFT(OrderNumber, CHARINDEX('.', OrderNumber) - 1)


For the chars after the '.':

SELECT ..., SUBSTRING(OrderNumber, CHARINDEX('.', OrderNumber) + 1, LEN(OrderNumber))


0
 
LVL 13

Expert Comment

by:MikeWalsh
ID: 12448084
When you say parse, do you mean parse or do you mean crop? Are you trying to only return what is to the left of the period or what is to the right of the period?

show an example of a row you would like returned.
0
 
LVL 8

Expert Comment

by:sigmacon
ID: 12448089
this also makes sure that it works if there is not . - if that can never happen, just do:


(
        case when charindex('.', dbo.OrderHeader.OrderNumber) > 0
                then Left(dbo.OrderHeader.OrderNumber, charindex('.', dbo.OrderHeader.OrderNumber) - 1)
                else null
          end
    ) as ORDERBNUMBERLEFT


0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
LVL 13

Expert Comment

by:MikeWalsh
ID: 12448092
or nevermind, I could have given you both like scott did :)
0
 
LVL 12

Expert Comment

by:pique_tech
ID: 12448099
What part of the OrderNumber do you need to keep or process?  What you're asking isn't difficult, but can't be specifically addressed without better understanding what you need to parse and process.
0
 
LVL 8

Expert Comment

by:sigmacon
ID: 12448100
or this if you want the order number when there is no period

(
        case when charindex('.', dbo.OrderHeader.OrderNumber) > 0
                then Left(dbo.OrderHeader.OrderNumber, charindex('.', dbo.OrderHeader.OrderNumber) - 1)
                else dbo.OrderHeader.OrderNumber
          end
    ) as ORDERBNUMBERLEFT

0
 

Author Comment

by:infutech
ID: 12448167
In my example above the Ordernumber is:  6061 - EXP 2.1014074546  I only need: 6061 - EXP 2  Retruned to me.
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

656 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