Get specific time from getdate() - sql

I'm facing an issue when trying to add a condition in my sql request
I want add a condition i.e date between DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0) and current date until 23:59:59.How can I specify the second part of condition?
thank you.

You can do with predicate:
someDate < dateadd(dd, 1, cast(getdate() as date))

I usually go with the following:
somedate >= cast(floor(cast getdate() as float) as datetime) and somedate < cast(ceiling(cast getdate() as float) as datetime)

Related

Get data from last month in SQL Server

I checked Get the records of last month in SQL server and it did not work!
I try to get the records of last month based on my database table and column issue_date.
What's the SQL query to do this?
For clarification, today (27-April-18) I want to get all records from March-18.
I have the issue_date in the format that I convert to date but below code gives me all records from 01-March-2018 to and including today.
DATEPART(month, CONVERT (VARCHAR(11),DATEADD(day,wo_header.issue_date,`1971/12/31`),106)) = DATEPART(month, DATEADD(month, -1, getdate()))
To get firstDay and lastDay of previous month
DECLARE #FirstDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -(DAY(DATEADD(m, -1, GETDATE() - 2))), DATEADD(m, -1, GETDATE() - 1))),
#LastDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -(DAY(GETDATE())), GETDATE()))
SELECT #FirstDayOfLastMonth, #LastDayOfLastMonth
Your required Query
SELECT *
FROM TABLE
WHERE CAST(issue_date AS DATE) BETWEEN CONVERT(DATE, DATEADD(d, -(DAY(DATEADD(m, -1, GETDATE() - 2))), DATEADD(m, -1, GETDATE() - 1))) AND CONVERT(DATE, DATEADD(d, -(DAY(GETDATE())), GETDATE()))
Perhaps the easiest method is eomonth(). If there is no time component on issuedate:
where issuedate > eomonth(getdate(), -2) and
issuedate <= eomonth(getdate(), -1)
If there is a time component:
where issuedate >= dateadd(day, 1, cast(eomonth(getdate(), -2) as date)) and
issuedate < dateadd(day, 1, cast(eomonth(getdate(), -1) as date))
Without eomonth, I would do:
where issuedate < cast(dateadd(day, 1 - day(getdate()), getdate()) as date) and
issuedate >= dateadd(month, -1, cast(dateadd(day, 1 - day(getdate()), getdate()) as date))
The code I figured out is not pretty but it works. It first adds extra column with the month number to the SELECT portion of my code:
Month(CONVERT (VARCHAR(11),DATEADD(day,wo_header.closing_date,'1971/12/31'),106)) As Month
And than is used for WHERE statement:
Month(CONVERT (VARCHAR(11),DATEADD(day,wo_header.closing_date,'1971/12/31'),106)) = month(getdate())-1
So for anyone like me working in SQL report kind-of environment it should work.

SQL Server 2014 - How to get date range of 7 days ago plus 1 hour

I need to pull up details from 1 hour past midnight, 7 days ago. But I'm not getting the results I need.
My code:
SELECT *
FROM Table
WHERE BusinessDay >= DateAdd(HOUR, 1, (DateAdd(DAY, -7, getdate()))) --#StartDate
AND BusinessDay <= DateAdd(HOUR, 1, getdate()) --#EndDate
When I give an actual time like below, I get the results I need:
WHERE BusinessDay >= '2018-04-04 00:01:000' --#StartDate
AND BusinessDay <= '2018-04-11 00:01:000' --#EndDate
I need to pass the dates in a parameters in SSRS. But clearly, the syntax is wrong in SSRS too.
I tried the below code but I get an error message:
WHERE BusinessDay >= DateAdd(HOUR, 1, (DateAdd(DAY, -7, CAST(getdate() as date)))) --#StartDate
AND BusinessDay <= DateAdd(HOUR, 1, CAST(getdate() as date)) --#EndDate
The error message: The datepart hour is not supported by date function dateadd for data type date.
So, I also tried the below code but got no results:
WHERE BusinessDay >= DateAdd(HOUR, 1, (DateAdd(DAY, -7, CAST(CAST(GETDATE() AS DATE) AS DATETIME)))) --#StartDate
AND BusinessDay <= DateAdd(HOUR, 1, CAST(CAST(GETDATE() AS DATE) AS DATETIME)) --#EndDate
Try this:
SELECT DATEADD(HH, 1, FORMAT(DATEADD(DD, -7, CAST(GETDATE() AS DATE)), N'yyyy-MM-dd HH:00:000'))
SQL Server 2014 introduced the FORMAT() function to make it easier to return/display data types in a formatting that you want without having to do a whole bunch of math with the formulas. Note: the format pattern is case-sensitive.
GETDATE() = '2018-04-11 18:20:00.000'
Formula Result: '2018-04-04 01:00:00.000'
For more information on the FORMAT() function, see here: https://learn.microsoft.com/en-us/sql/t-sql/functions/format-transact-sql
With a little tweak to Gordon's answer to avoid an error produced by doing a CAST within the DATEADD function:
DECLARE #td DATETIME = CAST (GETDATE() AS DATE);
DECLARE #td1AM DATETIME = DATEADD(HOUR, 1, #td);
DECLARE #weekAgo1AM DATETIME = DATEADD(DAY, -7, #td1AM);
Then do your query with this:
SELECT *
FROM Table
WHERE BusinessDay >= #weekAgo1Am -- #StartDate
AND BusinessDay <= #td1AM -- #EndDate
For SSRS, via the answer linked in your comments, use TODAY():
DECLARE #td DATETIME = TODAY();
DECLARE #td1AM DATETIME = DATEADD(HOUR, 1, #td);
DECLARE #weekAgo1AM DATETIME = DATEADD(DAY, -7, #td1AM);
And the same query:
SELECT *
FROM Table
WHERE BusinessDay >= #weekAgo1Am -- #StartDate
AND BusinessDay <= #td1AM -- #EndDate
getdate() has a time component. So, remove it first:
WHERE BusinessDay >= DateAdd(HOUR, 1, (DateAdd(DAY, -7, cast(CAST(getdate() as date) as datetime)))) AND --#StartDate
BusinessDay <= DateAdd(HOUR, 1, cast(CAST(getdate() as date) as datetime)) --#EndDate

