Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Jerremy09
Starting Member
4 Posts |
Posted - 2011-11-07 : 01:27:21
|
| Hi,I would need to take time out from datetime2. ExampleTable:ID Datetime Time1 2011-11-5 20:34:54 2 2011-11-4 21:21:393 2011-11-6 10:09:084 2011-11-6 23:51:07Now I want to take time out from column Datetime and set it in column Time. Table should look like this:Table:ID Datetime Time1 2011-11-5 21:34:54 21:34:542 2011-11-4 15:21:39 15:21:393 2011-11-6 10:09:08 10:09:084 2011-11-6 23:51:07 23:51:07Now if I create ordinary Select statement with "Order by Time", table should be ordered this way:Table:ID Datetime Time3 2011-11-6 10:09:08 10:09:082 2011-11-4 15:21:39 15:21:391 2011-11-5 21:34:54 21:34:544 2011-11-6 23:51:07 23:51:07There is no point of order table by DAtetime because it will order table according to DAY. I need to order table just according to time of the day. Coul you please help me. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-11-07 : 01:58:05
|
you can use convert(time, datetime_col) to convert to time.Depending on your requirement, if it is just for ordering the records during retrieving, you canselect *from yourtableorder by convert(time, datetime_col) or if you wish to have a column in the table, you can use computed columnalter table <table name> add time_col as convert(time, datetime_col) KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Lewie
Starting Member
42 Posts |
Posted - 2011-11-07 : 02:04:38
|
| declare @t1 table(abc char(1), T2 datetime2)insert into @t1select 'A',getdate()insert into @t1select 'B',getdate() + 1insert into @t1select 'c',getdate() - 1insert into @t1select 'D',getdate() + 2insert into @t1select 'E',getdate() - 2select *from @t1order by CAST(T2 as time(7)) |
 |
|
|
Jerremy09
Starting Member
4 Posts |
Posted - 2011-11-07 : 13:54:44
|
| Hi, thank you for replyes. I made it by "convert(time, datetime_col)" |
 |
|
|
|
|
|
|
|