I have two date columns; given_schedule and delivery_schedule. To query them, I use the following:
SELECT *
FROM table2
WHERE CONVERT(date, given_schedule) >= CONVERT(date, DATEADD(dd, -3, GETDATE()))
This SELECT returns all the rows within the last 3 days. What I need to do, though, is remove all of the returned rows which have today's date in the delivery_schedule.
How should I update my query to do this? Essentially, I want to remove anything returned by the following query:
SELECT * FROM table2
WHERE CONVERT(date, delivery_schedule) = CONVERT(date, GETDATE())
SELECT * FROM table2
WHERE CONVERT(date, given_schedule) >=CONVERT(date, DATEADD(dd, -3, GETDATE()))
AND CONVERT(date, GETDATE()) <> CONVERT(date, delivery_schedule)
You mean something like this?
SELECT *
FROM table2
WHERE CONVERT(date, given_schedule) != GETDATE()
AND CONVERT(date, given_schedule)>=CONVERT(date, DATEADD(dd, -3, GETDATE()))
It's not clear what this question has to do with C# though, since this is SQL code
Related
I want to calculate the currently logged in users for which i tried the below query:
select COUNT(*)from [dbname]
where login_time < SYSDATETIME()
and logout_time is NULL
But i want to get the records for only current date so i tried :
select COUNT(*)from [dbname]
where ( SYSDATETIME()-1) < login_time < SYSDATETIME()
and logout_time is NULL
This seems to be incorrect. So how to form the query properly? I cant use GETDATE() or Datetime.now() as it will give the secs as well which wont match with the datetime in the database
SELECT COUNT(*) from [dbname]
WHERE login_time < SYSDATETIME()
AND CONVERT(date, login_time) = CONVERT(date, getdate())
AND logout_time is NULL
You want to use CONVERT(date, getdate()) as of SQL Server 2008
CONVERT(date, getdate()) Examples
If the current date is 3/12/2015, then I need to get the files from dates 2/12/2015, 3/12/2015, 4/12/2015. Can anyone tell me an idea for how to do it?
<%
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433/CubeHomeTrans","sa","softex");
Statement statement = con.createStatement() ;
ResultSet resultset = statement.executeQuery("
select file from tablename
where date >= DATEADD(day, -1, convert(date, GETDATE()))
and date <= DATEADD(day, +1, convert(date, GETDATE()))") ;
while(resultset.next())
{
String datee =resultset.getString("Date");
out.println(datee);
}
}
catch(SQLException ex){
System.out.println("exception--"+ex);
}
%>
This is the query I have done, but it's erroneous. I need to get the previous date, current date and next date.
Use DATE_ADD() And DATE_SUB() functions:
Try this:
SELECT FILE, DATE
FROM ForgeRock
WHERE STR_TO_DATE(DATE, '%d/%m/%Y') >= DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY)
AND STR_TO_DATE(DATE, '%d/%m/%Y') <= DATE_ADD(CURRENT_DATE(), INTERVAL 1 DAY);
Check the SQL FIDDLE DEMO
::OUTPUT::
| file | DATE |
|------|------------|
| dda | 31/12/2015 |
| ass | 01/01/2016 |
| sde | 02/01/2016 |
Simplest way to get all these dates are as below:-
CURRENT DATE
SELECT DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
NEXT DAY DATE (Adding 1 to the dateadd parameter for one day ahead)
SELECT DATEADD(day, DATEDIFF(day, 0, GETDATE()), 1)
YESTERDAY DATE (Removing 1 from the datediff parameter for one day back)
SELECT DATEADD(day, DATEDIFF(day, 1, GETDATE()), 0)
If you go through the link here, you will get an amazing way of explanation for getting date. It will clear your logic and will be useful for future reference too.
Hope that helps you
You can use dateAdd function
syntax
DATEADD(datepart,number,date)
i.e for current date
select GETDATE()
for yesterday
select DATEADD(D,-1,GETDATE())
for tomorrow
select DATEADD(D,1,GETDATE())
so, your query should be like
select file from tablename
where date >= DATEADD(D,-1,GETDATE())
and date <= DATEADD(D,1,GETDATE())
current date
date = (SELECT CONVERT(char(10), GetDate(),126))
yesterday
date = (SELECT dateadd(day,datediff(day,1,GETDATE()),0))
next day
date= SELECT DATEADD(day, 1,(convert(date, GETDATE())))
Im trying to establish a basic report function.
The tables contain basic columns like this
ID, timestamp, value, key, Query, JSON
The only unique identifier is in the key colum
Theres half a million rows
I'd like it to report back this basically
SELECT min(value) as begin, max(value) as end FROM `key`WHERE NAME='Key1'
TIMERANGE='LastMonth'
Could someone please assist with the syntax of this or the concepts im missing
Thankyou and much appreciated
Try this
SELECT min(value) as begin, max(value) as end FROM `key`
WHERE NAME='Key1' and
timestamp>=date_add(current_date(),interval -1 month) and timestamp<=current_date()
SELECT min(value) as begin, max(value) as end FROM `key`
WHERE ID='Key1'
AND timestamp >= DATEADD(month, DATEDIFF(month, -1, getdate()) - 2, 0)
AND timestamp <= DATEADD(ss, -1, DATEADD(month, DATEDIFF(month, 0, getdate()), 0))
OR
SELECT ID, min(value) as begin, max(value) as end FROM `key`
WHERE timestamp >= DATEADD(month, DATEDIFF(month, -1, getdate()) - 2, 0)
AND timestamp <= DATEADD(ss, -1, DATEADD(month, DATEDIFF(month, 0, getdate()), 0))
GROUP BY ID
How to select week data (more precisely, last 7 days data) from the current date in the fastest way as I have millions or rows in the table. I have a time stamp of created_date in sql table.
I have tried this
SELECT Created_Date
FROM Table_Name
WHERE Created_Date >= DATEADD(day,-7, GETDATE())
I have two question:
Is this query is correct?
Is this is the fastest way to get the last seven day data from a table having millions of rows ?
Yes, the syntax is accurate and it should be fine.
Here is the SQL Fiddle Demo I created for your particular case
create table sample2
(
id int primary key,
created_date date,
data varchar(10)
)
insert into sample2 values (1,'2012-01-01','testing');
And here is how to select the data
SELECT Created_Date
FROM sample2
WHERE Created_Date >= DATEADD(day,-11117, GETDATE())
to select records for the last 7 days
SELECT * FROM [TableName]
WHERE Created_Date >= DATEADD(day, -7, GETDATE())
to select records for the current week
SET DATEFIRST 1 -- Define beginning of week as Monday
SELECT * FROM [TableName]
WHERE CreatedDate >= DATEADD(day, 1 - DATEPART(WEEKDAY, GETDATE()), CONVERT(DATE, GETDATE()))
AND CreatedDate < DATEADD(day, 8 - DATEPART(WEEKDAY, GETDATE()), CONVERT(DATE, GETDATE()))
if you want to select records for last week instead of the last 7 days
SET DATEFIRST 1 -- Define beginning of week as Monday
SELECT * FROM [TableName]
WHERE CreatedDate >= DATEADD(day, -(DATEPART(WEEKDAY, GETDATE()) + 6), CONVERT(DATE, GETDATE()))
AND CreatedDate < DATEADD(day, 1 - DATEPART(WEEKDAY, GETDATE()), CONVERT(DATE, GETDATE()))
The query is correct
2A. As far as last seven days have much less rows than whole table an index can help
2B. If you are interested only in Created_Date you can try using some group by and count, it should help with the result set size
I am trying to write a sql query to which will give me result or select row from a table if the datediff is equal to 7 days or 24 days.
MY table look something like this:
TableA:
ID ExpirationDate
I want to select row if ExpirationDate-DateTime.NOW == 7 days or 24 days.
Thanks
SELECT *
FROM
TableA
WHERE
DATEDIFF(DAY, ExpirationDate, GETDATE()) = 7
OR DATEDIFF(DAY, ExpirationDate, GETDATE()) = 24
SELECT ID
FROM TableA
WHERE DATEDIFF(DAY, ExpirationDate, NOW()) IN (7, 24)
If you are using SQL Server the syntax DateDiff(day, ExpirationDate, GetDate()) IN (7,24) will do the job. For mysql you can use DateDiff(ExpirationDate, CurDate()) IN (7, 24). the clause IN (7, 24) is equivalent to = 7 Or = 24 etc.