What should be the best way to store a percent value in SQL-Server?

前端 未结 5 1337
野性不改
野性不改 2020-12-13 12:12

I want to store a value that represents a percent in SQL server, what data type should be the prefered one?

5条回答
  •  难免孤独
    2020-12-13 12:29

    decimal(p, s) and numeric(p, s)

    p (precision):

    The maximum total number of decimal digits that will be stored (both to the left and to the right of the decimal point)


    s (scale):

    The number of decimal digits that will be stored to the right of the decimal point (-> s defines the number of decimal places)


    0 <= s <= p.

    • p ... total number of digits
    • s ... number of digits to the right of the decimal point
    • p-s ... number of digits to the left of the decimal point

    Example:

    CREATE TABLE dbo.MyTable
    ( MyDecimalColumn decimal(5,2)
     ,MyNumericColumn numeric(10,5)
    );
    
    INSERT INTO dbo.MyTable VALUES (123, 12345.12);
    
    SELECT MyDecimalColumn, MyNumericColumn FROM dbo.MyTable;
    

    Result:

    MyDecimalColumn: 123.00 (p=5, s=2)
    
    MyNumericColumn: 12345.12000 (p=10, s=5)
    

    link: msdn.microsoft.com

提交回复
热议问题