|
-
Aug 14th, 2001, 04:42 PM
#1
Thread Starter
New Member
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
-
Aug 14th, 2001, 06:59 PM
#2
Hyperactive Member
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
-
Aug 15th, 2001, 07:05 AM
#3
Fanatic Member
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
-
Aug 15th, 2001, 11:31 AM
#4
Thread Starter
New Member
Chris,
The field is not always going to be 3 comman separated values.
PS, that statement doesn't work.
Thanks,
Chris
-
Aug 15th, 2001, 12:45 PM
#5
Fanatic Member
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
-
Aug 15th, 2001, 02:52 PM
#6
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|