udaya726
udaya726

Reputation: 1020

Select values from a date column without consider seconds value in SQL

I have a SQL table which has a DATETIME column(EndTime). From my code I am passing a date and I want to get the date that has an EndTime equal to that date. I used

SELECT MAX([EndTime]) FROM [dbo].[Table]  where [EndTime]=@date

What I want is if I pass 2013-06-14 10:35 to the query it should return all the records in this time. But due to the seconds value in the table it returns only the value which has 2013-06-14 10:35:000. How can I ignore the seconds part in the column?

Upvotes: 2

Views: 204

Answers (2)

DevelopmentIsMyPassion
DevelopmentIsMyPassion

Reputation: 3591

You can also try below to remove seconds

SELECT MAX([EndTime]) 
FROM [dbo].[Table]  
where   DATEadd(ss,-datepart(ss,EndTime),EndTime) = @date

Upvotes: 0

Adriaan Stander
Adriaan Stander

Reputation: 166466

Why not try something like

SELECT MAX([EndTime]) 
FROM [dbo].[Table]  
where [EndTime]>=@date
AND [EndTime]<DATEADD(minute,1,@date)

OR even

SELECT MAX([EndTime]) 
FROM [dbo].[Table]  
where convert(varchar(16),EndTime,120) = @date

Upvotes: 1

Related Questions