April 7, 2011 at 7:25 am
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?
April 7, 2011 at 7:32 am
SELECT RIGHT('000'+CAST(CAST(MyField AS INT) AS VARCHAR(4)),4) should do the trick
April 7, 2011 at 7:37 am
That worked perfectly. Thank you!
August 5, 2011 at 8:06 am
thanks for the info
August 5, 2011 at 8:10 am
SELECT RIGHT('000'+CAST(CAST(MyField * 100.0 AS INT) AS VARCHAR(4)),4)
October 17, 2012 at 11:37 am
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.
October 17, 2012 at 12:02 pm
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
October 17, 2012 at 12:08 pm
just realized that - i switched it over to money and am working with it
thanks
October 17, 2012 at 7:39 pm
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 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?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
October 22, 2012 at 10:23 am
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.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply