Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET SQL-Server
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 8:02am
sunpop772
Rate this: bad
good
Please Sign up or sign in to vote.

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Dim paramval As String = "1023 - Hydrabad"
        Dim paramvalue() As String = paramval.Split("-")
       @codeName = paramvalue(1).ToString()
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

string str = "1023 - Hydrabad";
string[] strArray = str.Split('-');
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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

[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,769
1 Sergey Alexandrovich Kryukov 6,338
2 DamithSL 5,408
3 Manas Bhardwaj 4,816
4 Maciej Los 4,195


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 29 May 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100