how to get records of previous day using tsql?

I need all the records from last day?
Hi
Select * from table1 where tabledate > getdate() -1
with this query, i need to run is exactly after midnight to get exact result. I need to run it in day time and get all the previous day's records.
In SQL Server 2005, this is generally the fastest way to convert a datetime to a date:
DATEADD(day, DATEDIFF(day, 0, yourDate), 0)
In your case, it's done only once, so the how doesn't really matter much. But it does give the following query.
Select
*
from
table1
where
tabledate >= DATEADD(day, DATEDIFF(day, 0, getDate()) - 1, 0)
AND tabledate < DATEADD(day, DATEDIFF(day, 0, getDate()), 0)
Check this page out. It is a great resource for calculating dates.
http://www.simple-talk.com/sql/learn-sql-server/robyn-pages-sql-server-datetime-workbench/#calculatingdates
Another method is to use DATEDIFF alone:
SELECT * FROM table1
WHERE DATEDIFF(DAY, tabledate, GETDATE()) = 1
A datediff of 1 for day covers any time in the previous day.
DECLARE #d SMALLDATETIME;
SET #d = DATEDIFF(DAY, 0, GETDATE());
SELECT <cols> FROM dbo.table1
WHERE tabledate >= DATEADD(DAY, -1, d)
AND tabledate < #d;
Try this:
your_field = cast(dateadd(D,-1,getdate()) as DATE)

TSQL SELECT previous date's records

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

Compare current date with stored datetime using month an year only

Using SQL Server 2005 I have a field that contains a datetime value.
What I am trying to do is create 2 queries:
Compare to see if stored datetime is of the same month+year as current date
Compare to see if stored datetime is of the same year as current date
There is probably a simple solution but I keep hitting brick walls using various samples I can find, any thoughts?
Thanks in advance.
Compare the parts of the date:
WHERE YEAR( columnName ) = YEAR( getDate() )
While the other answers will work, they all suffer from the same problem: they apply a transformation to the column and therefore will never utilize an index on that column.
To search the date without a transformation, you need a couple built-in functions and some math. Example below:
--create a table to hold our example values
create table #DateSearch
(
TheDate datetime not null
)
insert into #DateSearch (TheDate)
--today
select getdate()
union all
--a month in advance
select dateadd(month, 1, getdate())
union all
--a year in advance
select dateadd(year, 1, getdate())
go
--declare variables to make things a little easier to see
declare #StartDate datetime, #EndDate datetime
--search for "same month+year as current date"
select #StartDate = dateadd(month, datediff(month, 0, getdate()), 0), #EndDate = dateadd(month, datediff(month, 0, getdate()) + 1, 0)
select #StartDate [StartDate], #EndDate [EndDate], TheDate from #DateSearch
where TheDate >= #StartDate and TheDate < #EndDate
--search for "same year as current date"
select #StartDate = dateadd(year, datediff(year, 0, getdate()), 0), #EndDate = dateadd(year, datediff(year, 0, getdate()) + 1, 0)
select #StartDate [StartDate], #EndDate [EndDate], TheDate from #DateSearch
where TheDate >= #StartDate and TheDate < #EndDate
What the statement does to avoid the transformations, is find all values greater-than or equal-to the beginning of the current time period (month or year) AND all values less-than the beginning of the next (invalid) time period. This solves our index problem and also mitigates any issues related to 3ms rounding in the DATETIME type.
SELECT * FROM atable
WHERE
YEAR( adate ) = YEAR( GETDATE() )
AND
MONTH( adate ) = MONTH( GETDATE() )
It sounds to me like DATEDIFF is exactly what you need:
-- #1 same month and year
SELECT *
FROM your_table
WHERE DATEDIFF(month, your_column, GETDATE()) = 0
-- #2 same year
SELECT *
FROM your_table
WHERE DATEDIFF(year, your_column, GETDATE()) = 0
The datepart function lets you pull the bits you need:
declare #d1 as datetime
declare #d2 as datetime
if datepart(yy, #d1) = datepart(yy, #d2) and datepart(mm, #d1) = datepart(mm, #d2) begin
print 'same'
end
You can use something like this
a)
select *
from table
where MONTH(field) = MONTH(GetDATE())
and YEAR(field) = YEAR(GetDATE())
b)
select *
from table
where YEAR(field) = YEAR(GetDATE())