PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > ms.sqlserver.server > Use String Function
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Use String Function

Réponse
 
LinkBack Outils de la discussion
Vieux 08/09/2008, 17h42   #1
tulip
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Use String Function

I would like to retreive the last string from this pattern

,B110,A104,AUSL,
,L101,A101,01,

which means i want to retrieve AUSL and 01

thanks
  Réponse avec citation
Vieux 08/09/2008, 18h22   #2
Rubén Garrigós
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: Use String Function

You can do that playing with charindex,substring and reverse:

DECLARE @string varchar(200)

SET @string = ',B110,A104,AUSL,'
SELECT
REVERSE(SUBSTRING(REVERSE(@string),2,CHARINDEX(',' ,REVERSE(@string),2)-2))

SET @string = ',L101,A101,01,'
SELECT
REVERSE(SUBSTRING(REVERSE(@string),2,CHARINDEX(',' ,REVERSE(@string),2)-2))


Rubén Garrigós
Solid Quality Mentors

"tulip" wrote:

> I would like to retreive the last string from this pattern
>
> ,B110,A104,AUSL,
> ,L101,A101,01,
>
> which means i want to retrieve AUSL and 01
>
> thanks

  Réponse avec citation
Vieux 08/09/2008, 18h31   #3
Plamen Ratchev
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Use String Function

You can use utility table with numbers to parse the delimited list:

CREATE TABLE Foo (
foo_key INT PRIMARY KEY,
foo_data VARCHAR(40));

INSERT INTO Foo VALUES(1, ',B110,A104,AUSL,');
INSERT INTO Foo VALUES(2, ',L101,A101,01,');

SELECT list_value
FROM (
SELECT SUBSTRING(foo_data, n, CHARINDEX(',', foo_data + ',', n) - n) AS
list_value,
n + 1 - LEN(REPLACE(LEFT(foo_data, n), ',', '')) AS list_idx
FROM (SELECT number
FROM master..spt_values
WHERE type = 'P'
AND number BETWEEN 1 AND 250) AS Nums(n)
CROSS JOIN Foo AS F
WHERE SUBSTRING(',' + foo_data, n, 1) = ','
AND n < LEN(foo_data) + 1) AS T
WHERE list_idx = 4;

--
Plamen Ratchev
http://www.SQLStudio.com
  Réponse avec citation
Vieux 08/09/2008, 23h24   #4
tulip
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: Use String Function

so how to retrieve the middle value, e.g. A104 and A101

thanks

"Rubén Garrigós" wrote:

> You can do that playing with charindex,substring and reverse:
>
> DECLARE @string varchar(200)
>
> SET @string = ',B110,A104,AUSL,'
> SELECT
> REVERSE(SUBSTRING(REVERSE(@string),2,CHARINDEX(',' ,REVERSE(@string),2)-2))
>
> SET @string = ',L101,A101,01,'
> SELECT
> REVERSE(SUBSTRING(REVERSE(@string),2,CHARINDEX(',' ,REVERSE(@string),2)-2))
>
>
> Rubén Garrigós
> Solid Quality Mentors
>
> "tulip" wrote:
>
> > I would like to retreive the last string from this pattern
> >
> > ,B110,A104,AUSL,
> > ,L101,A101,01,
> >
> > which means i want to retrieve AUSL and 01
> >
> > thanks

  Réponse avec citation
Vieux 09/09/2008, 01h30   #5
Aaron Bertrand [SQL Server MVP]
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Use String Function

How about using a SPLIT() type of function? For example:



CREATE FUNCTION [dbo].[SplitStrings]
(
@List NVARCHAR(MAX),
@Delimiter CHAR(1) = ','
)
RETURNS @Items TABLE
(
Position INT IDENTITY(1,1) NOT NULL,
Item NVARCHAR(MAX) NOT NULL
)
AS
BEGIN
DECLARE
@Item NVARCHAR(MAX),
@Pos INT;

SET @List = @List + ' ';

WHILE DATALENGTH(@List) > 0
BEGIN
SET @Pos = CHARINDEX(@Delimiter,@List);

IF @Pos = 0
BEGIN
SET @Pos = DATALENGTH(@List)+1;
END

SET @Item = LTRIM(RTRIM(LEFT(@List, @Pos-1)));

INSERT @Items(Item) SELECT @Item;

SET @List = SUBSTRING(@List, @Pos+1, DATALENGTH(@List));
END
RETURN
END
GO

SELECT * FROM dbo.SplitStrings(',B110,A104,AUSL,', ',');
SELECT * FROM dbo.SplitStrings(',B110,A104,AUSL,', ',') WHERE Position = 3;
SELECT * FROM dbo.SplitStrings(',B110,A104,AUSL,', ',') WHERE Position = 4;

