Solved

TSQL, How can I get only the year part?

Posted on 2011-03-18
5
341 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

635 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