Groups | Blog | Home
all groups > sql server programming > february 2007 >

sql server programming : Extract left part of string, until number.


Razvan Socol
2/12/2007 10:00:37 AM
Try something like this:

SELECT LEFT(x,ISNULL(NULLIF(PATINDEX('%[0-9]%',x),0)-1,LEN(x)))
FROM YourTable

Razvan
Tom Cooper
2/12/2007 1:04:25 PM
Select
Case When PatIndex('%[0-9]%', ProductID) > 0
Then Substring(ProductID, 1, PatIndex('%[0-9]%', ProductID) -1)
Else ProductID End
From MyTable

Tom

[quoted text, click to view]

Aaron Bertrand [SQL Server MVP]
2/12/2007 1:05:21 PM
CREATE TABLE #foo
(
s VARCHAR(32)
);

SET NOCOUNT ON;

INSERT #foo SELECT 'ABC000001'
INSERT #foo SELECT 'ABC000002'
INSERT #foo SELECT 'ABCD000001'
INSERT #foo SELECT 'ABCD000002'
INSERT #foo SELECT 'XYZ5'
INSERT #foo SELECT 'XYZ' -- no numbers
INSERT #foo SELECT '555' -- no letters


SELECT
s,
CASE WHEN f > 0 THEN LEFT (s,f-1)
ELSE s
END
FROM
(
SELECT
s,
f = PATINDEX('%[0-9]%', s)
FROM
#foo
) x;






DROP TABLE #foo;
[quoted text, click to view]

Aaron Bertrand [SQL Server MVP]
2/12/2007 5:51:55 PM
[quoted text, click to view]

Are you going to update the entire table every time any INSERT/UPDATE
statement occurs?

I suggest NOT storing this information, because it can always be derived at
query time. If you need to, create a view that uses my logic and add the
column there, instead of persisting it.

A

Mr. Smith
2/12/2007 6:55:36 PM
Hi.
I have product id's on the form:
ABC000001
ABC000002
ABCD000001
ABCD000002

How can I extract the left "character" part of the string (ABC and ABCD)
since it does not work with left(string,3).

I want this kind of code:
left(string,until find(0-9))

Any hints appreciated

Kind regards
Mr. Smith

Mr. Smith
2/12/2007 8:06:40 PM
Thanks all of you.
A bit more complicated than I woul (ever) find out myself.

Great!

Kind regards
Mr. Smith


[quoted text, click to view]

Aaron Bertrand [SQL Server MVP]
2/12/2007 10:16:48 PM
The other way would be to store ABC and 00001 separately as two varchar
columns, and then have a view that joins them together. You shouldn't have
to persist ABC twice.

A



[quoted text, click to view]

Mr. Smith
2/12/2007 11:46:21 PM
Mr. Bertrand, Aaron
Your code was worked like genious ...

I'm trying to use your code in a SET statement to put the result from your
algorithm into a dedicated field in the same table.

As in:
UPDATE tblproducts SET field_charcode = 'Your code'

If you still "on the line" I'll so much appreciate if you would give me a
hint.

Kind regards, and thanks for sharing your knowledge.
Mr. Smith


[quoted text, click to view]

Mr. Smith
2/13/2007 12:14:15 AM
Ok, A.
What I'm doing is to raise a relational framework based on a "flat"
datastructure.

The character part of the productid, will be the foreign key to make a
relationship to the supplier table, where each supplier is uniqly identified
by a character code.

Product ABC00001 is a product from supplier with the uniqe identifier ABC.

I share your view on putting the logic to work at query time, and I'll guess
I'll manage to build a "virtual" relationship through a view, rather than a
permanent table to table relationship....

Please share your thoughts if you have other suggestion given my scenario
description.

Thanks again.
Mr. Smith



[quoted text, click to view]

AddThis Social Bookmark Button