如何在MySql SELECT语句中正确使用CAST函数? [英] How to use the CAST function correctly in a MySql SELECT statement?
问题描述
我正在将一个MSSQL数据库转换为MySQL数据库,我有一个存储过程,使用cast函数从datetime数据类型转换为varchar数据类型。是否这在php / mysql中是重要的,因为php不是强类型的(我不知道是否重要)我真的想保持SP尽可能接近原始的,所以我可以保持相同的预期功能。问题是,我不能得到cast函数在mysql中正常工作。这是一个测试我试过,给我一个错误:
I'm converting an MSSQL DB to MySQL DB and I have a stored procedure that is using a cast function to convert from a datetime datatype to a varchar datatype. Whether or not this matters in php/mysql since php isn't strongly typed (and I dont know if it would matter or not) I really want to keep the SP a close to the orginal as possible so I can maintain the same expected functionality. The problem is that I can't get the cast function to work right in mysql. Here is a test I tried that got me an error:
DELIMITER ;//
DROP PROCEDURE IF EXISTS `test`;//
CREATE PROCEDURE `test`()
BEGIN
SELECT CAST(my_table.DateColumn AS VARCHAR(10)) as TextColumn
FROM my_table;
END;//
我做错了什么?
推荐答案
VARCHAR不是CAST函数的有效类型,但CHAR为。
VARCHAR isn't a valid type for the CAST function, but CHAR is.
SELECT CAST(my_table.DateColumn AS CHAR(10)) as TextColumn FROM my_table;
这篇关于如何在MySql SELECT语句中正确使用CAST函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!