Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Question about the YEAR Function

Author  Topic 

TWD38
Starting Member

7 Posts

Posted - 2012-05-10 : 11:59:32
SQL is telling me that my use of the YEAR function is not valid.
Can anyone tell me why? How can I query for multiple years?

Here is my query:



SELECT a.RECIP_SSN_NBR,

char(b.recip_retir_dt, USA),
sum(a.OBLIG_TOT_AMT) AS TOTAL
year(a.OBLIG_CREAT_DT)


FROM DSNP.PR01_T_OBLIG_CMPNT a,
DSNP.PR01_T_RECIP_SYS b



WHERE a.RECIP_SSN_NBR=b.RECIP_SSN_NBR




AND a.CMPNT_TYPE_CD = '42'
AND YEAR(a.OBLIG_CREAT_DT) IN (2009, 2010, 2011)
and b.benef_stat_cd in ('AC', 'DP')
and b.recip_retir_dt2 is NULL
and b.benef_seq_nbr = 1


GROUP BY

a.RECIP_SSN_NBR,

b.recip_retir_dt,
year(a.OBLIG_CREAT_DT)



The query works if I use = to a specific year, but with multiple years.

TWD38
Starting Member

7 Posts

Posted - 2012-05-10 : 12:01:39
Disregard. I'm an idiot. I just noticed the missing comma which was generating the complaint in my SELECT statement.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-10 : 12:03:23
quote:
Originally posted by TWD38

SQL is telling me that my use of the YEAR function is not valid.
Can anyone tell me why? How can I query for multiple years?

Here is my query:



SELECT a.RECIP_SSN_NBR,

char(b.recip_retir_dt, USA),
sum(a.OBLIG_TOT_AMT) AS TOTAL
year(a.OBLIG_CREAT_DT)


FROM DSNP.PR01_T_OBLIG_CMPNT a,
DSNP.PR01_T_RECIP_SYS b



WHERE a.RECIP_SSN_NBR=b.RECIP_SSN_NBR




AND a.CMPNT_TYPE_CD = '42'
AND YEAR(a.OBLIG_CREAT_DT) IN (2009, 2010, 2011)
and b.benef_stat_cd in ('AC', 'DP')
and b.recip_retir_dt2 is NULL
and b.benef_seq_nbr = 1


GROUP BY

a.RECIP_SSN_NBR,

b.recip_retir_dt,
year(a.OBLIG_CREAT_DT)



The query works if I use = to a specific year, but with multiple years.



is this t-sql?
highlighted doesnt seems to be inline with t-sql syntax
if you're not using MS SQL Server please post this in relevant forum at www.dbforums.com
This forum is specifically for SQL Server

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -