Link to home
Start Free TrialLog in
Avatar of whc4
whc4Flag for United States of America

asked on

Output SQL Date-type value as a string (year only)

I have the below SQL code.  The DocDate field in the database is of type datetime.  I need the data in the DocDate field to be output as a string, and specificaly I want to strip away the time as well as the month and day...I only need the year.  I am using this with an asp.net application, where I am using the output of the database to build a filesystem path and one level of the path consists of folders that are named by year (e.g. 1998, 2003, etc.)

However, I'd still like the "ORDER BY req.DocDate" portion of the code to order the results by the acutal datetime informaiton contained in the database.

Using MS SQL Server 2008 & MS Visual Studio 2008.

Thanks in advance!
SELECT req.DocumentTitle, req.Author, req.Organization, req.Description, req.DocDate, req.FollowUp, resp.DocumentID, resp.Organization, resp.DocumentTitle, resp.FileName, resp.FileType
FROM eog_docdb.dbo.Document req
LEFT OUTER JOIN (eog_docdb.dbo.Document_Responder d_r
INNER JOIN eog_docdb.dbo.Document resp
ON resp.DocumentID = d_r.ResponseDocID)
ON req.DocumentID = d_r.DocumentID
WHERE req.CategoryName = 'Information'
AND req.CategorySub1Name = 'Information Bulletins'
AND req.CategorySub2Name IS NULL
ORDER BY req.DocDate;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
use year function to get the year part from datetime.
try this

SELECT year(req.DocDate) Year_DocDate, req.DocumentTitle, req.Author, req.Organization, req.Description, req.DocDate, req.FollowUp, resp.DocumentID, resp.Organization, resp.DocumentTitle, resp.FileName, resp.FileType
FROM eog_docdb.dbo.Document req
LEFT OUTER JOIN (eog_docdb.dbo.Document_Responder d_r
INNER JOIN eog_docdb.dbo.Document resp
ON resp.DocumentID = d_r.ResponseDocID)
ON req.DocumentID = d_r.DocumentID
WHERE req.CategoryName = 'Information'
AND req.CategorySub1Name = 'Information Bulletins'
AND req.CategorySub2Name IS NULL
ORDER BY req.DocDate;
Hey whc4,


try this
SELECT req.DocumentTitle, req.Author, req.Organization, req.Description, DATEPART(year,req.DocDate) as DocYear, req.FollowUp, resp.DocumentID, resp.Organization, resp.DocumentTitle, resp.FileName, resp.FileType
FROM eog_docdb.dbo.Document req
LEFT OUTER JOIN (eog_docdb.dbo.Document_Responder d_r
INNER JOIN eog_docdb.dbo.Document resp
ON resp.DocumentID = d_r.ResponseDocID)
ON req.DocumentID = d_r.DocumentID
WHERE req.CategoryName = 'Information'
AND req.CategorySub1Name = 'Information Bulletins'
AND req.CategorySub2Name IS NULL
ORDER BY req.DocDate;
 

Open in new window

Just a reminder to all that have posted, the requirements from the author:
"I need the data in the DocDate field to be output as a string"
Hey whc4,

try this for a string output;
SELECT req.DocumentTitle, req.Author, req.Organization, req.Description, CONVERT(VARCHAR(4),DATEPART(year,req.DocDate)) as DocYear, req.FollowUp, resp.DocumentID, resp.Organization, resp.DocumentTitle, resp.FileName, resp.FileType
FROM eog_docdb.dbo.Document req
LEFT OUTER JOIN (eog_docdb.dbo.Document_Responder d_r
INNER JOIN eog_docdb.dbo.Document resp
ON resp.DocumentID = d_r.ResponseDocID)
ON req.DocumentID = d_r.DocumentID
WHERE req.CategoryName = 'Information'
AND req.CategorySub1Name = 'Information Bulletins'
AND req.CategorySub2Name IS NULL
ORDER BY req.DocDate;
 

Open in new window

Avatar of whc4

ASKER

Thanks!  Exactly what I needed.
Avatar of matthewssl
matthewssl

Cast(year(req.DocDate) as varchar) as DocYear

Might be a little less messy
>>Might be a little less messy<<
To put it mildly, at least it is better then CONVERT(VARCHAR(4),DATEPART(year,req.DocDate))  :)