Thursday, June 2, 2016

use CHARINDEX in sql server

//Charindex Used
declare @items varchar(500)
set @items='2150303#75469.00##0.00###66.25####26/05/2016#####25/05/2016######6168'

SELECT SUBSTRING(@items,0, CHARINDEX('#', @items)) as a
, SUBSTRING(@items,CHARINDEX('#', @items)+1,CHARINDEX('##', @items)-CHARINDEX('#', @items)-1) as b
, SUBSTRING(@items,CHARINDEX('##', @items)+2,CHARINDEX('###', @items)-CHARINDEX('##', @items)-2) as c
, SUBSTRING(@items,CHARINDEX('###', @items)+3,CHARINDEX('####', @items)-CHARINDEX('###', @items)-3) as d
, SUBSTRING(@items,CHARINDEX('####', @items)+4,CHARINDEX('#####', @items)-CHARINDEX('####', @items)-4) as e
,SUBSTRING(@items,CHARINDEX('#####', @items)+5,CHARINDEX('######', @items)-CHARINDEX('#####', @items)-5) as f
, SUBSTRING(@items,CHARINDEX('######', @items)+6,len(@items)) as g



===============================================================
O/P := 
    a             b             c            d               e                    f                 g
2150303 |  75469.00 | 0.00 | 66.25 |   26/05/2016  | 25/05/2016    |       6168

No comments: