如何获得今天的日期而不是使用sql的时间[重复]

时间:2022-08-25 11:01:08

Possible Duplicate:
Most efficient way in SQL Server to get date from date+time?

可能重复:SQL Server中从日期+时间获取日期的最有效方法是什么?

I want to update a field with today's date only but the problem i am having now is it is giving me the date and the time. I would like to update with specific format like this: 11/09/2012 how can i achieve this? here is my current code:

我想用今天的日期更新一个字段,但我现在遇到的问题是它给了我日期和时间。我想用这样的具体格式更新:11/09/2012我该如何实现这一目标?这是我目前的代码:

UPDATE MyTable  
        SET Field1 = GETDATE()

2 个解决方案

#1


12  

One way is to update and select only date (not time)

一种方法是更新并仅选择日期(而不是时间)

 UPDATE @tab SET dates=(CONVERT(VARCHAR(10),GETDATE(),103));
 SELECT CONVERT(VARCHAR, GETDATE(), 23) FROM @tab

#2


3  

Not much different here apart from slight syntax differences however I would recommend wrapping it up in a function.

除了轻微的语法差异,这里没有太大的不同,但我建议将其包装在一个函数中。

CREATE FUNCTION [dbo].[GetDateOnly] (@Datetime datetime) 

RETURNS datetime  

AS  

BEGIN  
    RETURN CONVERT(Datetime, FLOOR(CONVERT(float,@Datetime)))  
END

#1


12  

One way is to update and select only date (not time)

一种方法是更新并仅选择日期(而不是时间)

 UPDATE @tab SET dates=(CONVERT(VARCHAR(10),GETDATE(),103));
 SELECT CONVERT(VARCHAR, GETDATE(), 23) FROM @tab

#2


3  

Not much different here apart from slight syntax differences however I would recommend wrapping it up in a function.

除了轻微的语法差异,这里没有太大的不同,但我建议将其包装在一个函数中。

CREATE FUNCTION [dbo].[GetDateOnly] (@Datetime datetime) 

RETURNS datetime  

AS  

BEGIN  
    RETURN CONVERT(Datetime, FLOOR(CONVERT(float,@Datetime)))  
END