how to set timestamp in datetime field if the time is not specified?

Posted on

Question :

I have a Stored Procedure having input parameter as @startDate as datetime and I am using this parameter to compare this fields to table column. I want to convert that @startDate field if the timestamp is not passed by the user in SP?

Example: Let’s say if user passes @startDate = ’01/14/2018 6:30:00 PM’ then no it is should be taken as it is in the SP but if @startDate is passed as ’01/14/2018′ only then it should be converted to ’01/14/2018 11:59:59 PM’.

Answer :

There’s a few ways. Here’s one.

declare @startDate datetime = '20180114'

if @startDate = cast(@startDate as date)
begin
    set @startDate = dateadd(second,-1,dateadd(day,1,@startdate))
end

select @startDate

Leave a Reply

Your email address will not be published. Required fields are marked *