Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Query

Author  Topic 

sqlfresher2k7
Aged Yak Warrior

623 Posts

Posted - 2012-05-22 : 16:49:47
[code]
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

[/code]

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2012-05-22 : 18:10:13
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
Go to Top of Page

sqlfresher2k7
Aged Yak Warrior

623 Posts

Posted - 2012-05-22 : 19:04:16
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
   

- Advertisement -