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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Truncate a Year

Status
Not open for further replies.

StacyStacy

Programmer
Apr 16, 2003
60
US
How do I write code to extract the year only from a date field such as:
01/01/2004

Thanks,
 
Use YEAR(yourdate), it returns a numeric value.
Or you could a expression such as LEFT(DTOS(yourdate),4) to get the year as character.
 
How do I write it correctly?

use jt2
select wia_area, entertdte1 from jt2 into table allpart1
LEFT(DTOS(entertdte1)
 
Thank you. Now, let me try to understand what was written, and please, correct me when I'm wrong:

select wia_area, left(dots(entertdte1),4) from table:

select wia_area field,entertdte1 field starting from the left and only picking up the numbers after 4 characters from the beginning of the date to a string, thus getting 2004 from 01//01/2004. The 2 in 2004 is considered the 4th character in the date: 01/01/2004? Correct.
Thanks so much! ;)
 
You are welcome. Dtos() function returns a string in the format YYYYMMDD from a date expression, and left() function takes the first four characters of that string.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top