SELECT * FROM dbo.SplitStrings(',L101,A101,01,', ',');
SELECT * FROM dbo.SplitStrings(',L101,A101,01,', ',') WHERE Position = 3;
SELECT * FROM dbo.SplitStrings(',L101,A101,01,', ',') WHERE Position = 4;


You could also think about storing the data relationally instead of using
this ugly CSV string to encode different pieces of data inside a single
column... If you are going after individual parts of the string then storing
them separately makes a lot more sense than how you're currently doing it.


> so how to retrieve the middle value, e.g. A104 and A101
>
> thanks
>
> "Rubén Garrigós" wrote:
>
>> You can do that playing with charindex,substring and reverse:
>>
>> DECLARE @string varchar(200)
>>
>> SET @string = ',B110,A104,AUSL,'
>> SELECT
>> REVERSE(SUBSTRING(REVERSE(@string),2,CHARINDEX(',' ,REVERSE(@string),2)-2))
>>
>> SET @string = ',L101,A101,01,'
>> SELECT
>> REVERSE(SUBSTRING(REVERSE(@string),2,CHARINDEX(',' ,REVERSE(@string),2)-2))
>>
>>
>> Rubén Garrigós
>> Solid Quality Mentors
>>
>> "tulip" wrote:
>>
>>> I would like to retreive the last string from this pattern
>>>
>>> ,B110,A104,AUSL,
>>> ,L101,A101,01,
>>>
>>> which means i want to retrieve AUSL and 01
>>>
>>> thanks


  Réponse avec citation
Vieux 09/09/2008, 17h16   #6
tulip
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Use String Function

what does this function suppose return with select statement?
i tried those select but didn't return me the values.

"Aaron Bertrand [SQL Server MVP]" wrote:

> How about using a SPLIT() type of function? For example:
>
>
>
> CREATE FUNCTION [dbo].[SplitStrings]
> (
> @List NVARCHAR(MAX),
> @Delimiter CHAR(1) = ','
> )
> RETURNS @Items TABLE
> (
> Position INT IDENTITY(1,1) NOT NULL,
> Item NVARCHAR(MAX) NOT NULL
> )
> AS
> BEGIN
> DECLARE
> @Item NVARCHAR(MAX),
> @Pos INT;
>
> SET @List = @List + ' ';
>
> WHILE DATALENGTH(@List) > 0
> BEGIN
> SET @Pos = CHARINDEX(@Delimiter,@List);
>
> IF @Pos = 0
> BEGIN
> SET @Pos = DATALENGTH(@List)+1;
> END
>
> SET @Item = LTRIM(RTRIM(LEFT(@List, @Pos-1)));
>
> INSERT @Items(Item) SELECT @Item;
>
> SET @List = SUBSTRING(@List, @Pos+1, DATALENGTH(@List));
> END
> RETURN
> END
> GO
>
> SELECT * FROM dbo.SplitStrings(',B110,A104,AUSL,', ',');
> SELECT * FROM dbo.SplitStrings(',B110,A104,AUSL,', ',') WHERE Position = 3;
> SELECT * FROM dbo.SplitStrings(',B110,A104,AUSL,', ',') WHERE Position = 4;
>
> SELECT * FROM dbo.SplitStrings(',L101,A101,01,', ',');
> SELECT * FROM dbo.SplitStrings(',L101,A101,01,', ',') WHERE Position = 3;
> SELECT * FROM dbo.SplitStrings(',L101,A101,01,', ',') WHERE Position = 4;
>
>
> You could also think about storing the data relationally instead of using
> this ugly CSV string to encode different pieces of data inside a single
> column... If you are going after individual parts of the string then storing
> them separately makes a lot more sense than how you're currently doing it.
>
>
> > so how to retrieve the middle value, e.g. A104 and A101
> >
> > thanks
> >
> > "Rubén Garrigós" wrote:
> >
> >> You can do that playing with charindex,substring and reverse:
> >>
> >> DECLARE @string varchar(200)
> >>
> >> SET @string = ',B110,A104,AUSL,'
> >> SELECT
> >> REVERSE(SUBSTRING(REVERSE(@string),2,CHARINDEX(',' ,REVERSE(@string),2)-2))
> >>
> >> SET @string = ',L101,A101,01,'
> >> SELECT
> >> REVERSE(SUBSTRING(REVERSE(@string),2,CHARINDEX(',' ,REVERSE(@string),2)-2))
> >>
> >>
> >> Rubén Garrigós
> >> Solid Quality Mentors
> >>
> >> "tulip" wrote:
> >>
> >>> I would like to retreive the last string from this pattern
> >>>
> >>> ,B110,A104,AUSL,
> >>> ,L101,A101,01,
> >>>
> >>> which means i want to retrieve AUSL and 01
> >>>
> >>> thanks

