Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-02-15
4
Medium Priority
?
505 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

670 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