Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

TSQL, How can I get only the year part?

Posted on 2011-03-18
5
Medium Priority
?
346 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
  • 2
  • 2
5 Comments
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 1000 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 1000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

963 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