Solved

TSQL, How can I get only the year part?

Posted on 2011-03-18
5
339 Views
Last Modified: 2012-05-11
I have a column type as nvarchar(4) null which I converted to DateTime.  It originally has the 4 digits of year.  After changing to DateTime type, the data now has a 1/1 as the month and date and 12;00 am as the time.  How can I change it to only the year part?  thanks.

This is what I used to change the column type
ALTER TABLE BkPersonDegree ALTER COLUMN degree_Date DateTime
0
Comment
Question by:lapucca
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 250 total points
ID: 35168867


select DATEPART(YEAR, CONVERT(DATETIME, COLUMN, 101))
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35168886
datetime is simply not only year, so if you only want the year, why change to datetime?
please clarify
0
 

Author Comment

by:lapucca
ID: 35168982
Ang, I thought it would be easier to use for comparing in sql  but I think you're right.  That shouldn't make any difference when I query for certain year for cmoparring this column data in a query?
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 35169019
indeed. if all you need for the column is the year, then use INT datatype actually and not even varchar.
with INT, you can even do maths without having to cast the data type.
0
 

Author Closing Comment

by:lapucca
ID: 35195761
Thank you.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question