Job To Truncate Database Records More Than 15 Days Old

Jan 26, 2005

I have a SQL Server 2K db with some 10 tables and I want to setup a nightly job to truncate all db records which are more than 15 days old. Can anyone provide me with steps involved? Any help will be highly appreciated.

View 3 Replies


ADVERTISEMENT

SQL Server - Select Records Added Today, Last 3 Days, 7 Days...

Oct 25, 2006

Hello,I am writing a query to select records added to a table today, in the last 3 days, in the last 7 days, and so on.Here is what I have (which seems that its not working exactly).   -- total listed today
SELECT COUNT (*) FROM mytable WHERE DATEDIFF(Day, mydatecolumn, getdate() ) <= 0-- total listed yesterday
SELECT COUNT (*) FROM mytable WHERE DATEDIFF(Day, mydatecolumn, getdate() ) <= 1-- total listed in the last 3 days
SELECT COUNT (*) FROM mytable WHERE DATEDIFF(Day, mydatecolumn, getdate() ) <= 3I'd like to be able to select the count for records added within the last X number of days. Can someone please help me out?  Thanks so much in advance.

View 1 Replies View Related

Records That Are 90 Days Old

Feb 3, 2006

How can I select records from a schedule table that have end dates older than 90 days?

Can I do getdate() -90?


Code:

select *
from dbo.schedule
where enddate <> endate-(90 days?)

View 1 Replies View Related

Get All Records From Last Two Days?

Apr 18, 2006

Hi Everyone,I use the following to get records from the last two days in MySql:where date_entered <= curdate() and date_entered >=DATE_SUB(curdate(),INTERVAL 2 day)I'm looking to do the same in MS-Sql server but I'm just not getting it.I've got this so far which does not work:where hit_date <= GETDATE() and hit_date >= DATE_SUB(GETDATE(),INTERVAL 2day)then I tried this:WHERE hit_date >= DATEDIFF(GETDATE(), (GETDATE()-2)>Essentially, I need all records from the last two days.Any help or guidance in this matter would be greatly appreciated.-JohnyB

View 5 Replies View Related

SQL Server - Get Records From The Last X Days

Oct 25, 2006

Hello,I am writing a SQL query to count records in a table from the last "X" number of days.There is a dateTime column that I want to base this off of:Lets say..  I want to count the records that were added in the last 3 days.  What is the query I should use to get this value?Here is what I have and it seems to not be working correctly:SELECT @TotalListedLast3Days = COUNT (*) FROM myTable WHERE DATEDIFF(Day, DateAdded, getdate() ) <= 3Can someone help so I am able to get the count of records added in the last "X" days?Thank you very much.

View 2 Replies View Related

Records Greater Than 30 Days

Sep 10, 2014

I have a query pulling all records with a disconnect date and a transaction date. However, I would like to retrieve any records that have a transaction date greater than 30 days from the disconnect date. I have been unable to figure out the correct formula to use. I think I need to use the datediff function in SQL, but I've never really used this function before.

View 2 Replies View Related

Select Records From Past 30 Days

Jul 21, 2004

What would my statement look like if I have a column in a table (SoftwareInstall) called InstalledOn which stores a date in shortDateString format and I want to select all the records where that date is <= 30 days previous to today's date. Any ideas?

View 1 Replies View Related

Find Records For X Previous Days

Jul 13, 2005

On a webform, I have three button ... [7 days]  [15 days]  [30 days]When the user clicks one of the buttons, I want to return their orders for the past X days. The WHERE clause would include something like this (for 7 days):WHERE (Order_Date BETWEEN CONVERT(DATETIME, GETDATE() - 7, 102) AND CONVERT(DATETIME, GETDATE(), 102))How do I parameterize the number of days?Thanks,Tim

View 2 Replies View Related

Delete Records Older Than 5 Days .

Aug 20, 2002

Hello ,

I am little confused in writing the exact query i.e filling the correct details in the query .
To simplify let me explain....

I am using the query as

delete MYTABLE
where datediff(dd,loaddate,getdate())>5

I have a table now with loadate column which gets the default date and time . The loadate shows correct date and time when the data was imported in the table .

