SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Substring with IF or CASE statement


Substring with IF or CASE statement

Author
Message
Avdecha
Avdecha
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 18
Hi,

I am trying to import into SQL Server 2008 R2 a list of strings as follows:

=KET+N.207-13-F4001
=KET+DG014-13-F4011
=KET+RE002-36-MV009

I need to split the strings so they are inserted into separate fields. One of the substring statements is:

'SUBSTRING(xlCode; 15; 2) [if the second position is a number, and]
'SUBSTRING(xlCode; 15; 1) [if the second position is anything else]
AS GroupNo

My experience with T-SQL is just not enough to figure this one out. Can you tell me how the statement should be set up?

Thanks in advance for any help.

Maarten
Alan.B
Alan.B
SSCertifiable
SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)

Group: General Forum Members
Points: 5087 Visits: 7697
First, welcome to SSC. To save you some grief - in the future I would suggest posting some DDL so that we can better understand exactly what you are trying to do. See the link at the bottom for more details.

You can go to this article for a copy of the PatternSplit function I am using (and a great read).

Because I am not sure exacly how you want to split your strings up I will give you a couple ideas to get started; see my comments in the code for more details....


IF OBJECT_ID('tempdb..#strings') IS NOT NULL DROP TABLE #strings;
CREATE TABLE #strings (string_id int primary key, string varchar(20));

INSERT INTO #strings
SELECT 1, 'KET+N.207-13-F4001' UNION ALL SELECT 2,'KET+DG014-13-F4011' UNION ALL SELECT 3,'KET+RE002-36-MV009';

SELECT string_id, item
FROM #strings s
CROSS APPLY dbo.PatternSplitCM(string,'%-%')
WHERE Matched=0
GO

--Lets say we want to split by '-' & '+'
SELECT string_id, item
FROM #strings
CROSS APPLY dbo.PatternSplitCM(string,'%[+-]%')
WHERE Matched=0

--Lets say we want to split by '-' & '+' & '.'
SELECT string_id, item
FROM #strings
CROSS APPLY dbo.PatternSplitCM(string,'%[+-.]%')
WHERE Matched=0



http://www.sqlservercentral.com/articles/Best+Practices/61537/

Edit: accidently clicked post before finishing comment ...

-- Alan Burstein



Best practices for getting help on SQLServerCentral
Need to split a string? Try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL 2012+)
Need a pattern-based splitter? Try PatternSplitCM
Need to remove or replace those unwanted characters? Try PatExclude8K and PatReplace8K.

"I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001
Avdecha
Avdecha
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 18
Thanks for your reply, Alan.B.

I posted the same topic under the forum 'SQL Server 2008 - General' and got the answer I was looking for.

Here's the topic link:

http://www.sqlservercentral.com/Forums/Topic1432866-391-1.aspx?Update=1

Thanks for your input.

Maarten
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search