-1

i have view having a date column that includes both date and time ,i want seperate that columns in to date and time columns

I tried the getdate function its not working , here is my example sql table data

title  description       date
sample  this is sample   2018-11-08 23:59:59.000

i want to have

date           time 
2018-11-08    23:59
vyshnavi
  • 167
  • 1
  • 3
  • 16

2 Answers2

1

You can do :

select cast(date as date), left(cast(date as time), 5) 

You can easily do conversation with simple SELECT statement.

If you want separate date & time, then use ALTER statement to modify :

alter table t
     add [date] as CAST( [date] as date),
     add [time] as CAST( [date] as time(0))
Yogesh Sharma
  • 49,870
  • 5
  • 26
  • 52
0

Since you probably might need to use the date and time together, I'd suggest to create 2 computed columns. e.g.

CREATE TABLE SampleData(
    title       varchar(15),
    description varchar(100),
    [date]      datetime
)
INSERT INTO SampleData
VALUES( 'sample', 'this is sample', '2018-11-08 23:59:59.000')

ALTER TABLE SampleData
    ADD dateonly AS CAST( [date] as date),
        timeonly AS CAST( [date] as time)

SELECT * FROM SampleData;
Luis Cazares
  • 3,495
  • 8
  • 22