Now suppose if i want to delete previous 5 days records from today
( for e.g today is 20/08/2002 3:40:00 PM ) ideally it should delete all records which are 5 days older from today . i.e from 20/08/2002 3:40:00 PM to 15/08/2002 3:40:00 PM )
But when i execute the datediff command , it deletes the records previous than 15/08/2002 till 15/08/2002.
The records from 15/08/2002 to 20/08/2002 remain intact .

I am getting some different results .

Am i missing something in the query or i am confused about the calculation of the dates the datediff command performs .

Is the logic correct or i am missing someting important ?

Thanks and Regards
Admin001

View 1 Replies View Related

Returning Records Within Days Range

Oct 31, 2013

This is a follow on from one of my earlier threads where I was trying to return one specific record. In this case I am trying to return multiple records for the same person ID and within a number of days range. Snapshot of my date with same PersonID:

PersonID Arrival_Date Leaving_Date ArrivalID
======== ============ ============ =========
123456 01/12/2012 01/12/2013 arr_56464
123456 10/12/2012 10/12/2013 arr_56474
123456 13/12/2012 13/12/2013 arr_56494

And from this I want to check if one record's leaving date of the record is within 7 days of another record's arrival date. I also want to return the record that had a leaving date within 7 days of the next arrival date.I understand that if I self join on personID with the data above I will get 9 rows, for each row I will get 3 matches, I am using INNER JOIN to join to the same table but with a different alias so I assume this is the self join I should be using.

But then how do I process this? I would want to say for record 1 check the leaving date is within 7 days of arrival date of any other record matching that PersonId but not ArrivalID, and return both records.From my snapahot of code I would eventually want to return:

PersonID Arrival_Date Leaving_Date ArrivalID
======== ============ ============ =========
123456 10/12/2012 10/12/2013 arr_56474
123456 13/12/2012 13/12/2013 arr_56494

But can't seem to get this using a self join query like this:

select a.PersonID, a.Leaving_date,a.Arrival_Date,a.arrivalID
from arrivals a INNER JOIN arrivals b
ON b.personID = a.personID
WHERE
a.arrivalid != b.arrivalid
and DATEDIFF(DD, b.[Leaving_date], a.[Arrival_Date]) <=7
and DATEDIFF(DD, b.[Leaving_date], a.[Arrival_Date]) >=0

View 4 Replies View Related

Sql Query To Return Records That Are Only 45 Days

Dec 1, 2007

The topic pretty much sums up my question.

What would the sql query be to return only all the records in a table that are 45 days old from today.

Thank you for any help in this matter

Al

View 5 Replies View Related

Getting Records That 7 Days Pass Today's Date

Feb 26, 2008

I want to get all records that are 7 days pass today's date and not equal to today's date.  Don't know how to write it so I can get records 7 days old but with this procedure I'm still getting records that are due today.  Hope this makes sense.  Can someone assist me.
select * from libraryrequestwhere  duedate > getdate() and duedate != getdate()

View 5 Replies View Related

Delete Records In A Table Older Than 90 Days . Help

Aug 5, 2002

Hi ,

I have a scheduled job which does an text file import in my database . The data gets appended in my table every day from this import job .

Since my table is growing every day , i want to truncate the table after the data has been collected for three months i.e 90 days . The table will be empty and the new data will flow in through the import .

Any thoughts how to do it through query and schedule it ???

Thanks

View 1 Replies View Related

Calculate Median Of Difference In Days Between Records

Jan 24, 2006

I have a table of sample data

Samples(sample_no, sample_date..)

I have no idea how to do the following in sql server or if its even possible:

1. Calculate the difference in days between all samples.
2. Select the median result

Any trick to get this done would be really helpful

thanks,

DB

View 3 Replies View Related

Calculate Median Of Difference In Days Between Records

Jan 24, 2006

I have a table of sample data

Samples(sample_no, sample_date..)

I have no idea how to do the following in sql server or if its even possible:

1. Calculate the difference in days between all samples.
2. Select the median result

Any trick to get this done would be really helpful

thanks,

DB

View 10 Replies View Related

Get Count Of Records Grouped By Days And User

Dec 24, 2013

If I have a table like this:

ID User Date
20 22 1-1-2013
21 22 1-1-2013
22 31 1-1-2013
23 22 1-1-2013
24 22 1-2-2013
25 22 1-2-2013
etc...

How can I get the count of records grouped by date and user?

