Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
CSS
Hi,

Below mentioned is my Query:

SELECT DPSNumber, StatusDateTime FROM DispatchTool

The Resulst is:

DPSNumber   StatusDateTime
123123526   8/4/14 12:00 AM
123123527   8/5/14 12:00 AM
123123528   8/6/14 12:00 AM
123123529   8/7/14 12:00 AM

I want to add an additional column to this result as shown below:

DPSNumber   StatusDateTime  FiscalWeek
123123526   8/4/14 12:00 AM 27
123123527   8/5/14 12:00 AM 27
123123528   8/6/14 12:00 AM 27
123123529   8/7/14 12:00 AM 27

Fiscal Week for my Organization started on January 31, 2014. 'FiscalWeek' Column should display the Fiscal Week Number calculated using the Date-Time mentioned in 'StatusDateTime' column.

Thanks in Advance.
Posted

Hi,
Try this query hopw this will help you.
SQL
--create sample table 
CREATE TABLE [dbo].[DispatchTool](
	[DPSNumber] [int] NOT NULL,
	StatusDateTime Datetime
) ON [PRIMARY]


-- Insert Sample Data 

INSERT INTO [TestTB].[dbo].[DispatchTool]
           ([DPSNumber],[StatusDateTime])
     VALUES
           (123123526     ,     '2014-08-4 12:00:00.000')
INSERT INTO [TestTB].[dbo].[DispatchTool]
           ([DPSNumber],[StatusDateTime])
     VALUES
           (123123527     ,     '2014-08-5 12:00:00.000')
 
 INSERT INTO [TestTB].[dbo].[DispatchTool]
           ([DPSNumber],[StatusDateTime])
     VALUES
           (123123528     ,     '2014-08-6 12:00:00.000')
           
INSERT INTO [TestTB].[dbo].[DispatchTool]
           ([DPSNumber],[StatusDateTime])
     VALUES
           (123123529    ,     '2014-08-7 12:00:00.000')  

-- Your FiscalWeek select query.

Declare @officialFiscalWeek datetime='2014-01-31 12:00:00.000'

select  DPSNumber,
		StatusDateTime,
		DATEDIFF(week,@officialFiscalWeek,StatusDateTime) as FiscalWeek 
		FROM
		DispatchTool
 
Share this answer
 
v3
Hi community,

Below mentioned Query helped me solve the problem (hope this will help someone else here):

SQL
SELECT DPSNumber, StatusDateTime
, Datediff(wk, CONVERT(DATE, 'January 31, 2014'), CONVERT(DATE, StatusDateTime)) FiscalWeek
 
Share this answer
 
Comments
Maciej Los 23-Dec-14 4:32am    
Why to do you repeat solution1. It's rude!
And - by the way - you should rate solution1 and accept as an answer!

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