?
Solved

MS SQL Numeric to datetime format conversion

Posted on 2004-04-02
10
Medium Priority
?
1,203 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
8 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
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 

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
 
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 1000 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

829 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