Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
ambujnema
Starting Member
11 Posts |
Posted - 2010-06-18 : 12:41:38
|
Hi,I have a problem described belowSet @StartMonth = Aug Set @EndMonth = Jan Set @StartYear = 2009Set @EndYear = 2010I need to insert records into one new table based on the above informationsMonth name should go from StartMonth to EndMonth and Year also should insert like belowMonthName YearAug 2009Sep 2009Oct 2009Nov 2009Dec 2009Jan 20102) when I pass again Set @StartMonth = Dec Set @EndMonth = Feb Set @StartYear = 2009Set @EndYear = 2010It should check for the records already in the database and should not insert the duplicate record. Based on the above case it should insert only one recordMonthName YearFeb 2010Regards,Please suggest me the logic how can we implement in SQL Server 2005. |
|
ambujnema
Starting Member
11 Posts |
Posted - 2010-06-18 : 12:47:48
|
I need to also calculate number of days in the month except sat and sunday ..Like Jan2010 we have 21 days except Saturday and SundayIs their any logic through which we calculate the number of days.? |
|
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2010-06-18 : 20:03:21
|
Here is the basic record set. You should be able to use a NOT EXISTS in your INSERT/SELECT that prevents duplicates. The number of days logic is on you ;-)[CODE]declare @StartMonth char(3) = 'Aug', @EndMonth char(3) = 'Jan', @StartYear int = 2009, @EndYear int = 2010;declare @Months table ( strMonth char(3), intMonth int )insert into @Months (strMonth, intMonth) select 'JAN', 01 union all select 'FEB', 02 union all select 'MAR', 03 union all select 'APR', 04 union all select 'MAY', 05 union all select 'JUN', 06 union all select 'JUL', 07 union all select 'AUG', 08 union all select 'SEP', 09 union all select 'OCT', 10 union all select 'NOV', 11 union all select 'DEC', 12;-------------------------------with Years(MyYear)as ( select @StartYear union all select MyYear + 1 from Years where MyYear < @EndYear )select y.MyYear, m.strMonthfrom @Months mcross join Years ywhere y.MyYear * 100 + m.intMonth between @StartYear * 100 + (select intMonth from @Months where strMonth = @StartMonth) and @EndYear * 100 + (select intMonth from @Months where strMonth = @EndMonth)order by y.MyYear, m.intMonth[/CODE]=======================================A couple of months in the laboratory can save a couple of hours in the library. -Frank H. Westheimer, chemistry professor (1912-2007) |
|
|
ambujnema
Starting Member
11 Posts |
Posted - 2010-06-19 : 05:22:19
|
Hi thanks for the reply ,i am pretty new to sql server please tell me also how to check exsisting records in the table using IF EXSITS ..the number of business day calculation i can take care. |
|
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2010-06-21 : 11:44:16
|
The basic pattern is:insert into MyTableselect ...from SourceTablewhere NOT EXISTS (select * from MyTable where MyTable.Key = SourceTable.Key)In essence the query is saying, "If this record already exists, don't insert it."HTH=======================================A couple of months in the laboratory can save a couple of hours in the library. -Frank H. Westheimer, chemistry professor (1912-2007) |
|
|
|
|
|
|
|