Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Trying to get a count of how long an item is open on a weekly basis. So if it opens and closes I want to know how long it was open. If close date is null I want to use GetDate()

SQL
CREATE TABLE [dbo].[TestDates](
    [ItemId] [int] IDENTITY(1,1) NOT NULL,
    [AssignmentName] [nvarchar](max) NULL,
    [AssignedDate] [date] NULL,
    [DueDate] [date] NULL,
    [PId] [int] NULL,
    [FullName] [nvarchar](max) NULL,
    [OpenDate] [date] NULL,
    [CloseDate] [date] NULL,
    [Status] [nvarchar](max) NULL,
 CONSTRAINT [PK_TestDates] PRIMARY KEY CLUSTERED
(
    [ItemId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO



SQL
CREATE TABLE [dbo].[TestDates](
    [ItemId] [int] IDENTITY(1,1) NOT NULL,
    [AssignmentName] [nvarchar](max) NULL,
    [AssignedDate] [date] NULL,
    [DueDate] [date] NULL,
    [PId] [int] NULL,
    [FullName] [nvarchar](max) NULL,
    [OpenDate] [date] NULL,
    [CloseDate] [date] NULL,
    [Status] [nvarchar](max) NULL,
 CONSTRAINT [PK_TestDates] PRIMARY KEY CLUSTERED
(
    [ItemId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO
Posted
Comments
Sergey Alexandrovich Kryukov 13-Mar-13 18:53pm    
Sorry, this is not a question.
—SA

1 solution

You did not specify the unit of measure for the count. I used MINUTE in the example below. Using DATEDIFF, you can also specify other units of measure. See DATEDIFF (Transact-SQL)[^]
SELECT ItemId,ISNULL(AssignmentName,"") As AssignmentName,DATEDIFF(MINUTE,ISNULL(opendate,GETDATE()),ISNULL(closedate,GETDATE())) AS Minutes_Elapsed FROM testdates;


Using constants to test, the result was 90.
SELECT DATEDIFF(minute,ISNULL('2012-01-01 08:00:00',GETDATE()),ISNULL('2012-01-01 09:30:00',GETDATE())) As Minutes_Elapsed
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900