Searching for variable between two times in SQL - sql

I have a variable date_entered which is in DateTime format. I made a temp field TIME which is just the time portion of date_entered. Im trying to search for the amount of tickets entered for each hour between 7am and 7pm. I need to get tickets for each hour increment between 7am and 7pm so tickets between 7-8,8-9,9-10 etc.. and they are to be displayed in different columns.
Right now I have:
=Iif((Hour(Fields!Time.Value) >= 7) AND
(Hour(Fields!Time.Value) < 8), Fields!TicketNbr.Value, 0)
However this is not getting only tickets between this hour interval and instead all tickets for that day. How can I get tickets just in that hour period? I am also using BIDS through Microsoft Visual Studio. Thanks!

Why do you need a temp field TIME? There's a TIME datatype on SQL Server 2008 which allows you to do something like this:
SELECT * FROM YourTable WHERE CAST(YourDateField AS TIME) BETWEEN '07:00' and '19:00'

Related

how to get data for last calender week in redshift

I have a below query that I run to extract material movements from the last 7 days.
Purpose is to get the data for the last calender week for certain reports.
select
*
From
redshift
where
posting_date between CURRENT_DATE - 7 and CURRENT_DATE - 1
That means I need to run the query on every Monday to get the data for the former week.
Sometimes I am too busy on Monday or its vacation/bank holiday. In that case I would need to change the query or pull the data via SAP.
Question:
Is there a function for redshift that pulls out the data for the last calender week regardless when I run the query?
I already found following solution
SELECT id FROM table1
WHERE YEARWEEK(date) = YEARWEEK(NOW() - INTERVAL 1 WEEK)
But this doesnt seem to be working for redshift sql
Thanks a lot for your help.
Redshift offers a DATE_TRUNC('week', datestamp) function. Given any datestamp value, either a date or datetime, it gives back the date of the preceding Sunday.
So this might work for you. It filters rows from the Sunday before last, up until but not including, the last Sunday, and so gets a full week.
SELECT id
FROM table1
WHERE date >= DATE_TRUNC('week', NOW()) - INTERVAL 1 WEEK
AND date < DATE_TRUNC('week', NOW())
Pro tip: Every minute you spend learning your DBMS's date/time functions will save you an hour in programming.

Drupal: getting exact date from created column in ddbb

So I'm trying to extract the created, access and login dates from the users in the database of a Drupal site. But the values are like: 1377783381, 1384248801...
I have tried to do this in the SQL:
SELECT DATE_SUB(NOW(), INTERVAL us.created SECOND), us.name
FROM users us
But it returns dates from 1970 - 1974 and dates should go from 2013 - 2018 approx.
The truth is that I don't know what represents that numbers, but they aren't seconds.
Perhaps the values are milliseconds. Try this:
SELECT DATE_SUB(NOW(), INTERVAL us.created/1000 SECOND), us.name
FROM users us;
For some reason, MySQL supports seconds and microseconds, but not milliseconds.
Actually, as I think about it, subtracting a millisecond value from the current time doesn't make sense. More likely, this is a unix time value:
SELECT FROM_UNIXTIME(us.created), us.name
FROM users us;

Server time is not aligning with local time

I have been using date_default_timezone_set('Asia/Manila'); in my website and it echoes out the correct time of my time and date but when I uploaded it to my server the curdate() and now() function gets the time in the server. How will I know how much time should I add or subtract to my sql so that it would align in the server time? I have already tested echoing the time in my website but it only shows the correct time and not the server time. Is there a way to know the time in the server?
EDIT
I am making a filter for my records in my database. Those are today's date, week, month, and past 3 months. I have successfully done that but the time in my localhost is not aligned with the server time that's why when I uploaded it into the server it only shows the records of today after 1pm in the afternoon. I tried adding DATE_SUB(CURDATE(), INTERVAL 13 HOUR) but it doesn't work. 13 hours because I remembered adding 13 hours so that the time showed in the table is aligned with the server time. But then when I added 24 hours it shows the records in the morning but in the afternoon it doesn't show any records at all. Then I tried adding 20 hours to 23 hours but no records was shown both in the morning and in the afternoon. How can I see what is the time of the server so that I can easily add or subtract hours interval so that the records will be shown in the corresponding filter?
This is my whole sql
SELECT *
FROM `report`
WHERE DATE(`Timestamp`) = DATE_ADD(CURDATE(), INTERVAL 24 HOUR)
AND ( Employee_ID IN (SELECT T1.Employee_ID
FROM `employee` T1
WHERE T1.Supervisor_ID = '".$id."'
)
OR Employee_ID IN (SELECT T2.Manager_ID
FROM `branches` T2
WHERE T2.Manager_ID = '".$id."'
)
OR Employee_ID = '".$id."'
)

How to query for grouped results in Access, based on count of time and day of week?

I have a table in an Access 2013 database, which has many rows of incidents, each of which has a "time" column and a "day_of_week" column. I need to query for a result set that shows a count of incidents during hourly time ranges from 6:00am-5:00pm, per day of the week. I can get a total count of incidents per time range like so:
SELECT "6:00AM - 6:59AM" AS [Time Range],COUNT(time) AS [Count]
FROM Incidents
WHERE time >= #6:00:00 AM# AND time <=#6:59:00 AM#
UNION
SELECT "7:00AM - 7:59AM" AS [Time Range],COUNT(time) AS [Count]
FROM Incidents
WHERE time >= #7:00:00 AM# AND time <=#7:59:00 AM#
UNION
...
So on, and so forth. It is a lengthy query, but it gets the task done. However, as stated previously, I need to drill down further to figure out how many incidents occurred specifically during each hourly time frame, per day of the week. So, I need to have a column for "time", and then an additional column for each day of the week. I know this will involve some grouping, but I am not certain what must be done.
This is my first post here. Hopefully I followed the rules.
Thanks
This query should do what you need, except that it will only show hour as an hour number, rather than the expanded description your query fragment above shows:
select day_of_week, datepart("h",[time]) as hour_num, count(*)
from incidents
group by day_of_week, datepart("h",[time])
hope this helps

time stamp field to output records from last 24 hours

I need to make an Access query output records that were only from last 24 hours. The field called " SYSADM_CUSTOMER_ORDER.CREATE_DATE" is the time-stamp field. I cant use the criteria ">date()-1", because that would give me records from after 12AM the previous day and I need to run the query at 4PM every day and only output records from after 4PM the previous day. Please give me the preoper SQL for me to copy and paste, based on my SQL below. thank you very much, Nathaniel
SELECT , SYSADM_CUSTOMER_ORDER.ID
FROM SYSADM_CUSTOMER_ORDER;
I think you should probably be using now() - 1, something like:
select * from sysadm_customer_order where create_date > now() - 1;
The date function returns the date with an implicit time of 00:00:00. You want now() which gives you both current date and time.