12,402,241 members (40,488 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
sunpop872

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
 OriginalGriff 250 Richard Deeming 223 Karthik Bangalore 180 ppolymorphe 145 F-ES Sitecore 135
 OriginalGriff 7,073 ppolymorphe 3,030 Karthik Bangalore 2,917 F-ES Sitecore 2,187 Richard MacCutchan 2,125