2010-07-28 6 views
3

Je suis en train de convertir certains decimals en varchar, mais ils obtiennent arrondi.Pourquoi ce serveur Sql CAST (..) arrondit un Decimal à un VarChar?

Quelqu'un peut-il me dire pourquoi?

declare @UpperLeftLatitude DECIMAL, 
    @UpperLeftLongitude DECIMAL, 
    @BottomRightLatitude DECIMAL, 
    @BottomRightLongitude DECIMAL 

SET @UpperLeftLatitude = 38.663 
SET @UpperLeftLongitude = -122.857 
SET @BottomRightLatitude = 37.795 
SET @BottomRightLongitude = -121.219 


DECLARE @SearchRectangleString VARCHAR(MAX); 
SET @SearchRectangleString = 'POLYGON((' + CONVERT(VARCHAR(50), @UpperLeftLatitude) + ' ' + CAST(@UpperLeftLongitude AS VARCHAR(50)) + ',' 
    + CAST(@BottomRightLatitude AS VARCHAR(50)) + ' ' + CAST(@UpperLeftLongitude AS VARCHAR(50)) + ',' 
    + CAST(@BottomRightLatitude AS VARCHAR(50)) + ' ' + CAST(@BottomRightLongitude AS VARCHAR(50)) + ',' 
    + CAST(@UpperLeftLatitude AS VARCHAR(50)) + ' ' + CAST(@BottomRightLongitude AS VARCHAR(50)) + ',' 
    + CAST(@UpperLeftLatitude AS VARCHAR(50)) + ' ' + CAST(@UpperLeftLongitude AS VARCHAR(50)) + '))'; 

    SELECT @SearchRectangleString 

------------------- 
POLYGON((39 -123,38 -123,38 -121,39 -121,39 -123)) 

(1 row(s) affected) 

NOTE: Oui, je sais que mes Lat/sont le désire ardemment envers. Je vais les changer bientôt.

Répondre

6

C'est parce que vos décimales ne sont pas spécifiées avec une longueur. Ils ne stockent aucune décimale.

les opérations suivantes:

DECLARE @test DECIMAL, @test2 DECIMAL(8,4) 
SET @test = 12.3456 
SET @test2 = 12.3456 

SELECT @test, @test2 
+1

Éloigné-brussel pousse. Je pense qu'après 15 ans de travail, je choisirais ce shiz. Il est temps de rentrer à la maison. Cellule cérébrale unique dans le cerveau très fatigué. Merci mon pote! –

1

Comme mentionné ck s'arrondi ...

Lorsque la fonction est omis ou a une valeur de 0 (par défaut), numeric_expression arrondie. Lorsqu'une valeur autre que 0 est spécifiée, expression_numérique est tronquée.

Source: ROUND (T-SQL)

Questions connexes