SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 display last 18 numbers of instant numbers
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

djamit
Starting Member

16 Posts

Posted - 02/21/2014 :  06:34:05  Show Profile  Reply with Quote
Hi SQL team,

I have a column where instant numbers are imported. The numbers are like
1.2.840.10008.5.1.4.1.1.481.5.3020140221.75119.101

I only need the last 18 numbers (3020140221.75119.101) in the column.

How can I solve this?

Thanks,

Kind regards,


stepson
Constraint Violating Yak Guru

Romania
403 Posts

Posted - 02/21/2014 :  06:58:43  Show Profile  Reply with Quote
the pattern is the same , always?


declare @vcNumber as varchar(500)
		,@iNoPoints as int
		,@iNoChars as int

set @vcNumber='1.2.840.10008.5.1.4.1.1.481.5.3020140221.75119.101'
set @iNoPoints=2
set @iNoChars=18

select right(@vcNumber, @INoChars + @iNoPoints)






sabinWeb MCP
Go to Top of Page

djamit
Starting Member

16 Posts

Posted - 02/21/2014 :  07:03:50  Show Profile  Reply with Quote
Hello Stepson,

Yes the pattern is the same always but the numbers are differnet for each record.
Go to Top of Page

stepson
Constraint Violating Yak Guru

Romania
403 Posts

Posted - 02/21/2014 :  08:14:13  Show Profile  Reply with Quote



declare @vcNumber as varchar(500)
		,@iNoPoints as int
		,@iNoChars as int
		,@xml as xml

set @vcNumber='1.2.840.10008.5.1.4.1.1.481.5.3020140221.75119.101'
set @iNoPoints=2
set @iNoChars=18


select @xml='<Numbers><numb>' +
replace(@vcNumber,'.','</numb><numb>') + '</numb></Numbers>'


select 
	t.u.value('numb[12]','varchar(50)') + '.' + t.u.value('numb[13]','varchar(50)') + '.' + t.u.value('numb[14]','varchar(50)')
from @xml.nodes('Numbers')t(u)




sabinWeb MCP
Go to Top of Page

stepson
Constraint Violating Yak Guru

Romania
403 Posts

Posted - 02/21/2014 :  08:29:18  Show Profile  Reply with Quote


;with aCTE
AS (
		select 1 [ID],'1.2.840.10008.5.1.4.1.1.481.5.3020140221.75119.101' as InitCol union all
		select 2,'3.2.840.10308.5.1.4.1.1.481.5.33333020140221.3333375119.103'
)

,bCTE
AS
	(select ID, CAST('<Numbers><numb>' +replace(InitCol,'.','</numb><numb>') + '</numb></Numbers>' as XML) as FinalCol
		from aCTE )


select 
		t.u.value('numb[12]','varchar(50)') + '.' + t.u.value('numb[13]','varchar(50)') + '.' + t.u.value('numb[14]','varchar(50)')
from bCTE b
	CROSS APPLY
	b.FinalCol.nodes('Numbers') as t(u)




S


sabinWeb MCP
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000