Solved

SQL Concat Help

Posted on 2011-09-29
7
333 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 500 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦

734 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