I have a sql query in which I need to isolate part of the columm value
and return only that isolated portion. I can only do this within the
select statement, and cannot add a function or anything like that. I
would also like to keep this query within sql (I don't want to do this
in my programming environment)
The string value would normally look like "segment1-segment2-segment3".
I need to isolate segment2, but I have to be able to account for
situations in which either one or both dashes are missing (in which
case returning "" or the whole string is OK. The best I have been able
to do reliably is to get "segment2-segment3".
Anybody want to take a stab? 4 4810
On 26 Oct 2005 16:19:02 -0700, "wheresjim" <wh*******@gmail.com> wrote: I have a sql query in which I need to isolate part of the columm value and return only that isolated portion. I can only do this within the select statement, and cannot add a function or anything like that. I would also like to keep this query within sql (I don't want to do this in my programming environment)
The string value would normally look like "segment1-segment2-segment3". I need to isolate segment2, but I have to be able to account for situations in which either one or both dashes are missing (in which case returning "" or the whole string is OK. The best I have been able to do reliably is to get "segment2-segment3".
Anybody want to take a stab?
Well - it's pretty damn ugly, but the best I can figure given your
restrictions is...
SELECT CASE
WHEN value LIKE '%-%-%'
THEN SUBSTRING(value,
CHARINDEX('-',value)+1,
CHARINDEX('-',value,
CHARINDEX('-',value)+1) -
CHARINDEX('-',value) -
1)
ELSE 'aa-bb-cc'
END
Ugly, but functional! Thanks!
On 26 Oct 2005 16:19:02 -0700, wheresjim wrote: I have a sql query in which I need to isolate part of the columm value and return only that isolated portion. I can only do this within the select statement, and cannot add a function or anything like that. I would also like to keep this query within sql (I don't want to do this in my programming environment)
The string value would normally look like "segment1-segment2-segment3". I need to isolate segment2, but I have to be able to account for situations in which either one or both dashes are missing (in which case returning "" or the whole string is OK. The best I have been able to do reliably is to get "segment2-segment3".
Anybody want to take a stab?
Hi wheresjim,
Here's another way. Now that I wrote it, I think I like Steve's version
better - but since YMMV, I'll post it anyway.
DECLARE @a varchar(40)
SET @a = 'segment1-segment2-segment3'
SELECT REVERSE(SUBSTRING(REVERSE(SUBSTRING(@a,
CHARINDEX('-', @a) + 1,
LEN(@a))),
CHARINDEX('-', REVERSE(@a)) + 1,
LEN(@a)))
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)
Here's another solution, which should work if you are certain that
the period character . is not part of any of the segments:
SELECT
CASE WHEN value NOT LIKE '%-%-%'
THEN ''
ELSE PARSENAME(REPLACE(value,'-','.'),2) END
FROM @t
Steve Kass
Drew University
wheresjim wrote: I have a sql query in which I need to isolate part of the columm value and return only that isolated portion. I can only do this within the select statement, and cannot add a function or anything like that. I would also like to keep this query within sql (I don't want to do this in my programming environment)
The string value would normally look like "segment1-segment2-segment3". I need to isolate segment2, but I have to be able to account for situations in which either one or both dashes are missing (in which case returning "" or the whole string is OK. The best I have been able to do reliably is to get "segment2-segment3".
Anybody want to take a stab? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: David Frank |
last post by:
How can I write a string function that encloses the input string
in quotes "string" ??
below works for the "123 operation but adding " to it
clobbers the "123
main()
{
char...
|
by: Dim |
last post by:
I found that C# has some buggy ways to process string across methods.
I have a class with on global string var and a method where i add / remove
from this string
Consider it a buffer... with some...
|
by: tshad |
last post by:
Can you do a search for more that one string in another string?
Something like:
someString.IndexOf("something1","something2","something3",0)
or would you have to do something like:
if...
|
by: zoro |
last post by:
Hi,
I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party
string handling library that includes some very useful string
functions, in particular I'm interested in BEFORE (return...
|
by: Jeff |
last post by:
In the function below, can size ever be 0 (zero)?
char *clc_strdup(const char * CLC_RESTRICT s)
{
size_t size;
char *p;
clc_assert_not_null(clc_strdup, s);
size = strlen(s) + 1;
| |
by: WaterWalk |
last post by:
Hello, I'm currently learning string manipulation. I'm curious about
what is the favored way for string manipulation in C, expecially when
strings contain non-ASCII characters. For example, if...
|
by: John Salerno |
last post by:
Ok, for those who have gotten as far as level 2 (don't laugh!), I have a
question. I did the translation as such:
import string
alphabet = string.lowercase
code = string.lowercase + 'ab'...
|
by: Niyazi |
last post by:
Hi,
Does anyone knows any good code for string manipulation similar to
RegularExpresion?
I might get a value as string in a different format. Example:
20/02/2006 or 20,02,2006 or ...
|
by: Tawreq |
last post by:
Hi All,
I have quite a unique issue - I would be very grateful if someone
could help out with this challenge....
I have two columns, Column A contains and acronym, and Column B
contains a...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |