locked
How to find-out 2nd and 4th Saturdays of Month in SQL 2008 R2 (SP) RRS feed

  • Question

  • User-582711651 posted

    Dear Experts, 

    How to find out whether Date is 2nd or 4th Saturdays, 

    for examples, 
    
    DECLARE @dayNumber INT, @i_Attendance_Day date
    Set @i_Attendance_Day = '2019/06/08'
    
    SET @dayNumber = DATEPART(DW, @i_Attendance_Day)
    
    if  @dayNumber = 14 
    begin
    Select '2nd SAT'
    end
    else if @dayNumber = 28
    begin
    Select '4th SAT'
    end
    

    Wednesday, June 12, 2019 11:22 AM

Answers

  • User-2082239438 posted
    DECLARE @date DATETIME
    
    SET @date = '2019/06/08'
    
    IF DATENAME(DW,@DATE)='Saturday'
    BEGIN	
    	IF DATEPART(DAY,@date)/7=1
    	BEGIN
    		SELECT 'SECOND SATURDAY'
    	END
    	ELSE IF DATEPART(DAY,@date)/7=3
    	BEGIN
    		SELECT 'FOURTH SATURDAY'
    	END
    	ELSE IF DATEPART(DAY,@date)/7=0
    	BEGIN
    		SELECT 'FIRST SATURDAY'
    	END
    	ELSE IF DATEPART(DAY,@date)/7=2
    	BEGIN
    		SELECT 'THIRD SATURDAY'
    	END
    END

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 12, 2019 5:07 PM

All replies