简体   繁体   English

尝试在SQL中删除尾随零时的语法错误

[英]Syntax error when trying to remove trailing zeros in SQL

To avoid cosmetic adjustments in the presentation layer, I need to remove trailing zeros from my data outputs - eg in my Hours column I need "2" as opposed to "2.000" or "18.5" as opposed to "18.500". 为了避免在表示层中进行外观调整,我需要从数据输出中删除尾随的零-例如,在“小时”列中,我需要“ 2”(而不是“ 2.000”)或“ 18.5”(而不是“ 18.500”)。

Having reviewed existing resources, I've come up with the following: 回顾了现有资源之后,我提出了以下建议:

SELECT
  DATE_FORMAT(U.START_TIME + INTERVAL 1 hour, '%H:%i') as Start
, DATE_FORMAT(U.END_TIME + INTERVAL 1 hour, '%H:%i') as End
, CAST(CAST(TIMESTAMPDIFF(MINUTE, U.START_TIME, U.END_TIME)/60 as DECIMAL (6,2)) as FLOAT) as Hours
, REQUESTER_COMMENT as Requester_Commment
FROM VW_USER_SCHEDULE_INSTANCES U

I keep getting the error message: 我不断收到错误消息:

Error Code: 1064. You have an error in your SQL syntax; 错误代码:1064。 check the manual that corresponds to your MySQL server version for the right syntax to use near 'FLOAT), REQUESTER_COMMENT as Requester_Comment 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在“ FLOAT”附近使用,REQUESTER_COMMENT作为Requester_Comment

I'm struggling to work out what needs to be adjusted to avoid the error. 我正在努力找出需要调整的内容以避免错误。 Trying to integrate float is preventing any outputs. 试图集成float会阻止任何输出。 Apologies if I'm missing something obvious - I'm a total SQL beginner. 如果我缺少明显的东西,我深表歉意-我是一名SQL初学者。 Any assistance would be much appreciated! 任何帮助将不胜感激!

You should get into the habit of reading the documentation for functions like CAST() . 您应该养成阅读CAST()类的函数的文档的习惯。 See https://dev.mysql.com/doc/refman/5.7/en/cast-functions.html#function_cast 参见https://dev.mysql.com/doc/refman/5.7/en/cast-functions.html#function_cast

If you read the docs, you would learn that CAST() doesn't support FLOAT as a destination type. 如果您阅读这些文档,将会发现CAST()不支持FLOAT作为目标类型。 You can use BINARY, CHAR, DATE, DATETIME, DECIMAL, JSON, NCHAR, SIGNED INTEGER, TIME or UNSIGNED INTEGER. 您可以使用BINARY,CHAR,DATE,DATETIME,DECIMAL,JSON,NCHAR,SIGNED INTEGER,TIME或UNSIGNED INTEGER。 But not FLOAT or DOUBLE. 但不是FLOAT或DOUBLE。

I suggest you try the ROUND() function. 我建议您尝试ROUND()函数。 See https://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_round 参见https://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_round

But my preferred answer is that you should use SQL for fetching raw data, and do formatting for a given presentation in your application code. 但是我的首选答案是您应该使用SQL来获取原始数据,并在应用程序代码中对给定的表示进行格式化。

Just use to_seconds() : 只需使用to_seconds()

SELECT DATE_FORMAT(U.START_TIME + INTERVAL 1 hour, '%H:%i') as Start,
       DATE_FORMAT(U.END_TIME + INTERVAL 1 hour, '%H:%i') as End,
       (TO_SECONDS(U.END_TIME) - TO_SECONDS(U.START_TIME)) / (60*60) as Hours,
       REQUESTER_COMMENT as Requester_Commment
FROM VW_USER_SCHEDULE_INSTANCES U;

You can format it however you like. 您可以随意设置格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM