SQL syntax for toChar year


I need to add some fields to this sql to break the date field into separate fields for month, day and year. I want hem to be character. I tried  to_char("ESHPOSHD"."PHST_CHG_DATE",'YYYY') as chg_yr It does not like that. I tried to find the correct syntax for this but so far no luck.

SELECT DISTINCT
 "ESYPOSTR"."TITLE",
 "ESYINFOR"."DESCRIPTION",
 "ESYINFOR"."INFO_TYPE",
 "ESHPOSHD"."PHST_CHG_DATE",
 "ESHSHSTD"."SHST_OLD_SALARY",
 "ESHSHSTD"."SHST_OLD_HRLY_RATE",
 "ESHPOSHD"."PHST_DESCRIPTION",
 "ESHPOSHD"."PHST_ID",
 "ESYAPPTR"."CYCLE_CODE",
 "ESYEMPLR"."EMP_NO",
 "ESYAPPTR"."STATUS",
 "ESYEMPLR"."EMPLOYMENT_STATUS",
 "ESYEMPLR"."FNAME",
 "ESYEMPLR"."cur_hire_date",
 "ESYEMPLR"."HIRE_DATE",
 "ESYEMPLR"."TERM_DATE",
 "ESYEMPLR"."HOME_DEPT",
 "ESYEMPLR"."LNAME",
 "ESYEMPLR"."SSN",
 "ESYAPPTR"."SALARY",
 "ESYAPPTR"."HOURLY_RATE",
 "ESYEMPLR"."mname",
 "ESHSHSTD"."SHST_ID",
 "ESHPOSHD"."PHST_OLD_POS_NO",
 to_char("ESHPOSHD"."PHST_CHG_DATE",'YYYY') as chg_yr
 
 
 FROM   (((("EDENLive"."dbo"."ESYEMPLR" "ESYEMPLR"
 LEFT OUTER JOIN "EDENLive"."dbo"."ESYAPPTR" "ESYAPPTR"
 ON "ESYEMPLR"."EMP_ID"="ESYAPPTR"."EMP_ID")
 LEFT OUTER JOIN "EDENLive"."dbo"."ESYINFOR" "ESYINFOR"
 ON "ESYEMPLR"."EMPLOYMENT_STATUS"="ESYINFOR"."INFO_KEY")
 LEFT OUTER JOIN "EDENLive"."dbo"."ESHPOSHD" "ESHPOSHD"
 ON "ESYAPPTR"."EMP_ID"="ESHPOSHD"."PHST_EMP_ID")
 LEFT OUTER JOIN "EDENLive"."dbo"."ESYPOSTR" "ESYPOSTR"
 ON "ESYAPPTR"."POS_ID"="ESYPOSTR"."POS_ID")
 LEFT OUTER JOIN "EDENLive"."dbo"."ESHSHSTD" "ESHSHSTD"
 ON "ESHPOSHD"."PHST_EMP_ID"="ESHSHSTD"."SHST_EMP_ID"
 
 
 WHERE  "ESYAPPTR"."CYCLE_CODE"='b'
 AND "ESYEMPLR"."cur_hire_date"<{ts '2010-09-30 00:00:01'}
 AND "ESYEMPLR"."EMP_NO" LIKE 'E%'
 AND "ESYINFOR"."INFO_TYPE"='es'
qbjgqbjgConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee SavidgeCommented:
cast(ESHPOSHD.PHST_CHG_DATE as nvarchar(4))

Lee
0
qbjgqbjgConsultantAuthor Commented:
Will that give me the year? I need to name it. How about month and day?
0
bchoorCommented:
YEAR: CAST(DATEPART(yy, ESHPOSHD.PHST_CHG_DATE) AS varchar(4))
MONTH: CAST(DATEPART(m, ESHPOSHD.PHST_CHG_DATE) AS varchar(2))
DAY: CAST(DATEPART(d, ESHPOSHD.PHST_CHG_DATE) AS varchar(2))

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

qbjgqbjgConsultantAuthor Commented:
I need the number '2010' as character format, but I need it to be 2010, not letters. I tried your suggestion it gave me Nov, Dec, etc. For a date such as 01/20/2010, I need 3 fields, with 01 20 and 2010.
0
bchoorCommented:
did u try the datepart function? without the cast - you'll just get the numbers. Unless I'm not understanding what you're looking for.
0
bchoorCommented:
when I do this:
PRINT CAST(DATEPART(yy, GETDATE()) AS varchar(4))
PRINT CAST(DATEPART(m, GETDATE()) AS varchar(2))
PRINT CAST(DATEPART(d, GETDATE()) AS varchar(2))
PRINT ''
PRINT DATEPART(yy, GETDATE())
PRINT DATEPART(m, GETDATE())
PRINT DATEPART(d, GETDATE())

Result:
2010
4
23
 
2010
4
23


the first set of results is varchars, the second set of results is integers
0
qbjgqbjgConsultantAuthor Commented:
Thanks so much
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.