-1

I have 2 pieces of data

     Column1
DY04837293530000LFOY
DY9586074510000LOY

I am looking for this result

 Column1           Column2
DY04837293         530000
DY95860745         10000

I need to take the first 10 characters and put them in the first column and then remove the characters on the end and just add the remaining numbers in the middle to column 2.

Can anyone give me any advice on the best way to approach this? thanks

Ryan Gadsdon
  • 2,272
  • 4
  • 31
  • 56

1 Answers1

3
DECLARE @test VARCHAR(255) = 'DY04837293530000LFOY'

SELECT  @test ori
,       SUBSTRING(@test, 1, 10) first_10
,       SUBSTRING(@test, 11, PATINDEX('%[^0-9]%', SUBSTRING(@test, 11, LEN(@test))) - 1) last_numbers

Results in:

+----------------------+------------+--------------+
|         ori          |  first_10  | last_numbers |
+----------------------+------------+--------------+
| DY04837293530000LFOY | DY04837293 |       530000 |
+----------------------+------------+--------------+
HoneyBadger
  • 14,750
  • 3
  • 34
  • 48