Home > front end >  How to get only a date from a datetime column?
How to get only a date from a datetime column?

Time:10-22

I have a report to do, so I need to return a date from a datetime column, but it need to come only with the date from the column.

I am using the Microsoft SQL Server 2014. I've already tried to use CONVERT(name_of_the_column, GETDATE()) but I realised the it only works to return the current datetime from the server.

How can I do that?

CodePudding user response:

Use CONVERT(DATE, <expression>).

create table t (dt datetime);

insert into t (dt) values ('2022-10-20 12:34:56');

select *, CONVERT(DATE, dt) from t;

Result:

dt                       (No column name)
-----------------------  ----------
2022-10-20 12:34:56.000  2022-10-20

See example at db<>fiddle.

CodePudding user response:

I prefer to use CAST instead.

CAST(name_of_the_field AS DATE)

Let me know if that works. :)

  • Related