question

Gogolo avatar image
Gogolo asked

Remove Decimal

Dears, Can you please help on REMOVING LAST CHAR from 4 decimal value, WITHOUT ROUNDING decimal value?
sql-server-2008-r2decimal
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Håkan Winther avatar image Håkan Winther commented ·
can you give us a sample of the source data and how you want the target data to look like?
0 Likes 0 ·
Kev Riley avatar image
Kev Riley answered
It would be easier to give you the right answer if we had an example to work with, but the truncate function of the round function may give you what you need: declare @YourDecimal decimal(8,4) set @YourDecimal = 1.1234 select cast(round(@YourDecimal,3,1) as decimal(8,3)) The third argument of the ROUND function allows you to specify that the rounding should be done by truncation rather than 'normal' rounding, so you can use this to truncate the value to a decimal with 3 decimal places, and then re-cast that, otherwise the ROUND function returns the same scale as the value passed in.
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Gogolo avatar image Gogolo commented ·
Kev, Great solution..just without last digit, while I have all records with 4 decimals, I got a simple solution, LEFT(@Field,CHARINDEX('.',@Field)+3) Thank you very much for your effort.
0 Likes 0 ·
Fatherjack avatar image
Fatherjack answered
The method depends on the source data type. Some will round others will truncate. You seems to be looking for a method that truncates. CAST and CONVERT BOL - http://msdn.microsoft.com/en-us/library/ms187928 -- This is an example where conversion to an INT from a Numeric truncates the values DECLARE @Values TABLE ( Val DECIMAL(8, 4) ) INSERT INTO @Values ( Val ) SELECT TOP 2000 ( ABS(CHECKSUM(NEWID())) % 5000.0 + 1 ) + ( ABS(CHECKSUM(NEWID())) % 2000.0 + 1 ) * .0003 AS [value] FROM [sys].[syscolumns] AS s CROSS JOIN [sys].[syscolumns] AS s2 SELECT val , CONVERT(INT, val) AS [converted] FROM @Values AS v
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.