How would you create a column which displays the difference of two other columns in SQL? For example, column C=column A – column B.
My idea is that when ever data enters column A and column B, column C should show the difference of the two automatically. A, B and C are datetime columns. I'm looking at setting up an 'after' trigger on the table. Is there a better approach? Thanks.
I am trying to create an exception report that will show the difference between two versions of the same row. (Combination of two different sources in sql, with source 1 having childID = 0 and the other source having childID = 1; parentID is the link between them)
The results are as follows:
ParentID - ChildID - Col1 - Col2 - Col3 1 - 0 - AA - BB - CC 1 - 1 - AA - BF - CC 2 - 0 - GG - NN - TT 2 - 1 - DE - NN - TA 3 - 0 - etc 3 - 1 - etc 4 - etc
I have 12 month report and I need show volume and difference between current and prev month volume, what is the smart way to do this, do I need to put prev month value onto same row horizontally? I think should be some other smart way, I heard about LEAD function?
This what I think for now, It should be listed per ClientID also, in my example I have single ClientID for simplicity.
I tried to do LEAD but with not success..
/* IF OBJECT_ID('tempdb..#t') is not null drop table #T; WITH R(N) AS (SELECT 1 UNION ALL SELECT N+1 FROM R WHERE N <= 12 ) SELECT N as Rn, 10001 ClientID, DATENAME(MONTH,DATEADD(MONTH,-N,GETDATE())) AS [Month],
I require outputting the date difference between two date's if it is greater than 7(DateDiff(day, DateAdd(day, t.[Started], Nxt.started), (t.[started])) > 7).I get incorrect syntax on my operator.What is the correct code?
How can I automatically change records by a specific date specified inside the record. An example would be the way ebay sales work. How does ebay have the status of an item change to closed, at the time inside the item record of the database.
I know i could use triggers or something to check the current date against the enddate everytime the record is accessed, but is there a more efficient method?
I have a flat file that is read in by SSIS and then populates our db.
In this flat file there are blank spaces that represent null entries, which are date time values that we need to remain null but instead shows up in the table as the lowest date time value (1753)
Does SSIS automatically convert this since this is not done in our code?
Hello, I am using SQL Server 2005 and ASP.NET 2.0. We have a very simple content management system where we have to keep track of date last modified for each row in all of our content tables. I know there's a "timestamp" datatype that is used for replication scenarios, but is there anything similar that I can use to set up a date_modified column for each of my content tables that will automatically update with GETDATE() whenever anything in a given row is updated? Or do I have to create a date_modified column of smalldatetime datatype and write a trigger on update for EVERY single table of content that I have in the database? It seems there should be an easier way to do this than to write 20 triggers for my 20 content tables. Thanks!
I have a SSIS data flow task that downloads data from an Oracle source which has some real dates and some date values of 0001-01-01 which I am trying to convert to '1900-01-01' using a decode(Date1,'0001-01-01','1900-01-01',Date1). The problem I am having is that when I run the package in Business Intelligence Development Studio on my local machine the date value is stored correctly in the SQL table in a datetime field as 1/1/1900 12:00:00 AM, however when I run the package from the server the field gets saved as 1/1/2001 12:00:00 AM.
I have tried a bunch of ways to work around this issue and the only solution I have found that works it to download into a temp table and then load into the live table. Does anyone have any idea of what might be causing this issue when I run the package from the server?
I am using SQL Server 2005 with Reporting Services (Using the Visual side - not direct code)
I am having problems understanding the dates. eg where to put them,
I want a report that runs on the 1st day of the month for the previous month. I know you can set up something in subscriptions but then how do I get my report header to say from .......to...............
I have been through the AW reports but can't see what I need.
Happy if someone wants to direct me to somewhere that has date examples.
Hi I wanted to get difference between this two dates '12-31-2001','01-01-2002'). 1 day 0 month 0 year. Do we have any date function to get like this or we need write our own function stored proc?> Any advice please?.
I have 2 dates in the following format mm/dd/yyyy hh:mm:ss AM.
I would like to measure the difference between 2 dates (Date1 and Date2). When I use the following expression "DATEDIFF(d, Date1, Date2)" I get a value without decimals. How can I have a value in days with decimals like e.g. 5.38? If you calculate this using Excel you get decimals behind the comma.
INSERT @CallDetail SELECT 12123, 1, '11/30/2007 10:41:34 AM' UNION ALL SELECT 43555, 1, '12/1/2007 11:21:23 AM' UNION ALL SELECT 65322, 1, '12/18/2007 04:12:34 AM' -- 65322 is the current calldetail id
INSERT @Call SELECT 111, 1, 12123 UNION ALL SELECT 112, 2, 12123 UNION ALL SELECT 113, 2, 12123 UNION ALL SELECT 123, 1, 43555 UNION ALL SELECT 134, 1, 43555 UNION ALL SELECT 143, 1, 65322 UNION ALL SELECT 145, 1, 65322 UNION ALL SELECT 154, 2, 65322 UNION ALL SELECT 185, 3, 65322
Select * from @Call Select * from @CallDetail
EXPECTED OUTPUT-- For CallType 1 EmpID 1 LastCallDate Yesterday Duration 12/1/2007 12/17/2007 16 days
-- For CallType 2 EmpID 1 LastCallDate Yesterday Duration 11/30/2007 12/17/2007 17 days
-- For CallType 3 (There was no CallType 3 made earlier by Empid 1) LastCallDate Yesterday Duration - 12/17/2007 0 days
Have a table that list item#, date the standard cost went into effect and the standard cost. How do I find the difference in StdCost on the last EffectiveDate and second to last EffectiveDate. 5.59 (01/05/2015) minus 5.81 (09/29/.014) = -.22.
Hi I would like to get more information about using detail view together with auto date/time. I am designing user input form as follow. tid : uid : tool : priority: reticle: status: remarks: request time : <- to autogenetrate current timeInsert Cancel How to use date/time function inside detailed view, insert template.
I have a client data which has the candidate id, a start date which will have only the date in varchar format and a time value associated to that date in varchar format in a seperate column.
To give a brief explanation about the data a candidate will come to study center at any time point in a day for example 10:00 AM on 20-10-2014. The start date will be 20-10-2014 and from 10:00 AM he will have test based on the time point. So if the time point is 2 HR, then at 12:00 PM he will have a test. If the time point is 8 HR, it will get added with 10:00 AM and based on this he will have the test at 06:00 PM. When the timepoint reaches 00:00 the start date needs to be the next date instead of 20-10-2014.
The start date needs to be appended with the time value such that when it crosses over the time 00:00, the start date needs to get increased by 1 ie the next day. I have added the start date to the time by using the code below
CAST(STARTDATE as datetime) + CAST(CAST(STUFF([TIME],3,0,':') as time(0)) as datetime) as [EXPECTEDDATETIME]
By this above code i have created the expected datetime however
I will not be able to hardcode the value due to dynamic data. I tried to apply >= and < to the time value something like
case when MyTime >= '00:00' and MyTime < '08:10' the Dateadd(day, 1, date)
This works perfect but my concern is that I cannot put the value 08:10 because it will not a constant value for all rows.
I have provided a screenshot of my data and a expected date column for reference.
I know 2008 MS SQL Server has a timestamp data type that adds date and time when the rows are inserted. Is there a way to automatically update the date and time when the rows are updated?
I have a table containing following Columns plus other columns: Start Date 04-01-2002(mm-dd-yyyy) varchar(10) Start Time column 09:30:30(hh:mm:ss) varchar(8) End Date 04-05-2002(mm-dd-yyyy) varchar(10) End Time column 09:45:30(hh:mm:ss) varchar(8)
1]We want to create a view for Report Users So that they could just select the other columns and difference in start Date/time to End Date /Time. 2]The Difference in time should be based on Business Day. i.e. Saturday & Sunday should be excluded. & 3]Each business day is defined as 8.00 am to 5 .00pm ( 1 Business day = 9 hrs) 4]The time is stored as 24 hour clock.
For e.g. Start Date Start TimeEnd Date End Time Desired Output 04/01/2002 10:00:0004/01/2002 3:30:00 0 Days 5:30 Hrs 04/01/2002 16:00:0004/02/2002 10:45:00 0 Days 3:45 Hrs 04/01/2002 09:00:0004/03/2002 10:45:00 2 Days 1:45 Hrs 04/05/2002 16:30:0004/08/2002 9:45:00 0 Days 2:15 Hrs****** ******In last case as 04/06/2002 & 04/07/2002 were Saturday & Sunday ,hence excluded.So the difference of time is just 2:15 hrs
Can you please give me a suggestion/pseudo code/logic on how I might do this? We cannot use cursors as we want to make a view.This is for SQL Server 2000
Hi, I have a BETWEEN query (at least I think that's what it will need), but with a difference.
Normally you would specific a field which was BETWEEN two set variables
ie. {fieldname} BETWEEN 1 AND 3
However I need mine the other way round.
I have a series of records which have a startdate and enddate held against them.
When a user submits a new record to the db, I need it to check that the starting and ending date range doesn't overlap any of the existing start-end date ranges that exist.
In order to do that I'm trying to build a query which takes in the incoming startdate variable and see if that is within any of the existing start-date-enddate dates ranges of the existing records, and then same for the incoming endate. I actually want the ones that are going to cause a problem to appear...
I;m sure there is a pretty easy way of coding this, but I'm struggling to get my head round it.
how do you determine the date difference between the current record and the next record. i want to use the enddate on record 1 and calulate the time it took the next occurance to happen (start time) on record 2? and so forth.
Disable logins on access expiry date(Not windows password expiry). we grant access to users on databases only for 60 days. So access is only valid for 60 days. Then the user should again request access to the database going thru security clearance. Thn the DBA's enable the login. Maintaining all these logins of users manually is causing more confusion. As we know, we dont have any inbuilt functionality to automatically disable logins in SQL Server.
I have a table where the logins and expirydate were recorded in a DB.
Using this table and SQL Server agent. Can i achieve this process automated ? CREATE TABLE [dbo].[LoginsExpiry]( [LoginName] [varchar](50) NULL, [ExpiryDate] [date] NULL, [Roles] [varchar](500) ) ON [PRIMARY] GO
I have a report that shows patient information, such as patient number, name, and admit date. It also has a date parameter. I enter in the date and it shows all the patients admitted that day. I would like to show the last date the patient was admitted before the date I ran the report for.
For example, if I run the report for 12/20/06, I want to see the patients that were admitted that day and their info like name, and patient number. I would also like to see the last date they were admitted.
I have a field called 'LOG_COMMENTS' in a table named T_PRODUCTION_WORK_LOG.
In the 'LOG_COMMENTS' whenever a request is placed on hold comments are added by the application, such as 'Status changed from Open to On Hold' and 'Status changed from On Hold to Open' along with a 'LOG_DATESTAMP' field. A request can go on and off Hold multiple times, how do I determine the days a request is On Hold?
I know I can use the sql function DATEDIFF ( datepart , startdate , enddate ), but how do I account for the possiblity that the request was On Hold more than once? And how would I get LOG_DATESTAMP' times for 'LOG_COMMENTS' that contain 'Status changed from Open to On Hold' and 'Status changed from On Hold to Open''?
I have here a query which delivers me the user data from the last month. The problem what I have is, if employee have more then one rows in this month, they will be also deliverd. But exactly this is not needed. I need only the last record from last month.
SELECT a.FIRMA, a.PSNR, a.FELDNR, a.PFLFDNR, a.INHALT AS FTE, a.PFGLTAB,
As you can see, PSNR=364 has two rows and i need only the row from last month and last date.Maybe we can use Field PFLFDNR as counter. get only one row for every employee?
I have a table VisitLog pkey customer_id pkey user_id visit_date visit_note
Given both id's, I need a report that will list the visit interval. Assuming the rows are entered in ordered. the result should just display a single column with rows filled with date difference. Date Format in example below is dd/MM/yyyy
I'm trying to write a select statement that will show me the total payments, last payment date, and last payment amount for each client. I get results but it is all payments. Can anyone help me with this?
Thank you,
Here is what I have tried:
SELECT dbo.tblClients.Client_ID, Sum(dbo.tblPaymentReceipts.[Amount Paid]) AS SumOfAmtPaid, MAX(dbo.tblPaymentReceipts.[Date]) AS LastPaymentDate, dbo.tblPaymentReceipts.[Amount Paid] INTO #temp_UNPaymentsA FROM dbo.tblPayments INNER JOIN dbo.tblPaymentReceipts ON dbo.tblPayment.Pay_ID = dbo.tblPaymentReceipts.Pay_ID WHERE (dbo.tblPaymentReceipts.[Date] BETWEEN '1/1/2001' AND '12/31/2003') GROUP BY dbo.tblPayments.Pay_ID, dbo.tblPaymentReceipts.[Amount Paid]
Select * FROM #temp_UNPaymentsA GROUP BY Client_ID, SumOfAmtPaid, LastPaymentDate, [Amount Paid] HAVING SUM(SumOfAmtPaid) BETWEEN 0 AND 1000
I have a report that pulls data for the following date parameters:
DECLARE @STARTDATE datetime, @ENDDATE datetime SET @STARTDATE = GETDATE()-3 SET @ENDDATE = GETDATE() Select * from tablename where reportdate between @STARTDATE and @ENDDATE
This query works fine. I wanted to show the parameters chosen in a text box in the report so the user knows the date range the report was run. I tried using the following: "Discharge Date between " & Parameters!STARTDATE.Value & " and " & Parameters!ENDDATE.Value but I am getting a forward dependancy error. I have also tried setting a default value but can not get the correct code to subtract three days from Now()
I am having a table where i have the following columns where the date format is dd/mm/yyyy Purchase Description From_Date To_Date------------------------------- --------------- ----------------Desktop 2/2/2007 2/3/2007Mouse 2/1/2007 28/1/2007Laptop 5/1/2008 15/3/2008Speaker 4/1/2008 21/1/2008 My requirement is i need to create a stored procedure which will look for the from_date and to_date values. If the difference is more than 30 days that record should get deleted automatically. How to write the stored procedure? Please provide me with full stored procedure Thanx in advance