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 2008 Forums
 Transact-SQL (2008)
 Query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sqlfresher2k7
Aged Yak Warrior

583 Posts

Posted - 05/22/2012 :  16:49:47  Show Profile  Reply with Quote

I need a query add leading zero to the value..
Each lenght of value before period should be 5.

Ex:
2.16.840.1.114222
2.16.840.1.114222.65.61

Exepcted output

00002.00016.00840.00001.114222
00002.00016.00840.00001.114222.00065.00061

yosiasz
Flowing Fount of Yak Knowledge

USA
1608 Posts

Posted - 05/22/2012 :  18:10:13  Show Profile  Click to see yosiasz's MSN Messenger address  Reply with Quote
what kind of data is it and can you save it separately. I would not do this if I were you

declare @sqlfresh table(id int identity(1,1), value varchar(max))
INSERT INTO @sqlfresh
values('2.16.840.1.114222')
INSERT INTO @sqlfresh
values('2.16.840.1.114222.65.61')

;with cteJunk(id, policy#, yougi, value)			
AS
(
SELECT sq.id, Tags.val.value('.', 'VARCHAR(MAX)') AS policy#,
       CASE 
          WHEN LEN(Tags.val.value('.', 'VARCHAR(MAX)')) > 4 THen  Tags.val.value('.', 'VARCHAR(MAX)')
          ELSE REPLACE(POWER(10, 5 - LEN(Tags.val.value('.', 'VARCHAR(MAX)'))  ), '1','') + Tags.val.value('.', 'VARCHAR(MAX)')
       END as yougi, value
  FROM(  
		  SELECT id, CAST('<t>' + REPLACE(value, '.', '</t><t>') + '</t>' AS XML) AS TAG  
			FROM @sqlfresh
	  ) TAB 
 CROSS APPLY TAG.nodes('/t') as Tags(val)
 inner join @sqlfresh sq
 on sq.id = TAB.id 
 ) 
 
 
SELECT
   t1.id,
   value was ,
   IsNow = substring((SELECT ( '.' + yougi )
                           FROM cteJunk t2
                           WHERE t1.id = t2.id
                           ORDER BY id
                           FOR XML PATH( '' )
                          ), 2, 1000 
                         )FROM cteJunk t1
GROUP BY id, value


<><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion

Edited by - yosiasz on 05/22/2012 18:10:36
Go to Top of Page

sqlfresher2k7
Aged Yak Warrior

583 Posts

Posted - 05/22/2012 :  19:04:16  Show Profile  Reply with Quote
Thanks..

I am trying to sort the values..since it is a varchar i couldn't able to sort the values..
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.06 seconds. Powered By: Snitz Forums 2000