Solved

TSQL, How can I get only the year part?

Posted on 2011-03-18
5
335 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:
ewangoya earned 250 total points
ID: 35168867


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

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 142

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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to extract a number from a MS SQL server string. 39 71
MS Sql query to list a table and join another table though all rows are not there 13 45
Query 14 54
SQL query bug 3 28
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now