Solved

MS SQL Numeric to datetime format conversion

Posted on 2004-04-02
10
1,188 Views
Last Modified: 2010-05-19
I have a numeric date imported from AS400 in this format: 104125.  It's actually the 125th day of 2004.

How do I convert this in MS SQL to a datetime format?

Thanks
0
Comment
Question by:cmaxin
  • 3
  • 3
  • 2
10 Comments
 
LVL 11

Expert Comment

by:vc01778
ID: 10742144
Assuming that last three digits reperesent a day and the first three 1900 + years:

select dateadd(day,  your_column % 1000 - 1, dateadd(year, your_column/1000, '1900/1/1') ) from your_table

E.g.:

select dateadd(day, 104125 % 1000 - 1, dateadd(year, 104125/1000, '1900/1/1') )

gives:

5/4/2004 12:00:00 AM


VC
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 10742157
Declare @AS400Date integer
Set @AS400Date = 104125
Select DateAdd(Day, @AS400Date % 1000, DateAdd(Year, @AS400Date / 1000, '1900-01-01'))
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 10742164
Oops!
0
 

Author Comment

by:cmaxin
ID: 10742347
Thanks for the response!.  That seems to work if your data type is integer, but mine is numeric.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 11

Expert Comment

by:vc01778
ID: 10742398
create table t1(x numeric(6,0))

insert into t1 values(104125)

select dateadd(day,  x % 1000 - 1, dateadd(year, x/1000, '1900/1/1') ) from t1

5/4/2004 12:00:00 AM


????

VC
0
 
LVL 11

Accepted Solution

by:
vc01778 earned 250 total points
ID: 10742413
Perhaps your datatype is 'varchar'.  Then:

create table t1(x varchar(6))

insert into t1 values('104125')

select dateadd(day,  cast(x as int) % 1000 - 1, dateadd(year, cast(x as int) /1000, '1900/1/1') ) from t1

VC
0
 

Author Comment

by:cmaxin
ID: 10742577
OK.  I changed my data type from numeric to integer and this worked for me:

Declare @AS400Date integer
Set @AS400Date = 104125
Select DateAdd(Day, @AS400Date % 1000, DateAdd(Year, @AS400Date / 1000, '1900-01-01'))

Thank you very much!!
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 11545664
Please do not award me any points, I merely duplicate (inadvertently) vc01778's solution.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

706 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

16 Experts available now in Live!

Get 1:1 Help Now