Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using datepart to combine dates

Status
Not open for further replies.

thorny00

Programmer
Feb 20, 2003
122
US
I have a date/time field that contains the date in yyyy-mm-dd hh:mm:ss format. I'm trying to just strip off the year and month and combine into a new field.
example:
DATEPART(MONTH,dbo.SERVICE.YMDEFF)+ DATEPART(YEAR,dbo.SERVICE.YMDEFF) AS NEWEFF

but this is summing the month and year
month is 07 and year is 2002 the result is 2009

I need 200207

All help is greatly appreciated!!
 
this gives you almost what you want (the month part is missing the zero on jan to sept)

select convert(varchar,DATEPART(year,dbo.SERVICE.YMDEFF))+ convert(varchar,DATEPART(month,dbo.SERVICE.YMDEFF)) AS NEWEFF

will try and get the exact format you want, though if poss this would probably better be done at the client end
 
Looks like James has beaten me with a better way! (Did try that way but couldnt find the right date value)
 
James,

This works great!! Another question, what happens if I have two different date fields that I'm trying to do this to??

example
dbo.SERVICE.YMDEFF, convert (varchar(6), NEWEFF,112),
dbo.SERVICE.YMDPAID, convert (varchar(6), NEWPAID,112),

I'm getting an 'invalid column name' for neweff and newpaid

Thanks!!
 
I'm not sure I understand. If I'm right you have a table with two datetime columns and you want to display each column just as yyyymm format? If so then this should work:

Code:
SELECT CONVERT(varchar(6), DateCol1, 112), CONVERT(varchar(6), DateCol2, 112)
FROM Table1

If not, can you explain exactly what result you want?

--James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top