Parse Time out of A DateTime Field in Access 2016 - sql

I need to check the records of an Access Database that has a SigninTime field of data type DATETIME for any sign in that occurred between 6PM and 8PM.
I've tried:
SELECT TestingStatistics.SigninTime
FROM TestingStatistics
WHERE datepart(h,TestingStatistics.SigninTime) >=18;
Which asks me to define H
And
SELECT TestingStatistics.SigninTime
FROM TestingStatistics
WHERE TestingStatistics.SigninTime >=18;
Which just returns everything.
How do you search a DATETIME Field using Time instead of a date? Furthermore what query should I run?

Try making it a string:
datepart("h", TestingStatistics.SigninTime) >= 18;

You can extract JUST the date part of a date/time column with:
DateValue( some date time)
And you can extract JUST the time part of a date/time column with:
TimeValue( some date time)
So, this query would work.
Select SignInTime from TestingStatistics
Where TimeValue(SignInTime) between #6 pm# and #8 pm#

You can try something like this:
Where TestingStatistics.SigninTime> Convert(datetime,(Replace(Convert(varchar,GETDATE(),104),'.','-')+ ' 18:00:00.000'),104) and TestingStatistics.SigninTime <= Convert(datetime,(Replace(Convert(varchar,GETDATE(),104),'.','-')+ ' 20:00:00.000'),104))
This should show you all the record of today between 18PM-20PM, but You can replace Getdate() for any date that you need.
Hope this help!

Related

how to change date format to just year and month but must stay as a date value not varchar, or char..?

I read other similar question where the answer is to use FORMAT(getdate(),'yyy-MM') or something similar. However the problem for me in using anything like this, is that it changes the date type to a varchar or char. I need it to stay as datetype but just want Year and Month.. I tried the following..-> FORMAT(a.completeddate,'yyy-MM') which works to change to year and month but the date is no longer a datetype or date format. So when I try to do the following -> select #FirstCompletion = (A.completeddate) i get this error..Conversion failed when converting date and/or time from character string. Basically I need to convert the date column to year and month as date format so I can then pass values to variables using select #FirstCompletion = (A.completeddate) and set #secondMonth = DATEADD(month, 2, #FirstCompletion) which are Datetype variables.. Would appreciate any help I can get.. Thanks..

How to convert a datetime column to another column using just the date part plus a hardcoded time in SQL

How can I combine the datetime column (but just the date part, not the time part of it) with a hardcoded time stamp like 06:00:00 AM? I want to convert a current datetime field using the date portion of it + the 6AM time stamp to then use in a case scenario.
For example:
case
when event_datetime > datetimefield+06:00:00 AM
then 'late'
Convert both to DATE while in the CASE. Converting them to DATE will drop the time information. If you are using these fields again, I would recommend you convert them both to DATE, store them variables or temp table then utilize them.
CASE WHEN CONVERT(DATE, EVENTDATETIME) > CONVERT(DATE,DateTimefield)
THEN 'Late'
I ended up using DATEADD:
CASE WHEN event_datetime > DATEADD(HOUR,6,datetime field) THEN 'event_late'
CASE WHEN event_datetime <= DATEADD(HOUR,6,datetime field) THEN 'event_ontime'
ELSE 'event_unkwn'
END AS event_outcome
This helped me convert the datetime field to the date+06:00 AM (As date format) for the comparison with the event_datetime to work.

Remove Time from date field in Teradata

Im trying to remove the time portion of a date field I am pulling in. I am using Teradata. I tried
select cast(inv_dt as date) as invoice
from tablex
to no avail it still is showing 09/01/2015 12:00:00 AM
I dont want to cast it as a char as I need to use the field in a calculation for dates. Thank you.
Your cast(inv_dt as date) does exactly what you want, you can use it in your calculation for dates...
If it's still showing a time portion it's due to your client (maybe it assumes Teradata's DATE is similar to Oracle's including time).
Finally figured it out
select
cast(cast(inv.INV_DT- EXTRACT(DAY FROM inv.INV_DT) + 1 as date format 'mm/dd/yyyy') as char (10)) as inv_dt
from invoice inv

Records between start and end date

I am having two textfields which represents startDate and endDate.Now the problem I am facing is that I want all the records from the database which occured between this interval.But the field which stores date is TimeStamp and is of format :
24-APR-14 09.23.44.458000 PM or we can say dd-mm-yy hh.min.ss.milli AM/PM
Now obviously user entering the date is not going to enter it in such a format.So what should be query to select records from table say t1 between this date interval.
Saving Date data as Date or Datetime makes life easy.
You have tagged Mysql in the question so here is a Mysql Solution.
This is what you can do
select * from
test
where
date_format(str_to_date(`date`,'%d-%b-%y'),'%Y-%m-%d')
between '2014-04-15' AND '2014-04-24'
DEMO
You can format the user input as you want in the query in DATE_FORMAT().
User input will have a startdate and a enddate may be in datetime OR date variable
while trying to use these two input variables in the WHERE CLAUSE use BETWEEN CAST(#StartDate AS DATE) AND (CAST(#EndDate AS DATE) + 1)
This will fetch results that lies in both dates and the data range as well

In DB2 String replace

I have 2 fields in my DB2 database: DATE and TIME. DATE is string (8/25/2013), DATE is timestamp (1/1/1970 7:00:00 AM). I want to write a sql to replace 1/1/1970 with 8/25/2103. Please help.
Thanks.
Well, you need to cast your string to a date, using the DATE function
DATE(DATE)
Where you see that's it's a bad idea to name your columns with existing function names...
Then combining the TIMESTAMP, DATE, and TIME function (again really unclear with your field names, but...)
TIMESTAMP(DATE(DATE), TIME(TIME))
Will give you a timestamp where the date part is coming from the DATE field, and the time part from the TIME field
See this and that
After casting to a DATE format, adding the difference between those two absolute dates should work -
UPDATE <table-name>
SET DATE= DATEADD(day,DATEDIFF(day,'1/1/1970','8/25/2013'),DATE)
WHERE DATE < '1/2/1970' AND DATE>='1/1/1970'
More on DATEADD and DATEDIFF
Also, it is recommended to not use 'DATE' as a column name as it is a reserved [edit] function name.