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 help

Author  Topic 

sqlfresher2k7
Aged Yak Warrior

623 Posts

Posted - 2012-05-22 : 15:39:51
[code]
I need a query to add 4 zeros before peroid for the field values.
Ex:
2.16.840.1.114222
2.16.840.1.114222.65.61


Exepcted output

00002.000016.0000840.00001.0000114222
00002.000016.0000840.00001.0000114222.000065.000061

Thanks for your help in advance.

[/code]





robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-05-22 : 15:47:45
SELECT '0000' + REPLACE(myColumn,'.','.0000') FROM myTable
Go to Top of Page

boosts
Starting Member

9 Posts

Posted - 2012-05-22 : 15:49:48
quote:
Originally posted by sqlfresher2k7


I need a query to add 4 zeros before peroid for the field values.
Ex:
2.16.840.1.114222
2.16.840.1.114222.65.61


Exepcted output

00002.000016.0000840.00001.0000114222
00002.000016.0000840.00001.0000114222.000065.000061





Thanks for your help in advance.



SELECT REPLACE( '0000' + '2.16.840.1.114222' , '.', '.0000')







Go to Top of Page

sqlfresher2k7
Aged Yak Warrior

623 Posts

Posted - 2012-05-22 : 16:31:38
Thanks Boost,Robvolk..
It works...

How to modify the query to get the below output.

2.16.840.1.114222
2.16.840.1.114222.65.61

Lenght (5)

Exepcted output

00002.00016.00840.00001.114222
00002.00016.00840.00001.114222.00065.00061

Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-05-22 : 17:10:53
You'll need to parse out the numbers, pad them with zeros, then reassemble them. Frankly it's not worth it, they should be stored as separate rows if you need to manipulate them this way.

If you're still going to parse, there are articles here on techniques you can use:

http://www.sqlservercentral.com/articles/Tally+Table/72993/
http://www.sqlteam.com/article/parsing-csv-values-into-multiple-rows
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2012-05-22 : 17:20:33
definitely not worth it unless you are doing some sort of puzzle. as robvolk says you will parsing all day long and it will eventually bite you good

tear down and rebuild.

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')

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

--, '0000' + REPLACE(value,'.','.0000')
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


;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
   

- Advertisement -