如题所述
å°æç§æ°æ®ç±»åç表达å¼æ¾å¼è½¬æ¢ä¸ºå¦ä¸ç§æ°æ®ç±»åãCAST å CONVERT æä¾ç¸ä¼¼çåè½ã è¯æ³
ä½¿ç¨ CASTï¼ CAST ( expression AS data_type ) ä½¿ç¨ CONVERTï¼ CONVERT (data_type[(length)], expression [, style]) åæ°
expression æ¯ä»»ä½ææç Microsoft SQL Server" 表达å¼ãæå ³æ´å¤ä¿¡æ¯ï¼è¯·åè§è¡¨è¾¾å¼ã data_type ç®æ ç³»ç»ææä¾çæ°æ®ç±»åï¼å æ¬ bigint å sql_variantãä¸è½ä½¿ç¨ç¨æ·å®ä¹çæ°æ®ç±»åãæå ³å¯ç¨çæ°æ®ç±»åçæ´å¤ä¿¡æ¯ï¼è¯·åè§æ°æ®ç±»åã length ncharãnvarcharãcharãvarcharãbinary æ varbinary æ°æ®ç±»åçå¯éåæ°ã style æ¥ææ ¼å¼æ ·å¼ï¼åä»¥å° datetime æ smalldatetime æ°æ®è½¬æ¢ä¸ºå符æ°æ®ï¼ncharãnvarcharãcharãvarcharãnchar æ nvarchar æ°æ®ç±»åï¼ï¼æè åç¬¦ä¸²æ ¼å¼æ ·å¼ï¼åä»¥å° floatãrealãmoney æ smallmoney æ°æ®è½¬æ¢ä¸ºå符æ°æ®ï¼ncharãnvarcharãcharãvarcharãnchar æ nvarchar æ°æ®ç±»åï¼ã
å°æç§æ°æ®ç±»åç表达å¼æ¾å¼è½¬æ¢ä¸ºå¦ä¸ç§æ°æ®ç±»åãæå ³å¯ç¨çæ°æ®ç±»åçæ´å¤ä¿¡æ¯ï¼è¯·åè§æ°æ®ç±»åãæ¥ææ ¼å¼æ ·å¼ï¼åä»¥å° datetime æ smalldatetime æ°æ®è½¬æ¢ä¸ºå符æ°æ®ï¼ncharãnvarcharãcharãvarcharãnchar æ nvarchar æ°æ®ç±»åï¼ï¼æè åç¬¦ä¸²æ ¼å¼æ ·å¼ï¼åä»¥å° floatãrealãmoney æ smallmoney æ°æ®è½¬æ¢ä¸ºå符æ°æ®ï¼ncharãnvarcharãcharãvarcharãnchar æ nvarchar æ°æ®ç±»åï¼ãå½è½¬æ¢ä¸ºå符æ°æ®æ¶è¾åºã
éæ§è½¬æ¢å¯¹äºç¨æ·æ¯ä¸å¯è§çã
SQL Server èªå¨å°æ°æ®ä»ä¸ç§æ°æ®ç±»å转æ¢æå¦ä¸ç§æ°æ®ç±»åãä¾å¦ï¼å¦æä¸ä¸ª smallint åéåä¸ä¸ª int åéç¸æ¯è¾ï¼è¿ä¸ª smallint åéå¨æ¯è¾åå³è¢«éæ§è½¬æ¢æ int åéã æ¾å¼è½¬æ¢ä½¿ç¨ CAST æ CONVERT å½æ°ã
CAST å CONVERT å½æ°å°æ°å¼ä»ä¸ä¸ªæ°æ®ç±»åï¼å±é¨åéãåæå ¶å®è¡¨è¾¾å¼ï¼è½¬æ¢å°å¦ä¸ä¸ªæ°æ®ç±»åãä¾å¦ï¼ä¸é¢ç CAST å½æ°å°æ°å¼ $157.27 转æ¢æå符串 ''$157.27''ï¼CAST ( $157.27 AS VARCHAR(10) )
CAST å½æ°åºäº SQL-92 æ å并ä¸ä¼å äº CONVERTã å½ä»ä¸ä¸ª SQL Server 对象çæ°æ®ç±»ååå¦ä¸ä¸ªè½¬æ¢æ¶ï¼ä¸äºéæ§åæ¾å¼æ°æ®ç±»å转æ¢æ¯ä¸æ¯æçãä¾å¦ï¼nchar æ°å¼æ ¹æ¬å°±ä¸è½è¢«è½¬æ¢æ image æ°å¼ãnchar åªè½æ¾å¼å°è½¬æ¢æ binaryï¼éæ§å°è½¬æ¢å° binary æ¯ä¸æ¯æçãnchar å¯ä»¥æ¾å¼å°æè éæ§å°è½¬æ¢æ nvarcharã å½å¤ç sql_variant æ°æ®ç±»åæ¶ï¼SQL Server æ¯æå°å ·æå ¶å®æ°æ®ç±»åç对象éæ§è½¬æ¢æ sql_variant ç±»åãç¶èï¼SQL Server 并ä¸æ¯æä» sql_variant æ°æ®éæ§å°è½¬æ¢å°å ¶å®æ°æ®ç±»åç对象
ä½¿ç¨ CASTï¼ CAST ( expression AS data_type ) ä½¿ç¨ CONVERTï¼ CONVERT (data_type[(length)], expression [, style]) åæ°
expression æ¯ä»»ä½ææç Microsoft SQL Server" 表达å¼ãæå ³æ´å¤ä¿¡æ¯ï¼è¯·åè§è¡¨è¾¾å¼ã data_type ç®æ ç³»ç»ææä¾çæ°æ®ç±»åï¼å æ¬ bigint å sql_variantãä¸è½ä½¿ç¨ç¨æ·å®ä¹çæ°æ®ç±»åãæå ³å¯ç¨çæ°æ®ç±»åçæ´å¤ä¿¡æ¯ï¼è¯·åè§æ°æ®ç±»åã length ncharãnvarcharãcharãvarcharãbinary æ varbinary æ°æ®ç±»åçå¯éåæ°ã style æ¥ææ ¼å¼æ ·å¼ï¼åä»¥å° datetime æ smalldatetime æ°æ®è½¬æ¢ä¸ºå符æ°æ®ï¼ncharãnvarcharãcharãvarcharãnchar æ nvarchar æ°æ®ç±»åï¼ï¼æè åç¬¦ä¸²æ ¼å¼æ ·å¼ï¼åä»¥å° floatãrealãmoney æ smallmoney æ°æ®è½¬æ¢ä¸ºå符æ°æ®ï¼ncharãnvarcharãcharãvarcharãnchar æ nvarchar æ°æ®ç±»åï¼ã
å°æç§æ°æ®ç±»åç表达å¼æ¾å¼è½¬æ¢ä¸ºå¦ä¸ç§æ°æ®ç±»åãæå ³å¯ç¨çæ°æ®ç±»åçæ´å¤ä¿¡æ¯ï¼è¯·åè§æ°æ®ç±»åãæ¥ææ ¼å¼æ ·å¼ï¼åä»¥å° datetime æ smalldatetime æ°æ®è½¬æ¢ä¸ºå符æ°æ®ï¼ncharãnvarcharãcharãvarcharãnchar æ nvarchar æ°æ®ç±»åï¼ï¼æè åç¬¦ä¸²æ ¼å¼æ ·å¼ï¼åä»¥å° floatãrealãmoney æ smallmoney æ°æ®è½¬æ¢ä¸ºå符æ°æ®ï¼ncharãnvarcharãcharãvarcharãnchar æ nvarchar æ°æ®ç±»åï¼ãå½è½¬æ¢ä¸ºå符æ°æ®æ¶è¾åºã
éæ§è½¬æ¢å¯¹äºç¨æ·æ¯ä¸å¯è§çã
SQL Server èªå¨å°æ°æ®ä»ä¸ç§æ°æ®ç±»å转æ¢æå¦ä¸ç§æ°æ®ç±»åãä¾å¦ï¼å¦æä¸ä¸ª smallint åéåä¸ä¸ª int åéç¸æ¯è¾ï¼è¿ä¸ª smallint åéå¨æ¯è¾åå³è¢«éæ§è½¬æ¢æ int åéã æ¾å¼è½¬æ¢ä½¿ç¨ CAST æ CONVERT å½æ°ã
CAST å CONVERT å½æ°å°æ°å¼ä»ä¸ä¸ªæ°æ®ç±»åï¼å±é¨åéãåæå ¶å®è¡¨è¾¾å¼ï¼è½¬æ¢å°å¦ä¸ä¸ªæ°æ®ç±»åãä¾å¦ï¼ä¸é¢ç CAST å½æ°å°æ°å¼ $157.27 转æ¢æå符串 ''$157.27''ï¼CAST ( $157.27 AS VARCHAR(10) )
CAST å½æ°åºäº SQL-92 æ å并ä¸ä¼å äº CONVERTã å½ä»ä¸ä¸ª SQL Server 对象çæ°æ®ç±»ååå¦ä¸ä¸ªè½¬æ¢æ¶ï¼ä¸äºéæ§åæ¾å¼æ°æ®ç±»å转æ¢æ¯ä¸æ¯æçãä¾å¦ï¼nchar æ°å¼æ ¹æ¬å°±ä¸è½è¢«è½¬æ¢æ image æ°å¼ãnchar åªè½æ¾å¼å°è½¬æ¢æ binaryï¼éæ§å°è½¬æ¢å° binary æ¯ä¸æ¯æçãnchar å¯ä»¥æ¾å¼å°æè éæ§å°è½¬æ¢æ nvarcharã å½å¤ç sql_variant æ°æ®ç±»åæ¶ï¼SQL Server æ¯æå°å ·æå ¶å®æ°æ®ç±»åç对象éæ§è½¬æ¢æ sql_variant ç±»åãç¶èï¼SQL Server 并ä¸æ¯æä» sql_variant æ°æ®éæ§å°è½¬æ¢å°å ¶å®æ°æ®ç±»åç对象
温馨提示:答案为网友推荐,仅供参考
第1个回答 2014-11-21
语法不同而已