Avatar of johnkainn
johnkainn

asked on 

hour:min from datetime

Hi,
I would like to select all from a DateTime column where hour is 14 and minutes 10, i.e. 14:10.
It does not matter which date it is. How is best to do this?
Microsoft SQL Server 2008

Avatar of undefined
Last Comment
dr_linux
Avatar of Lee
Lee
Flag of United Kingdom of Great Britain and Northern Ireland image

You where clause should be:

where left(convert(nvarchar(8), myDateTimeField, 108), 2) = '14'
Avatar of celazkon
celazkon
Flag of Czechia image

Don't know if it's exactly the best, but it should work:

SELECT datetime FROM yourtablename WHERE datetime LIKE '%14:10%'
ASKER CERTIFIED SOLUTION
Avatar of Lee
Lee
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of dr_linux
dr_linux
Flag of United States of America image

You really should use DATEPART function to do it right.
http://msdn.microsoft.com/en-us/library/ms174420.aspx
Avatar of dr_linux
dr_linux
Flag of United States of America image

For example
select * from <tablename> where DATEPART(minute, columnname) = 12
Avatar of dr_linux
dr_linux
Flag of United States of America image

Or for your example
select * from ABook where DATEPART(hour, DateReceived) = 14 AND DATEPART(minute, DateReceived) = 10
Avatar of dr_linux
dr_linux
Flag of United States of America image

Obviously substitute 'ABook' for your table name and 'DateReceived' for your column name in the example above.
Microsoft SQL Server 2008
Microsoft SQL Server 2008

Microsoft SQL Server 2008 is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning. Major improvements include the Always On technologies and support for unstructured data types.

50K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo