Solved

SQL Concat Help

Posted on 2011-09-29
7
328 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
Comment Utility
select left(myField, charindex('.', myField)-1)
0
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 500 total points
Comment Utility
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
Comment Utility
Msg 536, Level 16, State 5, Line 1
Invalid length parameter passed to the SUBSTRING function.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 25

Expert Comment

by:Lee Savidge
Comment Utility
Does the column contain a null?
0
 
LVL 25

Expert Comment

by:Lee Savidge
Comment Utility
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
Comment Utility
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
Comment Utility
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now