Solved

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

Posted on 2011-02-15
4
460 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 125 total points
Comment Utility
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 125 total points
Comment Utility
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 40

Assisted Solution

by:Sharath
Sharath earned 250 total points
Comment Utility
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 40

Assisted Solution

by:Sharath
Sharath earned 250 total points
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now