sivaprakash.s
sivaprakash.s

Reputation: 35

need split the left side '/' charcter in sql server

I have a query in SQL 2014

SELECT [orde_reference],
SUBSTRING([orde_reference], 
CHARINDEX('/', [orde_reference]) + 1, 
LEN([orde_reference])) AS batch FROM   Orders 

That returns the following

input is ,86156161566156/454

Result need

86156161566156 only

Thanks

Upvotes: 1

Views: 66

Answers (3)

Chan Guan Yu
Chan Guan Yu

Reputation: 145

You can use this query:

SELECT LEFT(orde_reference, CHARINDEX('/', orde_reference) - 1) AS batch
FROM Orders;

Here is a link for different string manipulations in SQL Server: https://datatofish.com/left-right-substring-sql-server/

Upvotes: -1

Steve-o169
Steve-o169

Reputation: 2146

If you only want the first part of the string, than you're using the wrong values. The first parameter of substring is the string that you want to transform, the second parameter is the starting position, and the third is the end position.

SELECT [orde_reference],
SUBSTRING([orde_reference], 
0,
CHARINDEX('/', [orde_reference])) AS batch FROM   Orders 

This should do it.

Upvotes: 0

John Cappelletti
John Cappelletti

Reputation: 81970

Perhaps left() would be a cleaner option

Select left([orde_reference],charindex('/',[orde_reference]+'/')-1)

Upvotes: 4

Related Questions