Date 22 31
1-1-2013 3 1
1-2-2013 2 0
etc...

Is this possible?

View 4 Replies View Related

Transact SQL :: Split Records If Total Days Greater To 1

Nov 5, 2015

I want to split the data every employeid wise based on fromdate and todate if totaldays>1.

sample output specified below ....but same output required for allthe empid's

create table attendence(EmployeeID nvarchar(20),[From] datetime,[To] datetime,TotalDays float)
insert into attendence values('1417','2015-11-02 22:48:49.450','2015-11-04 22:48:49.450',3)
insert into attendence values('1418','2015-11-04 22:48:49.450','2015-11-04 22:48:49.450',1)
insert into attendence values('1419','2015-11-03 22:48:49.450','2015-11-04 22:48:49.450',2)
insert into attendence values('1420','2015-11-04 22:48:49.450','2015-11-05 22:48:49.450',2)
insert into attendence values('1421','2015-11-01 22:48:49.450','2015-11-04 22:48:49.450',4)

 OP
 -------------------------
 EmployeeID    [From]                          [To]                              TotalDays
 1417          2015-11-02 22:48:49.450         2015-11-02 22:48:49.450             3
 1417          2015-11-03 22:48:49.450         2015-11-03 22:48:49.450             3
 1417         2015-11-04 22:48:49.450          2015-11-04 22:48:49.450             3

View 3 Replies View Related

Reporting Services :: Select 10 Random Records From Past 30 Days For Each User

May 18, 2015

I have these columns. TicketID, User, Date...I want to select 10 random tickets for each user for the past 30 days. I not sure whether to do this via sql or using VB in the report design.

View 5 Replies View Related

Truncate And Update Sql Database Web From In ASP.NET 2.0

Sep 30, 2007

I have two very simple stored procedures that truncate (clear out) the rows of my 'Transactions' table, then import rows from another table into it, thus updating the table with the appropriate data.
I would like to call these two stored procedures from a web form - asp.net 2.0 web page, using 2 buttons or possibly 1 button if I could, but can't find any information on how to do this.
here is code for:
1.  my button for 'truncate'
<asp:Button ID="Truncate" runat="server" Text="Truncate" style="left: 45px; position: relative; top: -2px" Font-Size="8pt" /><br />
2.  my stored procedure for 'truncate'
ALTER PROCEDURE dbo.TruncateTrans
AS
SET NOCOUNT ON;
Truncate Table Transactions;
 
2.  my button click event and page load event handler for the web form;
 
Imports System.Data
Imports System.Data.SqlClientPartial Class Admin_Default
Inherits System.Web.UI.PageProtected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End SubProtected Sub Truncate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Truncate.Click
End Sub
End Class
 
Also, here is my other stored procedure called updateTransact.  how can i execute these using the web form and buttons?  or is there another way to do this?
 
ALTER PROCEDURE [dbo].[UpdateTransAct]
AS
BEGIN
SET NOCOUNT ON;
SELECT *
FROM Transactions INNER JOIN
UpdateTrans ON Transactions.TransID = UpdateTrans.TransID
INSERT INTO Transactions (Account, TransID, TransType, Transkind, Status, Amount, Transdate, Note, Ticker, TickerType, BaseDate, BasePrice, UnitPrice, Shares, UnitsPerContract, Commission, StopTypes, FixedValue, Strategy, WithHolding, SavUnitPrice, SavCommis, ReInvestDividends, ReInvestCapGains, StrategyType, OrderID, ReferenceID, Fees)
SELECT Account, TransID, TransType, Transkind, Status, Amount, Transdate, Note, Ticker, TickerType, BaseDate, BasePrice, UnitPrice, Shares, UnitsPerContract, Commission, StopTypes, FixedValue, Strategy, WithHolding, SavUnitPrice, SavCommis, ReInvestDividends, ReInvestCapGains, StrategyType, OrderID, ReferenceID, Fees
FROM UpdateTrans
WHERE (TransType= 'C' OR TransType='O')
ORDER BY TransDate
END
 
Any help would be appreciated.
Craig
 
 
 
 

View 3 Replies View Related

Truncate Database Statement Help

Jul 23, 2005

