Using variables and logic inside a SELECT

  • Hi All,

    I guess it is row processing logic I'm after, where the returned value is calculated on the fly. I'll be counting up instances of each weekday type. What is important is that each date is a month, and for that month I want to parse the days and count those daytypes.

    The output will be:

    Month, Weekday, DayCount

    2010-01-01, Friday, 5

    2010-01-01, Saturday, 5

    2010-01-01, Sunday, 5

    ...

    And so there'll be 7 rows per month.

    Anyway conceptually, I wanted to see if you can do something like this ? Inserting code where a column selection would go. It appears you can't, so I wonder how else to attack this problem ?

    ---

    SELECT month, weekday,

    BEGIN

    DECLARE @i INT = 0, @dte DATETIME

    WHILE @i < 32

    IF ISDATE(STUFF(@dte, 9,2,@i)

    IF ISDATE(@dte)

    /* link count of daytypes to "weekday" here, or something...*/

    @i = @i + 1

    END

    FROM Table

    ---

  • Most likely, you don't need looping in what you are trying to achieve!

    If you would post your question in line with the forum etiquette, I believe you will get relevant help in no time.

    Please follow the link at the bottom of my signature...

    If you are too lazy to do the above, I can advice you to search for "quirky update" article by Jeff Moden on this site. Looks like it's relevant to your problem.

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Thanks, I sorted it out myself - sorry to waste your time.

    Regards, Greg.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply