nokx — 2014-03-28T14:29:43-04:00 — #1
I have a table where I'd like to just pull records that are new that month. I'm not too familiar with SQL functions but I'm assuming there's a way to grab the current month and year within SQL.
So instead of editing the query every month...
SELECT * FROM myTable WHERE DateCreated => '3/1/2014'
I'd like to do something like (making up the SQL functions)...
SELECT * FROM myTable WHERE DateCreated => MAKEDATE(MONTH(NOW()), 1, YEAR(NOW()))
Anything available along these lines? Thanks!
wolfshade — 2014-03-28T14:33:29-04:00 — #2
What flavour SQL? MS? MySQL? Oracle?
nokx — 2014-03-28T14:40:53-04:00 — #3
wolfshade — 2014-03-28T14:46:10-04:00 — #4
Hmm.. well.. it's been a while since I worked MS SQL. I did manage to do this in Oracle, though.
WHERE TRUNC(Table_Date) >= TO_DATE(EXTRACT(YEAR from sysdate) || '-' || EXTRACT(MONTH from sysdate) || '-01','YYYY-MM-DD')
wolfshade — 2014-03-28T14:52:48-04:00 — #5
WHERE Table_Date >= DatePart('yyyy',getDate()) + '-' + DatePart('mm',getDate()) + '-01'
.. just guessing..
wolfshade — 2014-03-28T16:22:16-04:00 — #6
.. if the above doesn't work, then you might need to put everything after the ">=" into a CAST().
r937 — 2014-03-29T10:05:44-04:00 — #7
WHERE datecreated >= DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)