Results 1 to 14 of 14

Thread: SPLIT(value,char,0)

  1. #1
    Join Date
    Jul 2004
    Posts
    106

    SPLIT(value,char,0)

    hello

    if a column value = name (anything)
    i want to get name

    how can do it ?

    SELECT SPLIT( name, '(' , 0) FROM tableNames

    i want to get all what is before the first bracket (
    but sometimes there is no brackets then i want to get all


    thank you

  2. #2
    Join Date
    Sep 2002
    Posts
    5,938
    Which rdbms do you use?

  3. #3
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    If it is sql server

    declare @a varchar(100)
    set @a='name (anything)'
    select left(@a,charindex('(',@a,0)-1)

    Most of the RDBMS/application languages has inbuilt functions like LEFT,Substring, mid, instr.....

  4. #4
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    Extending Mak's solution for the case where there is no (

    declare @a varchar(100)
    set @a='name (anything)'

    select case
    when charindex('(',@a,0) > 0 then left(@a,charindex('(',@a,0)-1)
    else @a
    end

  5. #5
    Join Date
    Jul 2004
    Posts
    106
    but how do you use it ?


    SELECT users.name, users.alias FROM users


    if i want to apply the filter on users.name ?

  6. #6
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    select case
    when charindex('(',name,0) > 0 then left(name,charindex('(',name,0)-1)
    else name end from users

  7. #7
    Join Date
    Jul 2004
    Posts
    106
    SELECT TOP 100 PERCENT CASE WHEN charindex('(', name, 0) > 0 THEN LEFT(name, charindex('(', name, 0) - 1) ELSE name END, alias
    FROM users


    doesn't work

    enterprise manager doesn't accept the syntax

  8. #8
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    Why do you need TOP 100 Percent?.

    SELECT "Name"= CASE WHEN charindex('(', name, 0) > 0 THEN LEFT(name, charindex('(', name, 0) - 1) ELSE name END, alias
    FROM users

  9. #9
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    SELECT TOP 10 PERCENT [Name] = CASE WHEN charindex('(', name, 0) > 0 THEN LEFT(name, charindex('(', name, 0) - 1) ELSE name END
    FROM users

  10. #10
    Join Date
    Jul 2004
    Posts
    106
    Enterprise Manager has added the PERCENT , me not , i need all the rows

    the following table

    Code:
    CREATE TABLE [dbo].[Config] (
    	[id_Config] [int] IDENTITY (1, 1) NOT NULL ,
    	[nom] [varchar] (100) COLLATE French_CI_AS NULL ,
    	[valeur] [varchar] (255) COLLATE French_CI_AS NULL
    ) ON [PRIMARY]
    the column [nom] can store sometimes datas with (.....)
    like >> Paul Dubois (ingenieur)

    but not allways !

    i must clean the [nom] and get >> Paul Dubois
    and remove any space after the Dubois if any (TRIM)

    Code:
    SELECT nom, valeur FROM Config

    I cannot find a way to get my datas with your code, AND Enterprise Manager don't even accepts the syntax if I try to create a View pasting the code

    thank you

  11. #11
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254

  12. #12
    Join Date
    Jul 2004
    Posts
    106
    it works !!!

    that stupid enterprise manager :-)


    thank you

  13. #13
    Join Date
    Sep 2002
    Posts
    5,938
    Ya since em is not designed for that kind of process.

  14. #14
    Join Date
    Jul 2004
    Posts
    106
    I am still having a problem .. i must compare the splitted value with a clean value in another table

    Code:
    SELECT (CASE WHEN charindex('(',Item.IValue,0) > 0 THEN left(Item.IValue,charindex('(',Item.IValue,0)-1) ELSE Item.IValue END ) AS Computer, 
    Etiquettes_1.valeur AS Etiquette
    FROM dbo.Etiquettes Etiquettes_1 RIGHT OUTER JOIN
    dbo.Item Item ON Etiquettes_1.id_Station = Item.IValue;

    but in that line : Etiquettes_1.id_Station = Item.IValue

    I must compare : Etiquettes_1.id_Station = Computer the splitted value

    how can I do it ?


    thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •