Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Concat Help

Posted on 2011-09-29
7
Medium Priority
?
340 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
  • 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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

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!

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
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…

916 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