Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Extracting data based on the current date

$
0
0
Hi All,I have a requirement as below. One of my job runs every 9th calendar of every month. I have to extract the data as below.For example: I am running the job on 9th Jan 2015 - Based on the current date, need to extract the previous month (Dec 2015). If the previous month is December then it should extract data from 1st Jan 2015 to 31 Dec 2015. (Provided sample data for 2 months-Jan and Feb 2015)If the job is running on 9th March 2015 (previous month - Feb 2015) then it should extract data as below. From 01Jan 2015 to 27Feb2015. Note: Based on the month_id from below table, Feb ending is 27th Feb 2015. 28th Feb 2015 falls into March 2015[b]Can any one help me how to write the logic for this?[/b]Below are the sample data.CREATE TABLE temp_calendar( [day_id] [int] NOT NULL, [day_desc] [varchar](60) NOT NULL, [month_id] [int] NOT NULL, [month_desc] [varchar](60) NOT NULL, [year_id] [int] NOT NULL, [year_desc] [varchar](60) NOT NULL )insert into temp_calendar( [day_id],[day_desc],[month_id],[month_desc],[year_id],[year_desc] ) select '10958','Jan 01 2015','201501','Jan 2015','2015','2015' UNION ALL select '10959','Jan 02 2015','201501','Jan 2015','2015','2015' UNION ALL select '10960','Jan 03 2015','201501','Jan 2015','2015','2015' UNION ALL select '10961','Jan 04 2015','201501','Jan 2015','2015','2015' UNION ALL select '10962','Jan 05 2015','201501','Jan 2015','2015','2015' UNION ALL select '10963','Jan 06 2015','201501','Jan 2015','2015','2015' UNION ALL select '10964','Jan 07 2015','201501','Jan 2015','2015','2015' UNION ALL select '10965','Jan 08 2015','201501','Jan 2015','2015','2015' UNION ALL select '10966','Jan 09 2015','201501','Jan 2015','2015','2015' UNION ALL select '10967','Jan 10 2015','201501','Jan 2015','2015','2015' UNION ALL select '10968','Jan 11 2015','201501','Jan 2015','2015','2015' UNION ALL select '10969','Jan 12 2015','201501','Jan 2015','2015','2015' UNION ALL select '10970','Jan 13 2015','201501','Jan 2015','2015','2015' UNION ALL select '10971','Jan 14 2015','201501','Jan 2015','2015','2015' UNION ALL select '10972','Jan 15 2015','201501','Jan 2015','2015','2015' UNION ALL select '10973','Jan 16 2015','201501','Jan 2015','2015','2015' UNION ALL select '10974','Jan 17 2015','201501','Jan 2015','2015','2015' UNION ALL select '10975','Jan 18 2015','201501','Jan 2015','2015','2015' UNION ALL select '10976','Jan 19 2015','201501','Jan 2015','2015','2015' UNION ALL select '10977','Jan 20 2015','201501','Jan 2015','2015','2015' UNION ALL select '10978','Jan 21 2015','201501','Jan 2015','2015','2015' UNION ALL select '10979','Jan 22 2015','201501','Jan 2015','2015','2015' UNION ALL select '10980','Jan 23 2015','201501','Jan 2015','2015','2015' UNION ALL select '10981','Jan 24 2015','201501','Jan 2015','2015','2015' UNION ALL select '10982','Jan 25 2015','201501','Jan 2015','2015','2015' UNION ALL select '10983','Jan 26 2015','201501','Jan 2015','2015','2015' UNION ALL select '10984','Jan 27 2015','201501','Jan 2015','2015','2015' UNION ALL select '10985','Jan 28 2015','201501','Jan 2015','2015','2015' UNION ALL select '10986','Jan 29 2015','201501','Jan 2015','2015','2015' UNION ALL select '10987','Jan 30 2015','201501','Jan 2015','2015','2015' UNION ALL select '10988','Jan 31 2015','201502','Feb 2015','2015','2015' UNION ALL select '10989','Feb 01 2015','201502','Feb 2015','2015','2015' UNION ALL select '10990','Feb 02 2015','201502','Feb 2015','2015','2015' UNION ALL select '10991','Feb 03 2015','201502','Feb 2015','2015','2015' UNION ALL select '10992','Feb 04 2015','201502','Feb 2015','2015','2015' UNION ALL select '10993','Feb 05 2015','201502','Feb 2015','2015','2015' UNION ALL select '10994','Feb 06 2015','201502','Feb 2015','2015','2015' UNION ALL select '10995','Feb 07 2015','201502','Feb 2015','2015','2015' UNION ALL select '10996','Feb 08 2015','201502','Feb 2015','2015','2015' UNION ALL select '10997','Feb 09 2015','201502','Feb 2015','2015','2015' UNION ALL select '10998','Feb 10 2015','201502','Feb 2015','2015','2015' UNION ALL select '10999','Feb 11 2015','201502','Feb 2015','2015','2015' UNION ALL select '11000','Feb 12 2015','201502','Feb 2015','2015','2015' UNION ALL select '11001','Feb 13 2015','201502','Feb 2015','2015','2015' UNION ALL select '11002','Feb 14 2015','201502','Feb 2015','2015','2015' UNION ALL select '11003','Feb 15 2015','201502','Feb 2015','2015','2015' UNION ALL select '11004','Feb 16 2015','201502','Feb 2015','2015','2015' UNION ALL select '11005','Feb 17 2015','201502','Feb 2015','2015','2015' UNION ALL select '11006','Feb 18 2015','201502','Feb 2015','2015','2015' UNION ALL select '11007','Feb 19 2015','201502','Feb 2015','2015','2015' UNION ALL select '11008','Feb 20 2015','201502','Feb 2015','2015','2015' UNION ALL select '11009','Feb 21 2015','201502','Feb 2015','2015','2015' UNION ALL select '11010','Feb 22 2015','201502','Feb 2015','2015','2015' UNION ALL select '11011','Feb 23 2015','201502','Feb 2015','2015','2015' UNION ALL select '11012','Feb 24 2015','201502','Feb 2015','2015','2015' UNION ALL select '11013','Feb 25 2015','201502','Feb 2015','2015','2015' UNION ALL select '11014','Feb 26 2015','201502','Feb 2015','2015','2015' UNION ALL select '11015','Feb 27 2015','201502','Feb 2015','2015','2015' UNION ALLselect '11016','Feb 28 2015','201503','Mar 2015','2015','2015'

Viewing all articles
Browse latest Browse all 3145

Trending Articles