## Remove decimal places and force leading zeros

 Author Message fergusoj SSC Veteran Group: General Forum Members Points: 221 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 SSCarpal Tunnel Group: General Forum Members Points: 4640 Visits: 1993 SELECT RIGHT('000'+CAST(CAST(MyField AS INT) AS VARCHAR(4)),4) should do the trick _____________________________________________________________________MCSA SQL Server 2012 fergusoj SSC Veteran Group: General Forum Members Points: 221 Visits: 74 That worked perfectly. Thank you! dsekely-1052784 Valued Member Group: General Forum Members Points: 50 Visits: 15 thanks for the info Eugene Elutin SSC-Dedicated Group: General Forum Members Points: 31766 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 SSC-Enthusiastic Group: General Forum Members Points: 127 Visits: 6 What happens if you have digits to right of decimal?declare @value3 integer@value3 = 197.81SELECT RIGHT('000'+CAST(CAST(@value3 AS INT) AS VARCHAR(7)),7) gives value 000197any help on this would be appreciated. GSquared SSC Guru Group: General Forum Members Points: 138483 Visits: 9731 stimetb (10/17/2012)What happens if you have digits to right of decimal?declare @value3 integer@value3 = 197.81SELECT RIGHT('000'+CAST(CAST(@value3 AS INT) AS VARCHAR(7)),7) gives value 000197any 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, ETCProperty of The Thread"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon stimetb SSC-Enthusiastic Group: General Forum Members Points: 127 Visits: 6 just realized that - i switched it over to money and am working with itthanks dwain.c SSC-Forever Group: General Forum Members Points: 43925 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 SSC-Enthusiastic Group: General Forum Members Points: 127 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 cardi 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.