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
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 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
mickyT
mickyT
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2876 Visits: 3318
Hi Maarten

Not sure if this is what you want, but you could try

;with testdata as(
select *
from (values ('=KET+N.207-13-F4001'),('=KET+DG014-13-F4011'),('=KET+RE002-36-MV009')) as MD (value)
)
select value
,case
when substring(value,16,1) like '[0-9]' then
substring(value,15,2)
else
substring(value,15,1)
end as groupno
from testdata


Avdecha
Avdecha
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 18
Hi MickyT,

Thanks for your help.
This is what I needed, and I got there with your reply.


CASE WHEN substring(xlCode, 16, 1) LIKE '[0-9]' THEN substring(xlCode, 15, 1) ELSE substring
(xlCode, 15, 2) END AS GroupNo
, CASE WHEN substring(xlCode, 16, 1) LIKE '[0-9]' THEN substring(xlCode, 16, 4) ELSE substring
(xlCode, 17, 3) END AS SeqNo
mickyT
mickyT
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2876 Visits: 3318
Glad to help
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