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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Extract date and time from datetime field RESOLVED

Author  Topic 

Pinto
Aged Yak Warrior

590 Posts

Posted - 2007-01-11 : 11:07:40
How do I extract the ate and time from a datetime field. The field is called Log_DateTime. I want to extract the date and the time and then use these two expressions to sort/filter on in Query Analyzer.

Pinto
Aged Yak Warrior

590 Posts

Posted - 2007-01-11 : 11:15:24
CONVERT (nvarchar, Log_DateTime, 101)

CONVERT (nvarchar, Log_DateTime, 108)
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-01-11 : 11:18:34
why nvarchar?

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

Pinto
Aged Yak Warrior

590 Posts

Posted - 2007-01-12 : 10:41:57
Don't know - I found the code on the web - it works. What should I use instead ?
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-01-12 : 11:06:15
nvarchar will take double the space than varchar. If you don't have any special requirement to store UNICODE characters, you can use VARCHAR as well.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2007-01-12 : 11:39:45
Finding the Start of Time Periods
One of the most common questions is how to remove the time from a datetime so that you end up with just a date. In other words, change 2006/12/13 02:33:48.347 to 2006/12/13 00:00:00.000. The following links have functions that will find the start of Century, Decade, Year, Quarter, Month, Week, Day, Hour, 30 Minutes, 20 Minutes, 15 Minutes, 10 Minutes , 5 Minutes , x number of Minutes ,Minute , or Second.
Start of Time Period Functions:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=64755


Getting Time Only from DateTime
By convention, a time only column is stored in SQL Server as an offset from 1900-01-01 00:00:00.000. The function on this link will get the time from a datetime value.
Time Only Function: F_TIME_FROM_DATETIME
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=65358


Date/Time Info and Script Links
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=64762





CODO ERGO SUM
Go to Top of Page
   

- Advertisement -