Hello,I hope you can help me.We have a SQL Server 2000 database at work, (which works with a VB6frontend) which grew to a considerable size, so one of my past colleaguessent me this truncate statement to use on the database.I ran it like this, and all appeared to be well, it shrank the database,Shortly afterwards, after I ran this I noticed something not quite right.for example, one of the tables we keep is a User_Admin table which has 50records in there, when I accessed this using the frontend it only showed 1record, and this happened with every table in the database accessed by thefrontend.The data in the tables is still there, but only shows one record through thefrontend.Can anyone help me, and check this statement I ran, as its weird that allthis happened after I ran this statement.Thanks,Jayne Hegersp_dboption PBTDEV,'trunc. log on chkpt.', truecheckpoint--sp_dboption sadev,'trunc. log on chkpt.', falsesp_dboption PBTDEV,'autoshrink', true--sp_dboption sadev,'autoshrink', falseDBCC SHRINKDATABASE (PBTDEV, NOTRUNCATE)

View 2 Replies View Related

How To Truncate Complete Database?

Oct 26, 2006

Hello,

I want to truncate all tables present in the particular database, Is there any simple way to do it? or do I have to do it on individula basis (table by table)?



regards

View 4 Replies View Related

Remove Weekends And Non Working Days When Calculating Days Difference Between Two Dates

Jan 7, 2014

I have an SQL code below which removes weekends and non working days when calculating days difference between two dates:

ce.enquiry_time represents when the enquiry was logged

(DATEDIFF(dd, ce.enquiry_time, getdate()) + 1)
-(DATEDIFF(wk, ce.enquiry_time, getdate()) * 2)
-(CASE WHEN DATENAME(dw, ce.enquiry_time) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, getdate()) = 'Saturday' THEN 1 ELSE 0 END)
-(SELECT COUNT(*) FROM nonworking_day WHERE nonworking_day.nonworking_date >= ce.enquiry_time AND nonworking_day.nonworking_date < dateadd(dd,datediff(dd,0,getdate()),1))

It works but I don't understand how it works it out. I am having issues understanding each coloured piece of code and how it works together.

View 1 Replies View Related

Database Options/truncate Log On Checkpoint

Nov 27, 2000

I'm trying to make sure my database options are set up properly. Our database is used for our Decision Support System. The data is loaded in once a day. Should I have the truncate log on checkpoint on and should I limit the size that the log grows to. I just had to shrink the log. It grew so much that it took up all the space left. If I have the truncate log on checkpoint do I just have to issue the checkpoint command in order for it to truncate the transaction log?

View 1 Replies View Related

Truncate Transaction Log And SHRINK DATABASE

Aug 28, 2007

Is it possible to truncate Transaction Log and Shrink DATABASE while the database is being used by users or the database becomes unuvailable during this operations?

Thank you.

View 2 Replies View Related

Database Mirroring And Truncate Table Command

Jan 3, 2008



I was working on my Lab Environment and i had a DB which was mirrored in synchronous mode without witness server. I truncated one of the tables in this DB and did a failover to the mirror server. The table got truncated in the mirror too. Though we know that truncate is a non logged operation how does this mechanism work? Can someone please expain me??? Thanks in advance. Your replies will be very much helpful to me.

View 4 Replies View Related

'Truncate' Command Doesn't Truncate Enough

Dec 30, 2004

With a database size of almost 2 GB, I run the 'truncate table eventlog command' which completes successfully, but the database size only decreases by about 10 MB so stays too large - indeed the number of rows in the eventlog table is minimal, but the otehr tables in this database don't show such an amount of tables large enough to cause the size issue either. What could be the reason and how can I reduce it (possibly truncating another table but then which one, how could I determine which is too large and needs truncating?).

View 3 Replies View Related

Truncate Database Tables Based On Foreign Key Constraints

Nov 5, 2007

Guys,

I have 600 tables in my database, out of which 40 tables are look up value tables. I want generate truncate scripts which truncates all the tables in order of Parent child relationship excluding lookup tables. Is there any way to do this apart from figuring out Parent Child relationship and then writing the truncate statements for each of the table.

For example

EmployeeDetail table references Employee table
DepartmentDetail table references Department table
Department table references Employee table

My truncate script should be

TRUNCATE TABLE DEPARTMENTDETAIL
TRUNCATE TABLE EMPLOYEEDETAIL
TRUNCATE TABLE DEPARTMENT
TRUNCATE TABLE EMPLOYEE

