Question of the Week -08/04/2014



Suppose we need to find out the name of all the months in a given year (Year will be given as an input parameter) where first day of the month falls on weekend (Saturday or Sunday). 

So what should be the query/stored procedure to get the above result?

DMCA.com

About vivekjohari

Database Consultant with more than 11.5 years of experience in database designing & programming and DBA related activities.  Had good experience on different databases like SQL Server, MySQL & Oracle, Azure SQL &  Big Data.
This entry was posted in Interview Questions and tagged . Bookmark the permalink.

30 Responses to Question of the Week -08/04/2014

  1. create proc techmixing_8_4_14 @yr as numeric
    as
    SET NOCOUNT ON;
    set datefirst 1 — to set first day as monday

    declare @dt as varchar(8);
    declare @count as numeric=1
    create table #t1 (mn varchar(20)) — Temporary table
    while @count <=12
    begin

    set @dt=convert(varchar(4),@yr ) + isnull(REPLICATE('0',2-len(convert(varchar(2),@count))),'')+convert(varchar(2),@count)+ '01'

    set @count=@count+1
    if datepart(dw,convert(date,@dt,112 )) >=6
    begin
    insert into #t1 (mn) select datepart(MM,@dt)
    end

    end
    select * from #t1

    go

    exec techmixing_8_4_14 2012

  2. admin says:

    Just to little modify the Bharat answer by adding "No count off" in the end.

    Create proc techmixing_8_4_14 @yr as numeric
    as
    begin
    SET NOCOUNT ON;
    set datefirst 1 — to set first day as monday

    declare @dt as varchar(8);
    declare @count as numeric=1
    create table #t1 (mn varchar(20)) — Temporary table
    while @count <=12
    begin

    set @dt=convert(varchar(4),@yr ) + isnull(REPLICATE('0',2-len(convert(varchar(2),@count))),'')+convert(varchar(2),@count)+ '01'

    set @count=@count+1
    if datepart(dw,convert(date,@dt,112 )) >=6
    begin
    insert into #t1 (mn) select datename(MM,@dt)
    end

    end
    select * from #t1
    set nocount off
    end
    go

    exec techmixing_8_4_14 2012

  3. Soumya Joe says:

    Declare @Months Varchar(60)

    Declare @loop int
    SET @loop=1

    WHILE @loop<=12
    BEGIN
    IF(Datename(dw,'2014/'+str(@loop)+'/1') in ('Saturday','Sunday'))
    BEGIN
    SET @Months=COALESCE(@Months+ ', ', '')+ DateName( month , DateAdd( month , @loop , 0 ) -1)
    END
    SET @loop=@loop+1
    END

    Print @Months

  4. Pingback: pressure washers in charlotte nc

  5. Pingback: convert safaricom airtime to mpesa

  6. Pingback: อนิเมะฮาเร็ม

  7. Pingback: vegus

  8. Pingback: How to find 100X 500X Gains in Gold Silver Jr Mining Stocks

  9. Pingback: รีไฟแนนซ์รถมอเตอร์ไซค์

  10. Pingback: http://apartments-parami.pl/bezpieczenstwo-miedzynarodowe-181.php

  11. Pingback: http://amsico.pl/nasladuje-zwierzaki-2-latek-umie-161.php

  12. Pingback: link vao w88

  13. Pingback: w88th

  14. Pingback: Empire Market

  15. Pingback: fun88asia

  16. Pingback: Dream Market

  17. Pingback: Crystal Meth kaufen

  18. Pingback: cbdque

  19. Pingback: Empire Market

  20. Pingback: Tochka Market

  21. Pingback: dang ky v9bet

  22. Pingback: keluaran hkg

  23. Pingback: cdrqq

  24. Pingback: 2020 Cover Economist

  25. Pingback: mega poker99

  26. Pingback: www.1lapakqq.site

  27. Pingback: sbobet888

  28. Pingback: poker 99

Leave a Reply