tysss
tysss

Reputation: 73

How to convert a datetime into date without changing the data type in SQL Server?

How to split this TimeStamp column without changing data format?

I have a column TimeStamp which is in datetime format, and I just want to keep the date part (in datetime format).

I tried to use

CONVERT(DATE, "TimeStamp")

it shows only the date part, but the format is nvarchar(10).

SQL Code:

SELECT 
    "TimeStamp",
    CONVERT(DATE, "TimeStamp") AS Date

My expected result:

TimeStamp (datetime)    Date (datatype: datetime)
------------------------------------------------------
2017-03-10 07:30:25     2017-03-10
2017-03-10 07:30:28     2017-03-10
2017-03-10 07:31:30     2017-03-10
2017-03-10 07:31:39     2017-03-10

Upvotes: 2

Views: 39565

Answers (4)

Ant
Ant

Reputation: 11

SELECT CAST(TRANSDATE AS DATE) as TransDate FROM Vendtrans

Upvotes: 1

harsha reddy
harsha reddy

Reputation: 82

SELECT CONVERT(date, getdate());

Upvotes: 0

Ravi Sharma
Ravi Sharma

Reputation: 370

select GETDATE() as TimeStamp,
convert(date, getdate()) as date

enter image description here

Upvotes: 1

Nikunj Satasiya
Nikunj Satasiya

Reputation: 1043

Tysss I have made a demo for you please try this, Here I have used GETDATE() for return current date with a timestamp where you have to pass your column name as you showed in the question which is TimeStamp.

SOLUTION 1

SELECT CAST(CONVERT(VARCHAR,GETDATE(),110) AS DATE) AS DATE

OUTPUT

2019-05-17

SOLUTION 2

SELECT CAST(CONVERT(VARCHAR,GETDATE(),110) AS DATETIME) AS DATE

OUTPUT

2019-05-17 00:00:00.000

I think you need the only date from TimeStamp so, you need to change the datatype DATE else if you will keep your datatype DATETIME then it will return date something like 2019-05-17 00:00:00.000

Upvotes: 5

Related Questions