Is there any automated way to figure out parent and child tables and generate truncate script for the same.

Thanks

View 1 Replies View Related

Recovery Of Database - 23 Days?!?

Jun 7, 2004

I cancelled an INSERT operation on a database. Now I get:

"Recovery of database 'BounceRepository' (6) is 0% complete (approximately 2024654 more seconds)"

That is another 23 days of straight recover time!!! The original INSERT only ran for about 30 hours.

In Windows Performance Monitor, I can see that the system isn't disk or CPU limited. What is the server doing? How can I make it go faster?

View 14 Replies View Related

SQL Server Admin 2014 :: What Happens If Shrink Database With Truncate Only And Take A Backup

Apr 30, 2015

I would like to know what happens if i shrink the database with truncate only option and do a full backup or transaction log backup ? are the full backup or transaction log backup valid? I know that the performance of the database is bad if i shrink the database. What happens to full backup or transaction log backups?

View 9 Replies View Related

Copy Records From One Database To Another Database With Difference Of Records.

Apr 6, 2008

Hi,
I already submitted this type of question before and i receive reply. But unfortunately i found out errors when performing on my system.

My problem regarding to this one:

Suppose i have two databases with same tables with different records and I would like to copy the records from one database to another data and vice-versa. So that both the tables contains same number of records inside the tables.

Example:

Database1 (EmployeeTable) contains 6 records.
Database2 (EmployeeTable) contains 10 records.

It should copy only those records which is not present in each other database. No duplicate records.

Before i was recommend to use Primary key, if it is not present use index.

Hope this time i could solve my problem.

Thanks.

Kashif Chotu



View 1 Replies View Related

Shrink Database Takes Too Long (days)

Dec 14, 2007

I have a database with 2 almost identical tables.
Each one is about 1.2 Billion rows, 0.5 Tb each.
I've truncated one of them and started SHRINK DB WITH REORGANIZE PAGES
It is running already for 3 days.
When should I start to worry :)

Thank you in advance

View 3 Replies View Related

Schedule Backup Of Specific Database While Only Saving The Last 7 Days In Repository

Apr 15, 2008



Hello to all members.
I am using SQL Server 2005 std edition 32 bit
The database is configured in compatibility mode to sql server 2000
The backup mode for the database is configured as simple

With SQL Server Agent I am running schedule task of the backup script

The question is:

I would like to save only the last 7 days in repository.
This means that when the 8th days has become then the 1st day backup is deleted.
in the 9th day the 2nd day backup file is deleted.
and so on

I am using the the T-SQL BACKUP DATABASE command with the following arguments:
RETAINDAYS = 7
NOFORMAT
NOINIT
SKIP

I am in situation that the command above only append to the backup file and without deleting the expired backup days.

What I need to change in the parameters that the script will delete the expired backup days.

Thanking in advance everyone who assist.

View 1 Replies View Related

Schedule Backup Of Specific Database While Only Saving The Last 7 Days In Repository

Apr 19, 2008



Hello to all members.
I am using SQL Server 2005 std edition 32 bit
The database is configured in compatibility mode to sql server 2000
The backup mode for the database is configured as simple

With SQL Server Agent I am running schedule task of the backup script

The question is:

I would like to save only the last 7 days in repository.
This means that when the 8th days has become then the 1st day backup is deleted.
in the 9th day the 2nd day backup file is deleted.
and so on

I am using the the T-SQL BACKUP DATABASE command with the following arguments:
RETAINDAYS = 7
NOFORMAT
NOINIT
SKIP

I am in situation that the command above only append to the backup file and without deleting the expired backup days.

What I need to change in the parameters that the script will delete the expired backup days.


It is too much work when working with Database Maintenance Wizard.
lets take a scenario that I have 10 database.
I would like that each database will backup into seperate file.
I need to create 10 * 7 Maintenance plans.

I have created a directory with the name SDBbackup.
Now under the directory SDBbackup I have created 7 directories (1,2,3,4,5,6,7).
Each directory is for Sunday=1 , Monday=2 , and etc.

It's very much work if I need to accomplish it with Database Maintenance Wizard.

This is why I am asking a specific question regarding T-SQL.

Thanking in advance everyone who assist.

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved