1

I want to select either week days or a full week depending on a parameter.

I was looking at using a case statement to do the following, but I don't know how to convert a string of number to a value that can be passed as integers.

Im probably doing this all wrong but any help would be appreciated.

This is where im setting the param value:

set @days = (select case when FullWeek = 1 then cast('1,2,3,4,5,6,7' as Numeric(38,0))
                        when fullweek = 0 then cast('2, 3,4,5,6' as Numeric(38,0)) end 
            from Reports)

And this is how I want to call this, its part of a where statement:

where datepart(dw,date) in (@days)
2
  • Why not put hardcoded values in a table and usw another column like isWeekday ti identify the case Commented May 8, 2015 at 10:22
  • Ive used a one column reference as its meant to let clients select if they want reports by week days or full week, so made sense just to use one column as he reference and set the code to do the workings out than store the data. Commented May 8, 2015 at 11:11

3 Answers 3

4

Why not simplify it and do it this way:

Where (Fullweek = 1) -- Will get all days of week
   or 
      (Fullweek = 0 and datepart(dw,date) in (2,3,4,5,6))
Sign up to request clarification or add additional context in comments.

2 Comments

Fab this worked brilliantly and so simple in the end, I knew I was probably making it harder for myself. Thanks
No problem, glad it helped you out!
1

This isn't even a problem with sql, it's a conveptual problem. You can't convert such a value to a numeric. What do you expect the Numeric value to be in each case?

You are making a very popular beginer's mistake, when you try to convert a delimited string into an array. What you should do in this case is this:

where datepart(dw,date) in 
    case 
        when FullWeek = 1 then
            (1,2,3,4,5,6,7)
        else -- if fullweek is a bit. otherwise use when fullweek = 0 then
            (1,2,3,4,5,6,7)
        end

2 Comments

I tried the case statement in the where statement originally and I just get a syntax error which is why I then tried using the case in a select parameter. Tried your example and this also gives me a syntax error near the keyword "Case"
Actually, Christian Barron's code is way better then mine (got my vote up). I'm leaving this answer as well because I think that the text matters as much as the code itself. Seems like there is no way to use case instead of int.
0

I would prefer not to do hard-coding of values in a query. I would rather do it this way:

Create a table to categorize days:

  Day    isWeekday   biWeeklyReportDays
  1        0              0
  2        1              1
  3        1              0
  4        1              0
  5        1              1
  6        1              0
  7        0              0

And my query would be something like this.

IF FullWeek = 0
BEGIN
SELECT @days = days FROM theTable where isWeekDay = 1
END
ELSE
BEGIN
SELECT @days = days FROM theTable
END

----
where datepart(dw,date) in @days

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.