Solved

TSQL, How can I get only the year part?

Posted on 2011-03-18
5
334 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
Comment Utility


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

Expert Comment

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

Author Comment

by:lapucca
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

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 article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

11 Experts available now in Live!

Get 1:1 Help Now