473,231 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

String manipulation challenge in SQL

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?

Oct 26 '05 #1
4 4806
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

Oct 27 '05 #2
Ugly, but functional! Thanks!

Oct 27 '05 #3
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)
Oct 27 '05 #4
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?

Oct 28 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
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...
4
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...
32
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...
29
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...
53
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;
4
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...
8
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'...
5
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 ...
5
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.