Results 1 to 6 of 6

Thread: SQL Statement -- Getting the correct substring

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    5

    SQL Statement -- Getting the correct substring

    Hey all.
    I have a question. I have a column ('Value') in a table ('Data'). The column is of type varchar(500). The contents of this table look like this:

    57.24,0.00,1029.23
    400.25,0.00,612.12
    etc.

    Basically: value - comma - value - comma - value

    How do I get the middle value, ie the 0.00 by using a select statement?

    Any help would be greatly appreciated.

    Chris

  2. #2
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    Edinburgh, Scotland
    Posts
    272
    If you needed all of the data then just use:

    SELECT * FROM DATA

    and get the relevent column from the returned recordset:

    rs(0) would be 57.24
    rs(1) would be 0.00

    Of course you could just use the field name to get only the 0.00 field:

    SELECT FIELDNAME FROM DATA

    rs(0) would be 0.00

  3. #3
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    Okay, as long as your field only has three comma seperated values in it and you are using SQL Server, this should work:

    Code:
    Select Left(Substring(Value, Charindex(',', Value) + 2, Len(Value) - Charindex(',', Value) - 2),
    Charindex(',', Substring(Value, Charindex(',', Value) + 2, Len(Value) - Charindex(',', Value) - 2)) - 1)
    From Data
    Hope This Helps,

    Chris
    Last edited by vb_dba; Aug 15th, 2001 at 12:43 PM.
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    5
    Chris,
    The field is not always going to be 3 comman separated values.

    PS, that statement doesn't work.


    Thanks,
    Chris

  5. #5
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    I changed the statement so that it works now.

    How do you determine what the middle number is if there are an odd number of numbers, ie) 1.11, 0.00, 3.33, 2.22? Can you perform this action in a stored procedure?

    Chris
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    5
    Chris,
    My goal is to always get the second value in the comma separated string.

    Anyways, I came to the solution myself.

    select Answer= case when charindex(',', substring(value, charindex(',', value)+1, len(value)))<> 0
    then
    cast (substring(substring(value, charindex(',', value)+1, len(value)),1, charindex(',', substring(value, charindex(',', value)+1, len(value)))-1) as money)
    else
    cast (substring(value, charindex(',', value)+1, len(value)) as money)
    end


    Thanks for your help though!
    Chris

Posting Permissions

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



Click Here to Expand Forum to Full Width