Solved

Insert Dates into table

Posted on 2014-12-22
3
85 Views
Last Modified: 2015-03-04
I have a table with three fields Day (Date), Period (int), Period_Week (int)I want to insert data into the table as follows

Starting date - 4th October 2014 to 1st January 2016

Day            Period            Period_Week
04/10/2014      10            4
05/10/2014      10            4
06/10/2014      10            4
07/10/2014      10            4
08/10/2014      10            4
09/10/2014      10            4
10/10/2014      10            4
11/10/2014      11            1

The Period Week runs from Sat to Fri, and there are 4 weeks in a Period, which is why on the 11th October it goes onto Period 11 Week 1.

There are 13 periods in a year and 4 weeks in a period, the last date of period 13 week 4 is 02/01/15
Any help on this would be appreciated
0
Comment
Question by:halifaxman
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
Jesus Rodriguez earned 500 total points
ID: 40513842
run this query on your sql and you will see the result
 SELECT [DY]
      ,CASE PERIOD_WEEK
            WHEN 4 THEN PERIOD*4
			ELSE PERIOD_WEEK+((PERIOD-1)*4) END AS AMT_WEEKS
	  ,DATEADD(WK,CASE PERIOD_WEEK
                  WHEN 4 THEN PERIOD*4
			      ELSE PERIOD_WEEK+((PERIOD-1)*4) END 
				 ,DY) AS [DY FINAL]
	  ,'From ' + CONVERT(VARCHAR(12),DY,100) + ' To '+ CONVERT(VARCHAR(12),DATEADD(WK,CASE PERIOD_WEEK
																			 WHEN 4 THEN PERIOD*4
																			 ELSE PERIOD_WEEK+((PERIOD-1)*4) END 
																			 ,DY),0) as [YOUR FORMAT]
FROM YOUR_TABLE

Open in new window


You can change this CONVERT(VARCHAR(12),DY,100)  by CONVERT(VARCHAR(12),DY,113)  and will print in this format dd Month of yyyy
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 40513946
--specify date range to list
DECLARE @start_date date
DECLARE @end_date date
SET @start_date = '20141004'
SET @end_date = '20160101'

------------------------------------------------------------------------------------------------------------------------

DECLARE @known_period_1_start_date date
--will this always be the first Sat of the year?
--if so, this code can easily be made generic to work for every year
SET @known_period_1_start_date = '20140104'

;WITH
cteTally10 AS (
    SELECT 0 AS tally UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL
    SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0
),
cteTally100 AS (
    SELECT 1 AS tally
    FROM cteTally10 c1
    CROSS JOIN cteTally10 c2
),
cteTally10K AS (
    SELECT ROW_NUMBER() OVER(ORDER BY c1.tally) - 1 AS tally
    FROM cteTally100 c1
    CROSS JOIN cteTally100 c2
)
SELECT
    Period_Day,    
    (total_days_since_start_of_period_1 / 28) % 13 + 1 AS Period,
    (total_days_since_start_of_period_1 / 7) % 4 + 1 AS Period_Week
FROM cteTally10K t
CROSS APPLY (
    SELECT DATEADD(DAY, t.tally, @start_date) AS Period_Day
) AS assign_alias_1
CROSS APPLY (
    SELECT DATEDIFF(DAY, @known_period_1_start_date, Period_Day) AS total_days_since_start_of_period_1
) AS assign_alias_2        
WHERE t.tally BETWEEN 0 AND DATEDIFF(DAY, @start_date, @end_date)
ORDER BY t.tally
0
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 40513963
Also on my comment you can said with will be the First day of week of the SQl to start the qury like this

SET DATEFIRST 6 -- this will set Saturday as the first day for the query
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now