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


Remove decimal places and force leading zeros


Remove decimal places and force leading zeros

Author
Message
fergusoj
fergusoj
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 Visits: 74
I have output for scores that have 2 decimal places (all zeros). I need to remove the decimal places and return a 4 character number in int format. Example: 65.00 must return as 0065; 108.00 must return as 0108. Can anyone help?
Richard Warr
Richard Warr
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2786 Visits: 1987
SELECT RIGHT('000'+CAST(CAST(MyField AS INT) AS VARCHAR(4)),4) should do the trick

_____________________________________________________________________
MCSA SQL Server 2012
fergusoj
fergusoj
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 Visits: 74
That worked perfectly. Thank you!
dsekely-1052784
dsekely-1052784
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 15
thanks for the info
Eugene Elutin
Eugene Elutin
SSCarpal Tunnel
SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)

Group: General Forum Members
Points: 4964 Visits: 5478
SELECT RIGHT('000'+CAST(CAST(MyField * 100.0 AS INT) AS VARCHAR(4)),4)

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
stimetb
stimetb
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 6
What happens if you have digits to right of decimal?
declare @value3 integer
@value3 = 197.81
SELECT RIGHT('000'+CAST(CAST(@value3 AS INT) AS VARCHAR(7)),7)

gives value 000197

any help on this would be appreciated.
GSquared
GSquared
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23327 Visits: 9730
stimetb (10/17/2012)
What happens if you have digits to right of decimal?
declare @value3 integer
@value3 = 197.81
SELECT RIGHT('000'+CAST(CAST(@value3 AS INT) AS VARCHAR(7)),7)

gives value 000197

any help on this would be appreciated.


Do you want to keep the decimal values? If so, declare the variable as something that will do what you need, then don't cast it to Int inside the string function.

Declaring the variable as Int drops the decimal value before you even begin formatting it.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
stimetb
stimetb
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 6
just realized that - i switched it over to money and am working with it
thanks
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7227 Visits: 6431
Here's another way (just foolin' around):


;WITH MyData AS (
SELECT score=65.00 UNION ALL SELECT 108.00 UNION ALL SELECT 1108.00)
SELECT LEFT(REVERSE(CAST(REVERSE(score) AS DECIMAL(8,4))), 4)
FROM MyData





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
stimetb
stimetb
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 6
Now i have a field that is getting populated from 2 other fields based on which was in not null. We removed decimal point and 0 front filled it.
This is what I'm currently doing:
(CASE
WHEN edm.Percentage IS NULL THEN
(ISNULL(RIGHT ('00000000' + CONVERT (varchar (7), FLOOR (ABS (ROUND(edm.amount,2) * 100.0))),7),0))
else
(ISNULL(RIGHT ('00000000' + CONVERT (varchar (7), FLOOR (ABS (ROUND(edm.Percentage,2) * 100.0))),7),0))
end)

inside a select statement,
We are inputting this data in a card that will split this field up.
first 2 bytes go on 1 card and next 5 bytes go on 2nd card

i tried A SCALAR value.
but the problem i had was it gave me a constant on the field because i assigned it outside of the insert/select statement and wasnt sure how to do it inside the statement.
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