Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get today's Date only and not with the time using sql [duplicate]

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

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:

UPDATE MyTable  
        SET Field1 = GETDATE()
like image 788
moe Avatar asked Nov 12 '12 17:11

moe


2 Answers

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
like image 153
Narsimha Avatar answered Sep 17 '22 11:09

Narsimha


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
like image 25
David Steele Avatar answered Sep 18 '22 11:09

David Steele