Solved

MS SQL Numeric to datetime format conversion

Posted on 2004-04-02
10
1,191 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

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.

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

821 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