Monday, February 27, 2012

How can i get time?

There is a field which type is datetime in my table, when the filed is '2006-06-03 23:00:00', how can i get '2006-06-03' from the field?

TRY THIS

SELECT CONVERT(VARCHAR(10), '2006-06-03 23:00:00')

REGARDS

ANAS

|||

This only works if the field is a varchar. You would need to set a different style or you will get "Jun 3 200". See below...

IF OBJECT_ID('test', 'U') IS NOT NULL

DROP TABLE dbo.test

GO

CREATE TABLE dbo.test

(

testDate datetime

)

INSERT dbo.test (testDate) VALUES ('2006-06-03 23:00:00')

--This returns "Jun 3 200"

SELECT CONVERT(VARCHAR(10), testDate) FROM dbo.test

--This returns 2006-06-03

SELECT REPLACE(CONVERT(VARCHAR(10), testDate, 102), '.', '-') FROM dbo.test

No comments:

Post a Comment