>
>

  Réponse avec citation
Vieux 09/09/2008, 17h21   #7
Roy Harvey (SQL Server MVP)
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Use String Function

On Tue, 9 Sep 2008 08:16:01 -0700, tulip
<tulip@discussions.microsoft.com> wrote:

>what does this function suppose return with select statement?
>i tried those select but didn't return me the values.


I ran the code Aaron provided. The results I received:

Position Item
----------- -----------------------------------
1
2 B110
3 A104
4 AUSL
5

Position Item
----------- -----------------------------------
3 A104

Position Item
----------- -----------------------------------
4 AUSL

Position Item
----------- -----------------------------------
1
2 L101
3 A101
4 01
5

Position Item
----------- -----------------------------------
3 A101

Position Item
----------- -----------------------------------
4 01

Roy Harvey
Beacon Falls, CT
  Réponse avec citation
Vieux 09/09/2008, 17h27   #8
Aaron Bertrand [SQL Server MVP]
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Use String Function

> i tried those select but didn't return me the values.

I don't understand, sorry. What exactly did you try, and what did you
receive? Did you create the function first?

  Réponse avec citation
Vieux 19/09/2008, 18h07   #9
tulip
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Use String Function

so if the @list is a field value of another table, how to combine it with the
funtion below?
for example, select stringValue from StringTable, stringValue will be need
to parsed one by one to get the value of position 2, 3, 4.

thanks

"Aaron Bertrand [SQL Server MVP]" wrote:

> How about using a SPLIT() type of function? For example:
>
>
>
> CREATE FUNCTION [dbo].[SplitStrings]
> (
> @List NVARCHAR(MAX),
> @Delimiter CHAR(1) = ','
> )
> RETURNS @Items TABLE
> (
> Position INT IDENTITY(1,1) NOT NULL,
> Item NVARCHAR(MAX) NOT NULL
> )
> AS
> BEGIN
> DECLARE
> @Item NVARCHAR(MAX),
> @Pos INT;
>
> SET @List = @List + ' ';
>
> WHILE DATALENGTH(@List) > 0
> BEGIN
> SET @Pos = CHARINDEX(@Delimiter,@List);
>
> IF @Pos = 0
> BEGIN
> SET @Pos = DATALENGTH(@List)+1;
> END
>
> SET @Item = LTRIM(RTRIM(LEFT(@List, @Pos-1)));
>
> INSERT @Items(Item) SELECT @Item;
>
> SET @List = SUBSTRING(@List, @Pos+1, DATALENGTH(@List));
> END
> RETURN
> END
> GO
>
> SELECT * FROM dbo.SplitStrings(',B110,A104,AUSL,', ',');
> SELECT * FROM dbo.SplitStrings(',B110,A104,AUSL,', ',') WHERE Position = 3;
> SELECT * FROM dbo.SplitStrings(',B110,A104,AUSL,', ',') WHERE Position = 4;
>
> SELECT * FROM dbo.SplitStrings(',L101,A101,01,', ',');
> SELECT * FROM dbo.SplitStrings(',L101,A101,01,', ',') WHERE Position = 3;
> SELECT * FROM dbo.SplitStrings(',L101,A101,01,', ',') WHERE Position = 4;
>
>
> You could also think about storing the data relationally instead of using
> this ugly CSV string to encode different pieces of data inside a single
> column... If you are going after individual parts of the string then storing
> them separately makes a lot more sense than how you're currently doing it.
>
>
> > so how to retrieve the middle value, e.g. A104 and A101
> >
> > thanks
> >
> > "Rubén Garrigós" wrote:
> >
> >> You can do that playing with charindex,substring and reverse:
> >>
> >> DECLARE @string varchar(200)
> >>
> >> SET @string = ',B110,A104,AUSL,'
> >> SELECT
> >> REVERSE(SUBSTRING(REVERSE(@string),2,CHARINDEX(',' ,REVERSE(@string),2)-2))
> >>
> >> SET @string = ',L101,A101,01,'
> >> SELECT
> >> REVERSE(SUBSTRING(REVERSE(@string),2,CHARINDEX(',' ,REVERSE(@string),2)-2))
> >>
> >>
> >> Rubén Garrigós
> >> Solid Quality Mentors
> >>
> >> "tulip" wrote:
> >>
> >>> I would like to retreive the last string from this pattern
> >>>
> >>> ,B110,A104,AUSL,
> >>> ,L101,A101,01,
> >>>
> >>> which means i want to retrieve AUSL and 01
> >>>
> >>> thanks

>
>

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h59.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15960 seconds with 17 queries