Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL: Get records with one spesific day number (date = 10th or 20th)

Posted on 2011-02-15
4
Medium Priority
?
507 Views
Last Modified: 2012-06-22
I have a table containing users. Each user contains a smalldatetime field called Created.

In my stored Procedure (spGetUsers) I have 2 input parameters: @Day, @LastDayOfMonth. The @Day parameter can be integers from 1 to 31, the @LastDayOfMonth is '0' or '1'.

I need to return all users based on what day number the Created fields is.

Example: I want to return all users where Created day is 10th. and Status='1'.  I don't care about the month or year.

There is also another issue. If the Parameter called @LastDayOfMonth (bit) is "1" the procedure shall return all users where Created day >= @Day AND Status = '1'.

Thanks for all tips :)
0
Comment
Question by:webressurs
  • 2
4 Comments
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 total points
ID: 34895481
you can add this in Store procedure


if ( @LastDayOfMonth = 1)
Begin
Select * from users where Day(Created) >= @Day
end
else
Begin

Select * from users where Day(Created) = @Day
end
0
 
LVL 13

Assisted Solution

by:devlab2012
devlab2012 earned 500 total points
ID: 34897562
One correction to above comment:

if (@LastDayOfMonth = 1)
Begin
     Select * from users where Day(Created) >= @Day AND Status = 1
end
else
Begin
    Select * from users where Day(Created) = @Day AND Status = 1
end
0
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 1000 total points
ID: 34901904
You can do like this.
SELECT * FROM users 
 WHERE (@LastDayOfMonth = 1 AND [Status] = 1 AND DAY(Created) >= @Day) 
        OR (@LastDayOfMonth = 0 AND [Status] = 1 AND DAY(Created) = @Day)

Open in new window

0
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 1000 total points
ID: 34901911
or using DATEPART
SELECT * FROM users 
 WHERE (@LastDayOfMonth = 1 AND [Status] = 1 AND datepart(day,Created) >= @Day) 
        OR (@LastDayOfMonth = 0 AND [Status] = 1 AND datepart(day,Created) = @Day)

Open in new window

0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question