We help IT Professionals succeed at work.
Get Started

SP converting date time to date and time separately

Aleks
Aleks asked
on
171 Views
Last Modified: 2021-04-21
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
Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE