Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Concat Help

Posted on 2011-09-29
7
Medium Priority
?
336 Views
Last Modified: 2012-05-12
This one should be easy.....

I need a SQL script that will concat my field Original_Num to only show the first characters before any '.'

For example:

SHP001234.1 should be SHP001234
SHP001234.2 should be SHP001234
SHP001234. should be SHP001234
SELECT  Sales_Doc_Type,
        Sales_Doc_Num,
        Original_Num
FROM    dbo.tspvSalesDocumentHistory

Open in new window

0
Comment
Question by:r270ba
[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
  • 4
  • 2
7 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 36815382
select left(myField, charindex('.', myField)-1)
0
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 2000 total points
ID: 36815394
With you columns and table.

SELECT  Sales_Doc_Type,
        Sales_Doc_Num,
        left(Original_Num, charindex('.', Original_Num)-1)
FROM    dbo.tspvSalesDocumentHistory
0
 

Author Comment

by:r270ba
ID: 36815400
Msg 536, Level 16, State 5, Line 1
Invalid length parameter passed to the SUBSTRING function.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Expert Comment

by:Lee Savidge
ID: 36815415
Does the column contain a null?
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 36815427
SELECT  Sales_Doc_Type,
        Sales_Doc_Num,
        isnulll(left(Original_Num, charindex('.', Original_Num)-1), '') as Original_Num
FROM    dbo.tspvSalesDocumentHistory
0
 

Author Comment

by:r270ba
ID: 36815444
Actually...not ALL Original_Num will contain the '.'  The below code works...however sometimes the Original_Num will contain just the '.' at the end and sometimes it will contain a '.#' (.1, .2, etc).

Will that always work?
SELECT  Sales_Doc_Type,
        Sales_Doc_Num,
        original_num,
        [new]=CASE WHEN ORIGINAL_num LIKE '%.%' THEN left(Original_Num, charindex('.', Original_Num)-1) ELSE Original_Num end
FROM    dbo.tspvSalesDocumentHistory
WHERE Sales_Doc_Num='i038472'

Open in new window

0
 
LVL 42

Expert Comment

by:dqmq
ID: 36815480
SELECT  Sales_Doc_Type,
        Sales_Doc_Num,
        original_num,
 left(Original_Num+ '.', charindex('.', Original_Num+''.')-1) as NewNum
FROM    dbo.tspvSalesDocumentHistory
WHERE Sales_Doc_Num='i038472'
0

Featured Post

URL rewriting in AWS CloudFront

A quick how-to guide to implement with a Lambda function!

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

722 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