This is the code I have
SELECT headLine, message, datePosted
FROM (SELECT *
FROM announcements
WHERE DATEPART(yyyy, datePosted) = 2004)
The goal here is to return headLine, message, and datePosted. datePosted is a Date/Time field. I want to be able to send in a year to the function containing this statement and have it return only those 3 parts of an announcement from the given year. For some reason this is just not working for me. Am I using DATEPART wrong, because if I remove it and instead select all records it works but once I start to get picky it returns nothing on me. Any ideas? Thanks in advance!
I don’t know what SQl you are using so I would go for the brute force technique:
SELECT headLine, message, datePosted, DATEPART(yyyy, datePosted)
FROM announcements
look at the output and work out what the DATEPART function is returning.
Also, why the sub-select?
I figured out the problem. On the MSDN site it says the syntax is DATEPART(part, date). Well the correct syntax is DATEPART(‘part’, date). Problem solved! Thanks don’t ask
That’s what I was wondering.
As the above peeps were asking, why not just
SELECT headLine, message, datePosted *
FROM announcements
WHERE DATEPART(‘yyyy’, datePosted) = 2004
Unless you were showing us a simplification of your actual SQL code, the above is much more efficient and easily read.
darn, that ‘*’ should not have appeared next to the word datePosted please ignore it.
I’m not quite why the sub-select is there is actually. I’m working on updating a website and that select statement was already there for me from the last webmaster. I’ve since simplified it.