简体   繁体   English

SQL 将日期转换为 yyyy-mm-dd

[英]SQL convert date to yyyy-mm-dd

How would you convert a date in the format of:您将如何将日期转换为以下格式:

2012-10-16 07:44:22.000

to this format:到这种格式:

2012-10-16

And no just making the varchar(10) wont work as this question suggests, because I have to compare them and I can't do it with that method.并且不只是使 varchar(10) 不会像这个问题所暗示的那样工作,因为我必须比较它们并且我不能用那种方法来做到这一点。 Also nowhere does it give me the value to do that type of conversion.它也没有给我做这种类型的转换的价值。

This is as close as I've come:这和我来的一样接近:

declare @DATE DATETIME = '2012-10-16 00:00:000'
SELECT  Convert(varchar(20), @DATE,105) 

Result = 16-10-2012结果 = 16-10-2012

declare @DATE DATETIME = '2012-10-16 00:00:000'
SELECT  REPLACE(Convert(varchar(20), @DATE,102), '.', '-')

Here 's an overview of the date formats in SQL Server. 是SQL Server中日期格式的概述。

SELECT CONVERT(VARCHAR(10),@date, 120)
--Gives: yyyy-mm-dd

mysql you can use select CURDATE(); mysql 你可以使用 select CURDATE();

it returns the current date (yyyy-mm-dd)它返回当前日期(yyyy-mm-dd)

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

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