12,291,055 members (61,767 online)
Rate this:
See more:
In SOL Server, I get the value for the parameter, @codeName as "1023 - Hydrabad" from ASP.NET

ie, @codeName = "1023 - Hydrabad"

I want Hydrabad alone. How to split it?
Posted 12-Jul-13 7:02am
sunpop871

Rate this:

## Solution 1

You can use SQL Server's SUBSTRING and CHARINDEX functions:
DECLARE @codeName VarChar(100)
SET @codeName = '1023 - Hydrabad'

SELECT SUBSTRING(@codeName, CHARINDEX('-', @codeName) + 2, 100)
or use appropriate similar .NET methods in your code behind.
Rate this:

## Solution 2

DECLARE @LongSentence VARCHAR(MAX)
DECLARE @FindSubString VARCHAR(MAX)
SET @LongSentence = 'SQL,SERVER,TUTORIAL'
SET @FindSubString = ','
SELECT (LEN(@LongSentence) - LEN(REPLACE(@LongSentence, @FindSubString, ''))) CntReplacedChars,
(LEN(@LongSentence) - LEN(REPLACE(@LongSentence, @FindSubString, '')))/LEN(@FindSubString) CntOccuranceChars

this is working fine for me
Rate this:

## Solution 7

first u create function

ALTER FUNCTION [dbo].[Split]
(
@String NVARCHAR(4000),
@Delimiter NCHAR(1)
)
RETURNS TABLE
AS
RETURN
(
WITH Split(stpos,endpos)
AS(
SELECT 0 AS stpos, CHARINDEX(@Delimiter,@String) AS endpos
UNION ALL
SELECT endpos+1, CHARINDEX(@Delimiter,@String,endpos+1)
FROM Split
WHERE endpos > 0
)
SELECT 'Id' = ROW_NUMBER() OVER (ORDER BY (SELECT 1)),
'Data' = SUBSTRING(@String,stpos,COALESCE(NULLIF(endpos,0),LEN(@String)+1)-stpos)
FROM Split
)

--then u can use this function like this

select * from  dbo.Split(string,'-') where id=1
v3
Rate this:

## Solution 3

Dim paramval As String = "1023 - Hydrabad"
Dim paramvalue() As String = paramval.Split("-")
@codeName = paramvalue(1).ToString()
Rate this:

## Solution 4

string str = "1023 - Hydrabad";
string[] strArray = str.Split('-');
Rate this:

## Solution 5

I think the best solution for such cases is to use Regular Expressions. Check below code project article.

[^]

Top Experts
Last 24hrsThis month
 KARTHIK Bangalore 415 OriginalGriff 280 F-ES Sitecore 165 Mehdi Gholam 144 ppolymorphe 135
 OriginalGriff 8,110 ppolymorphe 4,987 CHill60 4,940 KARTHIK Bangalore 4,347 Sergey Alexandrovich Kryukov 4,233

Web02 | 2.8.160518.1 | Last Updated 29 May 2014