简体   繁体   English

将 T-SQL 的日期函数转换为 MySQL

[英]Converting T-SQL's date functions to MySQL

I am trying to convert this MS SQL Server code to MySQL but I am not getting the same date I would get in MS SQL Server.我正在尝试将此 MS SQL Server 代码转换为 MySQL,但我得到的日期与在 MS SQL Server 中得到的日期不同。 Does anyone know how to change this code to make it work in MySQL?有谁知道如何更改此代码以使其在 MySQL 中工作? The '2021-08-03 00:00:00' is a timestamp example being used. '2021-08-03 00:00:00' 是正在使用的时间戳示例。

SELECT
    FORMAT( DATEADD( DAY, 0, DATEDIFF( DAY, 0, '2021-08-03 00:00:00')), 'd', 'en-us' ) AS 'day'

在此处输入图片说明

The MYSQL Code so far.到目前为止的 MYSQL 代码。

SELECT FORMAT (adddate(datediff(NOW(), ma.Timestamp), INTERVAL 0 DAY), 'd', 'en-us') AS day,

Here's how to present dates in m/d/y format in MySQL:以下是在 MySQL 中以m/d/y格式显示日期的方法:

CREATE TABLE `dates` (d datetime);

INSERT `dates`(d) VALUES('2021-08-31 03:00'),('2021-11-13 05:40');

SELECT DATE_FORMAT(d, '%c/%e/%Y') AS day FROM `dates`;

Results:结果:

day
----------
8/31/2021
11/13/2021

I knew nothing about this since I can barely spell MySQL, but looking up DATE_FORMAT() took about two minutes.我对此一无所知,因为我几乎不会拼写 MySQL,但是查找DATE_FORMAT()大约需要两分钟。

As an aside, the existing expression you got from your co-workers is both needlessly complex and a real drag on performance due to the FORMAT() function .顺便说一句,由于FORMAT()函数,您从同事那里得到的现有表达式既不必要地复杂,又会真正拖累性能 Try:尝试:

DECLARE @d date = '2021-08-03T00:00:00.000';
SELECT CONVERT(char(10), @d, 101);

If you have a datetime already, that's fine, you can still strip time from it without all the dateadd and datediff noise:如果您已经有一个日期时间,那很好,您仍然可以从中删除时间,而不会产生所有 dateadd 和 datediff 噪音:

DECLARE @d datetime = '2021-08-03T00:00:00.000';
SELECT CONVERT(char(10), @d, 101);

Not that I believe m/d/y is a sensible format in the first place, or that you should go out of your way to remove those leading zeroes...并不是说我首先认为m/d/y是一种合理的格式,或者您应该不遗余力地删除那些前导零......

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

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