Question Locked

Performing a year comparison within SQL Server

0 helpful
2 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hello

I'm trying to do a comparison of a full date field to a year field using:

min(SUBSTRING(CONVERT(CHAR,ISNULL(me.e_start,GETDATE()),110),7,4)) "Start_Year"

to convert the date field in to a year, I then want to compare this converted field to a current year view that I have created.

e.g 19/06/2007 will convert to 2007

me.start=(SELECT AC_YR from current_ac_yr)

basically I want to know everybody who's e_start date falls in to the current year 2007

When I run it i keep getting error:

Conversion failed when converting datetime from character string.

Can I actually do a comparison like this?

Can anybody help?

2 replies

by tony hopkinson

select DatePart(year,e_start) From Table

select DatePart(year,IsNull(e_start,GetDate()) From Table

Declare
@Start int
select @Start = Min(DatePart(year,e_start)) From Table
Where e_date is not null

Select * From Table
where DatePart(year,e_start) = @Start
Or e_start is Null

HTHs

This discussion is closed to new replies.