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

get current month and year

Posted on 2014-10-01
1
262 Views
Last Modified: 2014-10-01
Is this correct on how to get current month and year? Prod_date  data is 2014-10-01 00:00:00.000

SELECT PROD_DATE FROM MYTABLE WHERE month(PROD_DATE) = MONTH(GETDATE()) AND year(PROD_DATE) = YEAR(GETDATE())
0
Comment
Question by:VBdotnet2005
1 Comment
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40355444
No, this is the proper technique:

SELECT PROD_DATE
FROM MYTABLE
WHERE
    PROD_DATE >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) AND
    PROD_DATE < DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 1, 0)

You always want to avoid using a function on a table column if at all reasonably possible.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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 we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

856 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