Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

get current month and year

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
VBdotnet2005
Asked:
VBdotnet2005
1 Solution
 
Scott PletcherSenior DBACommented:
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now