Juthathip Jangjaras
Juthathip Jangjaras

Reputation: 41

How to write the sql for select only some letters?

I have the data like this

112219/18
112219/1

But I only want

18
1

How can i write the sql code? I use sql server

Upvotes: 0

Views: 297

Answers (4)

En Ngn
En Ngn

Reputation: 29

You can use the above solutions, or use a scalar function like this:

CREATE FUNCTION dbo.YourFunctionName
(
@param nvarchar(max)
)
RETURNS NVARCHAR(50)
AS BEGIN
    DECLARE @char char = N'/'
    IF @Param IS NULL OR NOT @param LIKE '%'+@char+'%' RETURN @param;
    SET @param = SUBSTRING(@param, CHARINDEX(@char, @param)+1, LEN(@param))
    RETURN @param;
END

After that you can use it:

SELECT dbo.YourFunctionName(YourDataColumn) FROM YourTable

Upvotes: 2

Hemang A
Hemang A

Reputation: 1012

Please try this.

SELECT SUBSTRING(colName, CHARINDEX('/', colName) + 1, LEN(colName)) FROM tableName

Upvotes: 2

Faraz Ahmed
Faraz Ahmed

Reputation: 1607

you can extract from this query

select right(yourColumnName, charindex('/', reverse(yourColumnName)) - 1) from yourTable

Upvotes: 0

ScaisEdge
ScaisEdge

Reputation: 133380

If you are using sql-server you could try using right() and charindex()

    select  RIGHT(my_col, LEN(my_col) - CHARINDEX ('/', my_col))
    from my_table 

Upvotes: 0

Related Questions