user1858332
user1858332

Reputation: 2075

how to trim Date portion from DateTime Field in sql

i have a field that shows the date and time but i want to show only the time and would like to trim all the Date portion. Here is what i have:

Field1
Dec 30 1899 10:13AM

and i want to show only the time like this:

Field1
10:13AM

I would like to use an update function to accomplish this. How can i do this

UPDATE MyTable
SET Field1=Time...

thanks

Upvotes: 0

Views: 5470

Answers (2)

paparazzo
paparazzo

Reputation: 45096

If you really need to use Update (not format in the select).

As long as Field1 is DatTime DataType is will have a date component.
Cannot remove the date with an update.

If you want a time only column then create a Time column e.g. Time1

time (Transact-SQL)

Then

update mytable set Time1 = CAST(Field1 AS time)

Upvotes: 0

Kermit
Kermit

Reputation: 34063

Perhaps this:

SELECT CAST(GETDATE() AS time)

Nevermind, overlooked AM/PM requirements. Anyway, found the answer:

SELECT ltrim(right(convert(varchar(25), getdate(), 100), 7))

Upvotes: 2

Related Questions