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

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()

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

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'

How to seperate SQL query result based on parts of the column input

I'm wondering if it is even possible and how to do this. We work with SQL Server 2005
Someone asked me a question to sum up the volume used on the storage per day.
The size of files is stored in our DB as pixelsize. in the same table also the datetime column is available. It contains records like this: 2009-01-31 10:59:13.000
So to get the total size stored per day I need to count the pixelsize records and then seperate them by date so that the end result is that i get the volume pixel size per day.
Till now I got this:
select round (sum(pixeldatafilesize)/1048576,2) as studysizemb
from filename
where DateTime between '2014-05-01' and '2014-06-01'
I tried to add group by datetime asc but it fails to give me the correct result.
Does anyone know how to separate correctly in this case?
When I've dealt with this in the past I've used something like
SELECT ROUND(SUM(pixeldatafilesize)/1048576,2) AS studysizemb,CONVERT(varchar(50),DateTime ,101) AS DateTime FROM filename
WHERE DateTime BETWEEN '2014-05-01' AND '2014-06-01'
GROUP BY CONVERT(varchar(50),DateTime ,101)
Which should give you dates formatted as 09/17/2014 and will return the same value for any time of day.

Change a date field to the last day of the previous month in SQL

I have a date field in my database which I need to change to the last day of the previous month. Currently, all dates in this field are dated the first of the month. I know I can do this using a case statement, however, I know there has to be an easier way of doing this.
This is also a date time field if that makes a difference.
Any assistance would be greatly appreciated.
update your_table
set column_date=DATEADD(day,-1,DATEADD(month,(MONTH(#date)-1),DATEADD(year,Year(column_date)-1900,0)))
In SQL SERVER you can do it like this:
UPDATE MYTABLE
SET MYDATEFIELD = DATEADD(DAY,-1,MYDATEFIELD);
In Oracle it should just be:
UPDATE MYTABLE
SET MYDATEFIELD = MYDATEFIELD - 1;
Every major database should have something pretty similar

Get records as of today or up to a certain date in Oracle

Could somebody recommend the query to retrieve records up to today or certain dates?
I'm required to produce an Oracle report where user needs to enter a date and records up to that date will be shown.
I tried
select * from the_table where the_date <= sysdate
However it seems to produce an inaccurate result. What is the better query for this. For now I'm just playing around with sysdate. Later I will need to use a certain date keyed in by the user and all the records up to that date needs to be shown.
Any suggestions?
Sometimes you get inaccurate records because of little differences like minutes and seconds when two dates have the same day/month/year. Try the following
select * from the_table where TRUNC(the_date) <= sysdate
The TRUNC removes the minute and the seconds. Sometimes you get inaccurate records without using that