Monday, February 25, 2013

SQL BLOG: Calculating total number of days from 1st Jan without using DATEDIFF function

Below script will calculate total number of days based on the input from user from 1st Jan. Though we have a built in function DATEDIFF from which we can calculate this easily. But in the below script, i have tried calculating the days without this built in function.

I have used Scalar UDF for calculating remaining days in the month. Script for the same can be found in my first article.

/* Calculate total number of days from 1st Jan */

declare @fresult integer
declare @tdate date
declare @mdate integer
declare @counter integer
declare @intermidate_result integer
set @tdate = '2012-12-31'
set @counter = 1
set @mdate = MONTH(@tdate)
set @intermidate_result = 0
set @fresult = 0
while @counter < @mdate
begin
/* below function will give last month remaining days */
set @fresult = dbo.udf_calculate_remaining_days(DATEADD(mm,-@counter,@tdate))
/* will give total numbers of days minus current month */
set @intermidate_result = DATEPART(dd,dateadd(mm,-@counter,@tdate))+ @fresult+@intermidate_result
set @counter = @counter + 1
end
declare @current_date integer
set @current_date = DAY(@tdate)
set @intermidate_result = @intermidate_result + @current_date
select 'Total Number Of Days from :'+'2012-01-01'+'  '+'To'+'  '+CAST(@tdate as varchar(10))+'  '+'is:'+'  '+CAST(@intermidate_result as varchar(4))





Sample Output:
Total Number Of Days from :2012-01-01  To  2012-12-31  is:  366

Sunday, February 24, 2013

SQL BLOG: Calculating number of week along the days completed in that week for a given input date

Below code will display the week started along with the number of days completed in that week. For deriving this have used the modulo function.

Week-4 for this code starts from 22nd till the last day of the month, that's why week-4 remaining days are calculated by dividing with 21



/* CODE STARTS BELOW: */

/* FOR A SELECTIVE DATE, WILL GIVE THE WEEK COUNT ALONG WITH THE DAYS COMPLETED IN THAT WEEK*/

declare @tdate datetime
set @tdate = '2012-02-29'
SELECT @tdate as Todays_Date,
CASE WHEN ((DATEPART(DD,@tdate))/7) = 0 THEN 'WK-1'
     WHEN ((DATEPART(DD,@tdate))/7) = 1 THEN 'WK-2'
     WHEN ((DATEPART(DD,@tdate))/7) = 2 THEN 'WK-3'
     ELSE 'WK-4'
     END WEEK_COUNT
,CASE WHEN ((DATEPART(DD,@tdate))/7) = 0 THEN ((DATEPART(DD,@tdate))%7)
     WHEN ((DATEPART(DD,@tdate))/7) = 1 THEN ((DATEPART(DD,@tdate))%7)
     WHEN ((DATEPART(DD,@tdate))/7) = 2 THEN ((DATEPART(DD,@tdate))%7)
     ELSE ((DATEPART(DD,@tdate))%21)
     END Number_Of_Days_In_Week    

Sample Output:
Todays Date                          Week_Count      Number_Of_Days_In_Week
2012-02-29 00:00:00.000         WK-4                               8

Pls Note:    many  more conditions and logics can be applied to encounter different scenarios, which i will be coming up in my future scripts.

SQL BLOG: Playing with Date Time

Below code returns remaining days in the month based on the input date. We can have this inside a function also which will return the integer value.


/* code starts here */

/* calculate remaining days in the month from the given input */
declare @tdate datetime
set @tdate = '2011-02-10'
select (CAST(DATEPART(DD,@tdate) as varchar(4))+'-'+CAST(DATEPART(MM,@tdate) as varchar(4))+'-'+CAST(DATEPART(YYYY,@tdate) as varchar(4))) As Today
,case when DATEPART(MM,@tdate) in (1,3,5,7,8,10,12)
     then (31-DATEPART(DD,@tdate))
     when DATEPART(MM,@tdate) in (4,6,9,11)
     then (30-DATEPART(DD,@tdate))
     when (DATEPART(MM,@tdate) = 2) and ((DATEPART(YYYY,@tdate) % 4) = 0)
     then (29-DATEPART(DD,@tdate))
     else  (28-DATEPART(DD,@tdate))
     end as Month_Remaining_Days

Run the above code in the query editor. Sample output is:


Today                       Months_Remaining_Days
2011-02-10               18