I'm having a hard time doing this query.
I want to compare dates in my query, dates from my DB are in this format:
(MM/DD/YYYY HH:MM:SS AM)
I want to compare this date with tomorrow's day, today plus one.
My questions are:
How do I declare tomorrow's date in sql server?
How would you compare these two dates?
Thank you!! =D
EDIT : DATES in DB are VarChar =S
declare tomorrow's date : DATEADD(dd,1,getdate())
compare dates :
WHERE column >= CONVERT(datetime, CONVERT(varchar, DATEADD(day, 1, GETDATE()), 102))
AND column < CONVERT(datetime, CONVERT(varchar, DATEADD(day, 2, GETDATE()), 102))
Assumes datetime datatype for columns
WHERE
MyCol >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 1)
AND
MyCol < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 2)
This (yyyy-mm-dd) removes the time component to test of MyCol is tomorrow
2009-10-06 00:00:00 <= MyCol < 2009-10-07 00:00:00
You don't strip time from MyCol: this will affect performance and disallow index usage etc
Efficiency of remove time from datetime question, which is why I used this format and avoid varchar conversions...
Edit:
Avoiding implicit conversions and string matching
10/06/2009 <= MyCol < 10/07/2009
WHERE
MyCol >= CONVERT(char(10), DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 1), 101)
AND
MyCol < CONVERT(char(10), DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 2), 101)
Of course, it'll fail at the end of December...
I would think your dates are most likely to be in SQL Server's datetime datatype, and that the format you give is just the default string representation.
Typically, I use something like:
SELECT *
FROM tbl
WHERE datecol = CONVERT(datetime, CONVERT(varchar, DATEADD(day, 1, GETDATE()), 101))
However, if your datetimes include a time piece, you need to use something like this:
SELECT *
FROM tbl
WHERE datecol >= CONVERT(datetime, CONVERT(varchar, DATEADD(day, 1, GETDATE()), 101))
AND datecol < CONVERT(datetime, CONVERT(varchar, DATEADD(day, 2, GETDATE()), 101))
There are other date arithmetic tricks you can use. There are plenty here on SO if you look for SQL dates
SQL Server allows you to declare variables
DECLARE #TomorrowsDate DateTime
And you can set it to the current date and time
SET #TomorrowsDate = DATEADD (Day, 0, GETDATE())
For tomorrow's date (without time)
SET #TomorrowsDate = DATEADD (Day, 1, CONVERT (VARCHAR, GETDATE(), 101))
To use it in a query with a column without declaring a variable
SELECT Column1, Column2
FROM YourTableName
WHERE DateColumn BETWEEN DATEADD (Day, 0, CONVERT (VARCHAR, GETDATE(), 101))
AND DATEADD (Day, 1, CONVERT (VARCHAR, GETDATE(), 101))
Related
I am trying to filter records based on current date (date part only) against a column "date_sent" which is a DateTime datatype. Though this can be written in multiple ways, I want to know which method will be the most efficient. The table has 11-12 millions of records ant any given time.
Let's say the current date is 16th April 2018
SELECT *
FROM TABLE_NAME
WHERE datepart(YEAR, date_sent) = 2018
AND datepart(MONTH,date_sent) = 4
AND datepart(DAY,date_sent) = 16;
SELECT *
FROM TABLE_NAME
WHERE convert(char(8), date_sent, 112) = convert(char(8), getdate(), 112);
Any other suggestions.
I would start with:
select *
from table_name
where date_sent >= dateadd(day, datediff(day, 0, getdate()), 0) and
date_sent < dateadd(day, 1 + datediff(day, 0, getdate()), 0)
The right hand side removes the time component of the current date. The comparisons should allow Sybase to still us an index on date_sent.
EDIT:
Perhaps Sybase doesn't permit 0 as a date value. You can also do:
select *
from table_name
where date_sent >= dateadd(day, datediff(day, cast('2000-01-01' as date), getdate()), cast('2000-01-01' as date)) and
date_sent < dateadd(day, 1 + datediff(day, cast('2000-01-01' as date), getdate()), cast('2000-01-01' as date))
In my SQL Server procedure, I have one input parameter like created_date.
Based on that created date I have to find out first and last date of the month.
For example,
If created_date is 12-08-2016,
start_date should be '01-08-2016'
end_date should be '31-08-2016'
If Created_date is 15-06-2016
start_date should be '01-06-2016 00:00:00'
end_date should be '30-06-2016 23:59:59'
Since your input is in DD-MM-YYYY format, so it is need to covert first as MM-DD-YYYY then only it is easy to find the first and last day of the month.
The below query will accept the input and return the result in your expected format:
DECLARE #datevalue AS VARCHAR(20) = '15-06-2016'; --12-08-2016';
SELECT CONVERT(VARCHAR(10), DATEADD(M, DATEDIFF(M, 0, CONVERT(DATETIME, #datevalue, 105)), 0), 105) [start_date],
CONVERT(VARCHAR(10), DATEADD(D, -1, DATEADD(M, DATEDIFF(m, 0, CONVERT(DATETIME, #datevalue, 105)) + 1, 0)), 105) [end_date]
Output:
start_date end_date
----------------------
01-06-2016 30-06-2016
select
DATEADD(Month, DATEDIFF(Month, 0, GETDATE()), 0) as monthfirstdate,
CAST(EOMONTH(GETDATE()) as DATETIME) as monthlastdate
EOMONTH works from SQL server 2012
I want to query the database within a stored procedure with the following SQL statement:
SELECT date1 FROM table INTO dates
WHERE date1 < CONVERT(VARCHAR(8), GETDATE(), 112)
AND date1 > CONVERT(VARCHAR(8), DATEADD(DAY, -4, GETDATE()), 112);
I get an error on the WHERE clause, what am I doing incorrectly?
The syntax erro is because the INTO is wrong.
1) But as #Jonathan has mentioned , the funcitions GETDAT, CONVERT, DATEADD
aren't native and not exists at INFORMIX database. Unless you have create it...
2) The "dates" should be a variable at your SPL.
3) The SELECT should return only 1 row....
SELECT date1 INTO dates FROM table
WHERE date1 < CONVERT(VARCHAR(8), GETDATE(), 112)
AND date1 > CONVERT(VARCHAR(8), DATEADD(DAY, -4, GETDATE()), 112);
This should be a valid Informix syntax:
SELECT date1 INTO dates FROM table
WHERE date1 < today
AND date1 > today - 4 units day ;
For more information about the syntax , please check at the IBM Knowledge Center
Assuming date1 is datetime type then Use Between
SELECT date1 INTO dates FROM table
WHERE date1 BETWEEN CONVERT(VARCHAR(8), DATEADD(DAY, -4, GETDATE()), 112) AND
CONVERT(VARCHAR(8), GETDATE(), 112);
if date1 is not datetime, then convert it to then Use Between
SELECT date1 INTO dates FROM table
WHERE CONVERT(VARCHAR(8), date1, 112) BETWEEN CONVERT(VARCHAR(8), DATEADD(DAY, -4, GETDATE()), 112) AND
CONVERT(VARCHAR(8), GETDATE(), 112);
I wrote a query to obtain First of month,
SELECT DATEADD(DAY, -(DATEPART(DAY,GETDATE())-1), GETDATE()) as First_Of_Month;
for which i do get the appropriate output, but my time stamp shows the current time.
Here's what I am doing in the query, hope i make sense.
using datepart i calculated the no. of days (int) between the 1st and today (27-1 =26)
Then using dateadd function, i added "-datepart" to get the first of the month.
This is just changing the date, what should i look at or read about in order to change the time. I am assuming that it would have something to do with 19000101
For SQL Server 2012 (thanks adrift and i-one)
DECLARE #now DATETIME = CURRENT_TIMESTAMP;
SELECT DATEADD(DAY, 1, EOMONTH(#now, -1));
-- or
SELECT DATEFROMPARTS(YEAR(#now), MONTH(#now), 1);
For SQL Server 2008+
DECLARE #now DATETIME = CURRENT_TIMESTAMP;
-- This shorthand works:
SELECT CONVERT(DATE, #now+1-DAY(#now));
-- But I prefer to be more explicit, instead of relying on
-- shorthand date math (which doesn't work in all scenarios):
SELECT CONVERT(DATE, DATEADD(DAY, 1-DAY(#now), #now));
For SQL Server 2005
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()),0);
A caveat if you're using this SQL Server 2005 method: you need to be careful about using expressions involving DATEDIFF in queries. SQL Server can transpose the arguments and lead to horrible estimates - as seen here. It might actually be safer to take the slightly less efficient string approach:
SELECT CONVERT(DATETIME, CONVERT(CHAR(6), GETDATE(), 112) + '01');
SELECT convert(varchar(10),DATEADD(DAY, - (DATEPART(DAY,GETDATE())-1), GETDATE()),120)+' 00:00:00' as First_Of_Month;
Just the date
DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
So for a month it is:
DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0) AS FirstDatetimeOfMonthmm,
I think the easiest way is to cast the result to date:
SELECT cast(DATEADD(DAY, -(DATEPART(DAY,GETDATE())-1), GETDATE()) as date) as First_Of_Month
One alternative:
SELECT cast(
cast(datepart(yyyy, getdate()) as varchar)
+ '-'
+ cast(datepart(mm, getdate()) as varchar) + '-01 00:00:00'
as datetime)
Build up the date from year/month components, then tack on the 1st and midnight.
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) as First_Of_Month;
Try following code:
SELECT CAST(CAST(GETDATE() AS DATE) AS DATETIME) AS StartDateTime,
DATEADD(ms, -3, CAST(CONVERT(date, DATEADD (DAY,1,GETDATE())) AS varchar(10))) AS EndDateTime
Result:
StartDateTime
EndDateTime
2022-05-23 00:00:00.000
2022-05-23 23:59:59.997
I want to select all records from a table Log where the DateAndTime field values (of type datetime) are for the day before today, whatever day it is.
So if today is 2011-06-08, I want to select all rows where DateAndTime is greater than or equal to 2011-06-07 00:00:00 and also less than 2011-06-08 00:00:00.
I'm guessing the potential pitfall here would be it's behaviour on the 1st day of the month, as obviously a date like 2011-06-00 is invalid, and should be 2011-05-31.
For SQL Server 2008 you can use this.
select *
from [log]
where cast(DateAndTime as date) = cast(getdate()-1 as date)
Pre 2008 you can use this
select *
from [log]
where DateAndTime >= dateadd(d, datediff(d, 0, getdate())-1, 0) and
DateAndTime < dateadd(d, datediff(d, 0, getdate()), 0)
Related on DBA: Cast to date is sargable but is it a good idea?
SELECT * FROM Log
WHERE DateAndTime >= DATEADD(DAY,-1, CAST(GETDATE() AS DATE))
AND DateAndTime < CAST(CAST(GETDATE() AS DATE) AS DATETIME)
This example assumes SQL Server:
select *
from log
where convert(varchar(8), DateAndTime , 112) = convert(varchar(8), getdate()-1, 112)
Essentially, convert the date to yyyymmdd (the 112 parameter) and then check it is equal to yesterday's date (getdate()-1), also converted to yyyymmdd.
Assuming SQL Server
declare #today date
set #today = GETDATE()
select * from Log where DateAndTime between DATEADD(dd, -1, #today ) and #today
It should include conditional operator and not between .
Otherwise it includes today's records as well.
Declare #today date
Set #today = GETDATE()
Select YourcolumnNames from log
Where DateAndTime >= DATEADD(dd, -1, #today ) and DateAndTime < DATEADD(dd, -1, #today )
Moreover, you should mention the column name and * should be avoided in the select statement. This can improve the performance