Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Substring with IF or CASE statement Expand / Collapse
Author
Message
Posted Tuesday, March 19, 2013 9:15 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, February 3, 2014 3:55 AM
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
Post #1432725
Posted Tuesday, March 19, 2013 5:20 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 8:10 AM
Points: 2,156, Visits: 7,270
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
Post #1432942
Posted Wednesday, March 20, 2013 2:42 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, February 3, 2014 3:55 AM
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
Post #1433494
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse