Solved

get current month and year

Posted on 2014-10-01
1
255 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:
ScottPletcher 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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…

919 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now