SQL Server- Get Date 6 months in past - sql-server

Hey how would I get data for a date column that's older than 6 months?
select * from myTable where dateColumn >
Thanks

Use DATEDIFF function.
Read more here: https://msdn.microsoft.com/en-us/library/ms189794.aspx
SELECT *
FROM myTable
WHERE DATEDIFF(MM, dateColumn, GETDATE()) > 6

SELECT * FROM myTable WHERE DATEDIFF(day, NOW(), dateColumn) > 180

select *
from table
where
date_column >=
DATEADD(m, -6, convert(date, convert(varchar(6), getdate(),112) + '01'))

I would use
select * from myTable where dateColumn > DATEADD(mm,GETDATE(),-6)
This way you arent applying a function on the lookup column, which in some cases can result in performance issues

Dateadd is very simple to use.
the first parameter is the interval, m means month, d means day ect. the second parameter is the increment, and the last one is obviously the date
select dateadd(m,6,getdate())
more info here
http://www.w3schools.com/sql/func_dateadd.asp
Once you understand dateadd then you can simply use it in where clause as such
Select * from Table1 where date1 >= dateadd(m,6,date1)

Related

How to get results older than 6 months in SQL Server 2014?

I need code that pulls up records of Accounts where last Payment Status was older than 6 months ago. But I'm not quite sure why my DateDiff won't work.
My code so far:
SELECT A.[AccountId]
,[AccountNumber]
,[AccountTypeId]
,[AccountStatusId]
,[CurrentBalance]
,[PaymentStatusID]
,D.Last_Change
FROM [Account] A
INNER JOIN (
SELECT AccountId
,MAX(Created) Last_Change
FROM PaymentStatusHistory
WHERE ToPaymentStatusID IN (1,2,11)
GROUP BY AccountId
) D
ON A.AccountID = D.AccountId
WHERE PaymentStatusID IN (1,2,11)
AND AccountStatusId IN (1,2)
--AND DATEDIFF (DAY, GETDATE(), D.Last_Change) > 180 --Need THIS line corrected.
ORDER BY CurrentBalance DESC, AccountNumber
You seem to have mixed up the starting_date and ending_date of your datediff.
AND DATEDIFF (DAY, D.Last_Change, GETDATE()) > 180
Get_date(), current date, should be after your Last_change date.
If you want a positive number, try reversing the order of the dates in your DATEDIFF.
For example: SELECT DATEDIFF(DAY,GETDATE(),'20170101') returns -250. SELECT DATEDIFF(DAY,'20170101',GETDATE()) returns 250.
I changed the order in the DATEDIFF and it worked.
AND DATEDIFF (DAY, D.Last_Change, GETDATE()) > 180
This line of code worked and gave me the result as needed.

MSSQL Obtain the last entries added in the last 30 seconds?

I want to obtain the last entries in the last 30 seconds from a MSSQL database.
In Mysql you do:
SELECT * FROM table WHERE datetime > (now() - interval 30 second)
How to do that in MSSQL?
DATEDIFF is your solution here.
SELECT
*
FROM
[table]
WHERE
DATEDIFF(second, [datetime], GETDATE()) < 30
Using dateadd with a negative integer will give you the data you are asking for
DATEADD (Transact-SQL)
SELECT
*
FROM
table
WHERE
datetime >= DATEADD(SECOND,-30,datetime)
You can use GETDATE() in combindation with DATEADD:
SELECT * FROM table WHERE datetime > (DATEADD(second,-30,getdate()))
Use DATEADD()
DATEADD(SECOND,-30,GETDATE())

How to get number of rows with timestamp between 10am and now?

