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
Solved

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

Posted on 2011-02-15
4
486 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
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 125 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 40

Assisted Solution

by:Sharath
Sharath earned 250 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 40

Assisted Solution

by:Sharath
Sharath earned 250 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SYbase 4 36
SubQuery link 4 35
Oracle Query - Convert letters to numbers and display the difference 3 30
sql server query 12 24
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

840 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