Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Remove decimal places and force leading zeros Expand / Collapse
Author
Message
Posted Thursday, April 07, 2011 7:25 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, March 26, 2013 12:27 PM
Points: 15, 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?
Post #1089867
Posted Thursday, April 07, 2011 7:32 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 3:23 AM
Points: 1,989, Visits: 1,617
SELECT RIGHT('000'+CAST(CAST(MyField AS INT) AS VARCHAR(4)),4) should do the trick
Post #1089875
Posted Thursday, April 07, 2011 7:37 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, March 26, 2013 12:27 PM
Points: 15, Visits: 74
That worked perfectly. Thank you!
Post #1089878
Posted Friday, August 05, 2011 8:06 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, August 05, 2011 8:06 AM
Points: 4, Visits: 15
thanks for the info
Post #1155070
Posted Friday, August 05, 2011 8:10 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, April 03, 2014 10:10 AM
Points: 2,792, Visits: 4,874
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1155076
Posted Wednesday, October 17, 2012 11:37 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, October 22, 2012 10:17 AM
Points: 3, 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.
Post #1373969
Posted Wednesday, October 17, 2012 12:02 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: 2 days ago @ 10:04 AM
Points: 15,442, Visits: 9,590
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
Post #1373984
Posted Wednesday, October 17, 2012 12:08 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, October 22, 2012 10:17 AM
Points: 3, Visits: 6
just realized that - i switched it over to money and am working with it
thanks
Post #1373986
Posted Wednesday, October 17, 2012 7:39 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 5:39 PM
Points: 3,596, Visits: 5,113
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!
Post #1374106
Posted Monday, October 22, 2012 10:23 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, October 22, 2012 10:17 AM
Points: 3, 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.
Post #1375594
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse