Select all sql records within hour range - sql

I have a sql table called morecrimes. I would like to select every record that occurred after 10pm and before 6am regardless of date.
I know I should use datepart but cannot quite get it to fire
There is a column is called date and has values like '2013-09-13 16:45:59'
Is the below SQL on the right track?
SELECT datepart('hour',date)>22
from morecrimes

SELECT *
FROM morecrimes
WHERE datepart('hour',date)>=22
OR datepart('hour',date)<6

Related

Max Date Last Update Date SQL

I am asking for help with this item. I am a novice to SQL and not very sure how to handle this problem I appreciate any help from the forum.
I have a table that is updated multiple times a day. I would like to create a view that only displays the last update that was made for a given day.
Here is a sample of the data
enter image description here
This is the desired result of the SQL Query when the data set provided has been queries
enter image description here
As I understood you want to get the last record of every day.
Just group it by day
You will have to use EXTRACT to do it
Example: EXTRACT(DAY FROM DATE)
Then select the max time from the column where you have the time of the day. If you dont have the time in a different column you will also need to extract it.
SELECT MAX(TIME_COLUMN) FROM `TABLE_NAME`
GROUP BY EXTRACT(DAY FROM DATE)
SELECT MAX (your_date) AS "Max Date"
FROM your_table

SQL: select date rows that contain specific hour and minute

I am querying a table that has the date column as follows:
date
2021-03-08 05:05:31+00
2021-03-08 05:10:31+00
How can I select all the rows that contain 05:05 as the hour and minute in SQL? i.e. rows where hour = 05, and minute = 05. In this case it will be the first row.
Q: How can I select all the rows that contain 05:05 as the hour and minute in SQL?
A: For MySQL, look in the MySql Date and Time functions. There, you'll find Extract().
You can use it as follows:
https://www.w3schools.com/sql/func_mysql_extract.asp
Extract the minute from a datetime:
SELECT EXTRACT(MINUTE FROM "2017-06-15 09:34:21");
This assumes that you're storing the column as a "Date" type.
Different RDBMS vendors have different Date/Time functions. You'll have to read the documentation and experiment to determine which syntax to use for your particular DB vendor and your particular table schema.
You Can Use below Query for get Result as per your question .
There is DateName function in SQL and you can put this in your query as below.
CreatedDate is column name..
Example :
Select * from #tmp1 where datename(hour,createdDate)=07 And datename(minute,CreatedDate)=07

How to select SQL Server data based on today's date and time?

I'm using SQL Server to COUNT some data which originates from a HTML table. I want to COUNT total rows in the database based on today's date. And then after the date hits tomorrow's date, to set the COUNT value back to zero and to restart the count back from the start.
Is there a query that can help me fetch data based on current date and time?
Something like this:
SELECT COUNT(data_id)
FROM table1
WHERE clock_time BETWEEN 000000 AND 235959
AND date_time IS TODAY's DATE (or something like that)
GROUP BY XXX
ORDER BY XXX
After the clock hits tomorrow's date, I want to reset the COUNT back to zero, and to start a new count back from 00:00:00.
I know there is NOW() query but as far as I know it only shows the date.
Someone told me I could use WHERE DATE(date)=CURDATE() but the SQL Server won't work with that.
Thank you...
You can use convert function like this
where convert(Date,date_time)= CONVERT(Date,GETDATE())
Need not to use time as you want today's data.
Try this one
SELECT COUNT(data_id) FROM table1
WHERE convert(date,date_time) = getdate()

SQL to select current month MS Access

I'm wondering if it it possible to SELECT records in which [RRRmonth] field is the current month? I've written this SQL in many ways and can't seen to get it to work.
SELECT CFRRR.CFRRRID, CFRRR.[program], CFRRR.[language]
FROM CFRRR
WHERE (((CFRRR.[workername]) Is Null) AND ((CFRRR.RRRmonth)=Month([RRRmonth])));
Thanks!
The month function will give you the month of a date/time field. The date function will give you the current date. Use the two together will give you the answer you're looking for
SELECT CFRRR.CFRRRID, CFRRR.[program], CFRRR.[language]
FROM CFRRR
WHERE CFRRR.[workername] Is Null AND month(CFRRR.[RRRmonth])=month(Date());
You also don't need all those parenthesis and makes for a messy query.

Retrieving how many transactions were made on a date in SQL?

I have a table named Sales and a column within it named Date. I'm simply trying to find how many sales were made on a specific date. My intuition was to use something like this:
SELECT COUNT(Date) FROM Sales WHERE Date='2015-04-04'
this should count all sales that were made on that date, but that returns 0. What am I doing wrong?
While it is difficult to be precise without table definitions or an indication of what RDBMS you are using, it is likely that Date is a time/date stamp, and that the result you want would be obtained either by looking for a range from the beginning of the day to the end of the day in your WHERE clause, or by truncating Date down to a date without the time before comparing it to a date.
Try the below once.
select count(*) from <t.n> where date like '2015-04-04%';
When you want to find the count of rows based on a field (Date) You need to Group By over it like this:
SELECT Date, COUNT(*)
FROM Sales
GROUP BY Date
Now you have all count of rows for each Date.
Type and Value of Date is important in the result of the above query.
For example in SQL Server your best try is to convert a DateTime field to varchar and then check it as the result of CONVERT like this:
SELECT COUNT(*)
FROM Sales
WHERE CONVERT(VARCHAR, Date, 111) = '2015/04/04'