SQL Query Selecting records For the Given Month

Good Day Experts!

I am working on a new task that requires working with dates in a SQL query.  I have multiple user records for an individual. Those records have an entry date on them.  I will take in the passed month from the report tool.  For that month, I need to get all the records for each user.  

I can think of two ways to approach this.  1) Build into the query a way to identify the numerical range of days in a month for that passed in month from the report.  Then go after that range of days, in the date on the record, for the given month grabbing all of the records.  2) Just grab all the records for that user with the passed in month in the month portion of the entry date on the record.

The second one would "assume" that data validation was done on the way in for the date when it was the record was inserted.

What are your thoughts?

If the first one is that way to go, do you know of any references that could help me get started?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark WillsTopic AdvisorCommented:
Better if you pass in month and then within the procedure / job, declare @start and @end of the same datatype as your table and then select rows that comply.

What version of SQL Server ?
Jimbo99999Author Commented:
Mark WillsTopic AdvisorCommented:
Ok, then for example, if we were to pass in an integer value for month (shown below as a variable @passedinmonth), then we can do

  declare @passedinmonth int = 3    -- would not declare here - just showing a parameter for example

  declare @start datetime
  declare @end datetime

  set @start = datefromparts(Year(getdate()),@PassedinMonth,1)
  set @end = datefromparts(Year(getdate()),@PassedinMonth + 1,1)

--select @start , @end

  select <columns>
  from <datasource>
  where mydatecolumn >= @start and mydatecolumn < @end

Open in new window

have a look at datefromparts()  : https://docs.microsoft.com/en-us/sql/t-sql/functions/datefromparts-transact-sql

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jimbo99999Author Commented:
Thank you.  I was not aware of datefromparts.  That certainly cleans things up compare to what I have in my "pseudocode" query.

I will try it after lunch.

Jimbo99999Author Commented:
Thank you for the help...it works great.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.