I am trying to run a T-SQL query that would return all rows that contain a timestamp between 00:00:00 and now for any given date.
I've used the following code, but this only returns items within the past 24 hours:
SELECT *
FROM table
WHERE timestamp_closed = DATE(GETDATE()-1);
Here you have the number of rows:
SELECT COUNT(*)
FROM *yourtable*
WHERE timestamp_closed BETWEEN CAST(GETDATE() AS DATE) AND GETDATE()
SELECT *
FROM table
WHERE timestamp_closed BETWEEN CAST(GETDATE() AS DATE) AND GETDATE()
You could build the datevalue for "Today at 00:00:00" and now and then do a
WHERE timestamp_closed >= "Today at 00:00:00" and timestamp_closed<=GETDATE()
You can probably wrap this in a function.
select *
from table
where datepart(hh,timestamp_closed)*100 + datepart(mi,timestampclosed) <
datepart(hh,getdate())*100 + datepart(mi,getdate())
SELECT *
FROM table
WHERE (timestamp_closed > CAST(#specificDate AS DATE)
AND timestamp_closed <= GETDATE())

SQL Server: Return records within X days of a date

I need a WHERE condition in SQL Server where I can return the past 7 days of activity from a given date.
Pretend I have 2 columns of dates [dateA] and [dateB]
I am looking for something like
SELECT *
FROM TABLE
WHERE [dateB] >= ([dateA] - 7 days)
WHERE dateB >= DATEADD(DAY, -7, dateA)
Potentially useful reading...
select * from table
where dateB >= dateadd(dd,-7, dateA)
SELECT *
FROM TABLE
WHERE [dateB]) >= convert(datetime,[dateA] - 7)
You were so close
IF you need past 7 days activity then you need take difference of date in that column from today i.e GETDATE() it will be something like this......
WHERE (DATEDIFF(DAY, GETDATE(), dateA) <= 0 AND DATEDIFF(DAY, GETDATE(), dateA) > -7 )
AND (DATEDIFF(DAY, GETDATE(), dateB) <= 0 AND DATEDIFF(DAY, GETDATE(), dateB) > -7 )

SQL QUERY ( SQL SERVER) Date & Time Difference

I have a SQL SERVER Table which has only one field "StartDate" the records are as follows
**
2011-07-28 19:30:00.000
2011-07-29 21:50:00.000
2011-07-25 09:20:00.000
**
What i want to do is :
SHOW RECORDS if its CURRENT DATE ( todays date ) and the time difference between current time the StartDate is not less then 5 minutes, i have written the following code but it doesnt show me the time difference ?
SELECT * FROM table WHERE DATEDIFF(day, StartDate, GETDATE()) <= 0
SELECT StartDate
FROM table
WHERE YEAR(StartDate)=YEAR(GETDATE())
AND MONTH(StartDate)=MONTH(GETDATE())
AND DAY(StartDate)=DAY(GETDATE())
AND (DATEDIFF(minute, StartDate, GETDATE()) >= 5
OR
DATEDIFF(minute, StartDate, GETDATE()) <= 5)
How about:
SELECT StartDate
,GETDATE()
,DATEDIFF(day, StartDate, GETDATE())
,DATEDIFF(minute, StartDate, GETDATE())
,*
FROM table
WHERE DATEDIFF(day, StartDate, GETDATE()) <= 0
AND DATEDIFF(minute, StartDate, GETDATE()) >= 5
There are two ways to do it one being DateDiff the other is DATEADD. Judging by the way I read your question DateAdd should do it. Here is an example;
SELECT *
FROM [dbo].[TABLE]
WHERE [LAST_UPDATE] > DATEADD(minute,-5,GetDate())
Using BETWEEN is probably a little more optimal than two AND statements (maybe not). Try not to do a calculation on each row if you don't have to. Doing DATEADD only on the current date will only be calculated once.
SELECT
whatever
FROM
table
WHERE
StartDate
BETWEEN FLOOR( CAST( GETDATE() AS FLOAT ) )
AND DATEADD(minute, -5, GETDATE())
I interpret the question as looking for rows where the date is today (between the start of today) but not within the last 5 minutes (and less than 5 minutes ago). That might not be what you were going for.
Hope that helps

Resources