Solved

Tsql Statement

Posted on 2013-01-30
7
234 Views
Last Modified: 2013-02-02
Hi I have this number in one column 06-03253 and need the first 2 digits to be made into a year in the column next to it ie.(06-03253 = 2006) Is this possible? If so how would I go about it?

Thanks

T
0
Comment
Question by:Tech315
7 Comments
 
LVL 3

Accepted Solution

by:
Nalinkumarbalaji earned 500 total points
ID: 38836265
Replace (FieldName,'06-','2006-')

Before replacing check if there is any other values are coming for the select query.

Select Fieldname from tablename where Fieldname like '%06-%'
0
 
LVL 7

Expert Comment

by:Ross Turner
ID: 38836311
Maybe try something like (untested):
Left(RIGHT('20' + CONVERT(VARCHAR,'06-0325310',12),4)
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 38836322
this should work for 2000 and 1900

SELECT CASE WHEN CAST(LEFT(urColumn,2) AS  int) between 0 and 20 then '20' else '19' end + urColumn
where urColumn like '??-%'
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Expert Comment

by:daamonturne
ID: 38836346
use an update staement to o\populate the adjacent field

      update tablename set col2 = 2000 + left(col1,2)

or if selecting try
 
      select col1,2000 + left(col1,2) as col2 ...
0
 
LVL 32

Expert Comment

by:awking00
ID: 38836461
What is currently in the column next to it and what is its datatype? Can there be value like 98-32523 where you would want 1998 in the next column,
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 38837172
You can use a computed column, so you don't even have to store the year value in a column unless you just want to.


ALTER TABLE dbo.tablename
ADD [year] AS CAST(CASE WHEN LEFT(column_name, 2) < '50' THEN '20' ELSE '19' END + LEFT(column_name, 2) AS char(4))


From then on, you can use [year] just like any other column name:


SELECT year, MAX(column_name), ...
FROM dbo.tablename
WHERE
    year BETWEEN '2010' AND '2012'
GROUP BY
    year
ORDER BY
    year


If the computation for that column ever changes, you only need to change a single definition in a single place.
0
 

Author Closing Comment

by:Tech315
ID: 38847136
This was the quickest and most easy one. Thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server syntax question 13 30
How do I subtract date and time within a same column in SQL 4 36
Help Required 2 29
Query Help - MSSQL - Averages 5 25
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

816 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

9 Experts available now in Live!

Get 1:1 Help Now