Hi. I have a field in the format yyyy-mm-dd hh:mm e.g. 2018-01-01 00:00. How do I query the time element of this field?
e.g. select [object] where [answer] = '00:05'
thanks
SQL Server 2016
Hi. I have a field in the format yyyy-mm-dd hh:mm e.g. 2018-01-01 00:00. How do I query the time element of this field?
e.g. select [object] where [answer] = '00:05'
thanks
SQL Server 2016
If fieldtype is datetime:
select *
from yourtable
where hour(yourfield)*100+minute(yourfield)=0005
;
select ... from ... where cast([answer] as time) = '00:05'
Is there a Sar-gable solution
CREATE TABLE #t
( Dt datetime
, MinuteDt AS datepart(MINUTE, Dt) PERSISTED
, PRIMARY KEY(MinuteDt, Dt)
);
GO
INSERT #t ( Dt)
SELECT Cast(DateAdd(mi, number, GetDate()) AS datetime)
FROM master.dbo.spt_values sv
WHERE sv.type='P'
AND sv.number BETWEEN 0 AND 1000;
SELECT t.Dt
, t.MinuteDt
FROM #t t
ORDER BY t.Dt;
SELECT t.Dt
, t.MinuteDt
FROM #t t
WHERE t.MinuteDt = 5
ORDER BY t.Dt;
DROP TABLE #t;
GO