SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How do I pad a delimited number?


How do I pad a delimited number?

Author
Message
tschuler-738392
tschuler-738392
SSC Veteran
SSC Veteran (268 reputation)SSC Veteran (268 reputation)SSC Veteran (268 reputation)SSC Veteran (268 reputation)SSC Veteran (268 reputation)SSC Veteran (268 reputation)SSC Veteran (268 reputation)SSC Veteran (268 reputation)

Group: General Forum Members
Points: 268 Visits: 90
I am trying to write a SQL statement to pad numbers with 0's. The number string has a . delimiter and I need to add 0's to the front of each string. The string does not have the same amount of numbers before and after the . delimiter. I need a 7.7 format. Examples below.

Current Code -->> Code needed

0008.22816 -->> 0000008.0022816
069.3142 -->> 0000069.0003142


Can someone assist please?
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)

Group: General Forum Members
Points: 97521 Visits: 38988
Something like this?



with SampleData as ( -- this cte simply provides the sample data for the query
select
strval
from
(values ('0008.22816'),('069.3142'))dt(strval))
select
strval,
right('0000000' + substring(strval,1,charindex('.',strval) - 1), 7) +
'.' +
right('0000000' + substring(strval,charindex('.',strval) + 1,len(strval) - charindex('.',strval)), 7)
from
SampleData;




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
ColdCoffee
ColdCoffee
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9009 Visits: 5555
This?

DECLARE @Tab TABLE
(
Value DECIMAL (14,7)
)

INSERT INTO @Tab (Value)
SELECT 0008.22816 -->> 0000008.0022816
UNION ALL SELECT 069.3142 -->> 0000069.0003142

SELECT T.Value
,OtrApp.ConvVal
,[7.7 Format]= RIGHT(('0000000'+CrsApp.Whole) , 7) + '.' + RIGHT (('0000000'+CrsApp.Frac) , 7)
FROM @Tab T
OUTER APPLY ( SELECT CONVERT(VARCHAR(15),T.Value)) OtrApp (ConvVal)
CROSS APPLY (SELECT LEFT (OtrApp.ConvVal , CHARINDEX('.',OtrApp.ConvVal)-1)
,STUFF(OtrApp.ConvVal,1,CHARINDEX('.',OtrApp.ConvVal),'')
) CrsApp (Whole,Frac)


tschuler-738392
tschuler-738392
SSC Veteran
SSC Veteran (268 reputation)SSC Veteran (268 reputation)SSC Veteran (268 reputation)SSC Veteran (268 reputation)SSC Veteran (268 reputation)SSC Veteran (268 reputation)SSC Veteran (268 reputation)SSC Veteran (268 reputation)

Group: General Forum Members
Points: 268 Visits: 90
Thanks. Worked like a Gem.
SomewhereSomehow
SomewhereSomehow
Old Hand
Old Hand (396 reputation)Old Hand (396 reputation)Old Hand (396 reputation)Old Hand (396 reputation)Old Hand (396 reputation)Old Hand (396 reputation)Old Hand (396 reputation)Old Hand (396 reputation)

Group: General Forum Members
Points: 396 Visits: 469
Here is one more
with t(v) as ( select '0008.22816' union all select '069.3142' )
select
[formattted] =
replace(str(parsename(t.v,2),7),' ','0') + '.' +
replace(str(parsename(t.v,1),7),' ','0'),
*
from t



Also, be aware that all presented solutions will fail or loose data if there will be more than 7 digits.
To avoid it some extra moves should be done.


I am really sorry for my poor gramma. And I hope that value of my answers will outweigh the harm for your eyes.
Blog: http://somewheresomehow.ru
Twitter: @SomewereSomehow
Steven Willis
Steven Willis
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2103 Visits: 1721
This should have no length limitations since the result is a string.


DECLARE
@S VARCHAR(50)
,@Split CHAR(1)
,@X XML
,@strNewValue VARCHAR(50)


SET @S = 0008.22816 -->> 0000008.0022816
SET @S = 069.3142 -->> 0000069.0003142

SET @Split = '.'

--split the two parts into separate elements using the XML reader
SELECT @X = CONVERT(XML,'<root><s>' + REPLACE(@S,@Split,'</s><s>')+'</s></root>')

SELECT
--recombine the elements
@strNewValue = COALESCE(@strNewValue + @Split,'')
+ CAST(Result.PaddedValue AS VARCHAR(MAX))
FROM
(
SELECT
--add the padding
REPLICATE('0',7-LEN(CAST(Value AS INT)))+Value AS PaddedValue
FROM
(
--make some table rows
SELECT T.c.value('.','VARCHAR(MAX)') AS [Value] FROM @X.nodes('/root/s') T (c)
) Element
) Result

SELECT
@strNewValue AS NewValue



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search