Avatar of Aleks
Aleks
Flag for United States of America asked on

SP converting date time to date and time separately

I have a SP that will compare a datetime field with today's date
I also want to extract the time only for a separate column

I tried the following:

    DECLARE @today [DATETIME] = CONVERT([DATETIME], CONVERT([DATE], GETDATE()));

Open in new window


The first one will return todays date with zeros in the time part of the field (2017-04-18 00:00:00.000)

In order to compare to my "date_start"  field which contains time, I need to 'remove' the time portion or make it all zeros.
I tried using the

    DECLARE @datetime_start_today [DATETIME] = CONVERT([DATETIME], CONVERT([DATE], datetime_start));

Open in new window


I get an error saying 'datetimestart'  field does not exist, when in fact it does.

Once that is fixed (I assume my syntax is incorrect). I need to compare both dates

WHERE @datetime_start_today = @today

Open in new window


The above should compare only the dates and not the times.

Finally I also want to extract the time only so it shows on the screen as "10:00 AM"  for example
Web DevelopmentASPSQL

Avatar of undefined
Last Comment
Aleks

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
UnifiedIS

THIS SOLUTION 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
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Aleks

ASKER
It worked!  thanks.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck