Connecting Tech Pros Worldwide Help | Site Map

I want to query of getting hour from date field

Newbie
 
Join Date: Oct 2009
Posts: 1
#1: 4 Weeks Ago
In my table the field of DateTime name is TIN. In this field DATE and TIME are shown for date I pass this query:
Expand|Select|Wrap|Line Numbers
  1. SELECT DAY(TIN) FROM SHIFT WHERE SHID = 1
Then the result is "10".

So the same I want to get Hour fromt his field I pass this query:
Expand|Select|Wrap|Line Numbers
  1. SELECT HOUR(TIN) FROM SHIFT WHERE SHID = 1
But it gives this error:
Quote:
'HOUR' is not a recognized function name.
So please help me and give me the complete query from getting hour.
best answer - posted by nbiswas
Either use DATEPART() or DATENAME()
e.g.

Expand|Select|Wrap|Line Numbers
  1. SELECT DATEPART(HH,GETDATE()) AS [Hour]
  2.  
  3. SELECT DATENAME(HH, GETDATE())AS [Hour]
Refer this articles for more infos

a) DateName( ) http://msdn.microsoft.com/en-us/library/ms174395.aspx

b) DatePart() http://msdn.microsoft.com/en-us/library/ms174420.aspx

Hope this helps
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,078
#2: 4 Weeks Ago

re: I want to query of getting hour from date field


Because HOUR() expects a TIME data type
Expert
 
Join Date: Oct 2008
Location: Cedar City, Utah, USA
Posts: 92
#3: 4 Weeks Ago

re: I want to query of getting hour from date field


If MySQL is anything like MS-SQL, it saves the date/time as a number along the lines of 1345.5467, where the whole number is the day and the fraction is the time of day. Performing a manipulation to pull the whole number off the fraction, then formatting the remaining decimal as a time value will accomplish the same result as the Hour() function.
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,747
#4: 3 Weeks Ago

re: I want to query of getting hour from date field


Based on the fact that HOUR is a valid MySQL function, and that the error message matches that of a MSSQL server (as far as I know), I am going to assume this is in fact a MSSQL question posted in the wrong forum.

If that is the case try this:
Expand|Select|Wrap|Line Numbers
  1. SELECT DATEPART('hh',TIN) WHERE SHID = 1
However, It has been years since I used MSSQL, so this could very well be wrong. (Worth a try tho.)

I'll move this over to the MSSQL forum now.
ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,925
#5: 3 Weeks Ago

re: I want to query of getting hour from date field


Use the datepart() function.

Happy Coding!!!


--- CK
nbiswas's Avatar
Member
 
Join Date: May 2009
Location: India
Posts: 33
#6: 2 Weeks Ago

re: I want to query of getting hour from date field


Either use DATEPART() or DATENAME()
e.g.

Expand|Select|Wrap|Line Numbers
  1. SELECT DATEPART(HH,GETDATE()) AS [Hour]
  2.  
  3. SELECT DATENAME(HH, GETDATE())AS [Hour]
Refer this articles for more infos

a) DateName( ) http://msdn.microsoft.com/en-us/library/ms174395.aspx

b) DatePart() http://msdn.microsoft.com/en-us/library/ms174420.aspx

Hope this helps
Reply


Similar Microsoft SQL Server bytes