Solved

get current month and year

Posted on 2014-10-01
1
264 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
point in time restore in SQL server 26 41
Need time in SQL 8 29
Need help with another query 10 34
Need some alteration to below mention query 2 10
In this article I will describe the Copy Database Wizard 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.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

679 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