sqlÖгý·¨±£´æСÊýÔõôËãµÄ
sql³ý·¨±£´æСÊýµÄÒªÁ죺ʹÓÃcast¡¢round»òtruncateº¯Êý¡£castº¯ÊýÇ¿ÖÆת»»ÊýÖµÀàÐÍÒÔ±£´æÌض¨¾«¶ÈºÍСÊýλÊý¡£roundº¯ÊýËÄÉáÎåÈ븡µãÊýЧ¹û¡£truncateº¯Êý½Ø¶Ï¸¡µãÊýЧ¹û¡£
SQL Öгý·¨±£´æСÊýµÄÅÌËãÒªÁì
ÔÚ SQL ÖУ¬³ý·¨²Ù×÷·ûÊÇб¸Ü£¨/£©£¬Ëü»á±¬·¢Ò»¸ö¸¡µãÊýЧ¹û¡£Ä¬ÈÏÇéÐÎÏ£¬¸¡µãÊýЧ¹û±£´æСÊýµãºó 15 λÊý×Ö¡£
±£´æСÊýµÄÒªÁì
ΪÁ˱£´æ¸ü¶à»ò¸üÉÙµÄСÊý룬¿ÉÒÔʹÓÃÒÔÏÂÒªÁ죺
CAST º¯Êý£º½«ÆäÓÃÓÚ±»³ýÊý»ò³ýÊýÒÔÇ¿ÖÆת»»Îª¾ßÓÐÌض¨¾«¶ÈºÍ±ÈÀýµÄÊý×ÖÀàÐÍ¡£ÀýÈ磺
SELECT CAST(numerator AS DECIMAL(10, 2)) / CAST(denominator AS DECIMAL(10, 2));
µÇ¼ºó¸´ÖÆ
ROUND º¯Êý£ºÓÃÓÚ¶Ô¸¡µãÊýЧ¹û¾ÙÐÐËÄÉáÎåÈë¡£ÀýÈ磺
SELECT ROUND(numerator / denominator, 2);
µÇ¼ºó¸´ÖÆ
TRUNCATE º¯Êý£ºÓÃÓڽضϸ¡µãÊýЧ¹û¡£ÀýÈ磺
SELECT TRUNCATE(numerator / denominator, 2);
µÇ¼ºó¸´ÖÆ
ʾÀý
˼Á¿ÒÔÏÂÅÌÎÊ£º
SELECT 10 / 3;
µÇ¼ºó¸´ÖÆ
ĬÈÏÇéÐÎÏ£¬¸ÃÅÌÎʻ᷵»Ø¸¡µãÊýЧ¹û 3.3333333333333335¡£
ÈôÊÇÒª±£´æÁ½Î»Ð¡Êý£¬¿ÉÒÔʹÓà CAST º¯Êý£º
SELECT CAST(10 AS DECIMAL(10, 2)) / CAST(3 AS DECIMAL(10, 2));
µÇ¼ºó¸´ÖÆ
¸ÃÅÌÎʻ᷵»Ø 3.33¡£
ÒÔÉϾÍÊÇsqlÖгý·¨±£´æСÊýÔõôËãµÄµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