Solved

Convert AS400 fields to SQL Date

Posted on 2013-06-17
5
365 Views
Last Modified: 2013-06-18
I need to combined three fields in our AS400 database in SQL 2008 view to represent a date, i.e. Month, Day, Year

Currently I have used a combination of the following and am not able to display the date information properly:
CONVERT (DateTime, CONVERT(varchar, dbo.PR503AP.PRBDMM) + '/' + CONVERT (varchar, dbo.PR503AP.PRBDDD) + '/' + CONVERT (varchar, dbo.PR503AP.PRBDYY), 110) AS Birth_Date

Doing so, I render YYYY-MM-DD 00:00:00

My desired display should be MM/DD/YYYY or MM-DD-YYYY
0
Comment
Question by:edrz01
  • 3
5 Comments
 
LVL 5

Expert Comment

by:DOSLover
ID: 39254797
SQL Server's CAST and CONVERT functions can help as follows.

select CONVERT(varchar(10),CAST(dbo.PR503AP.PRBDYY+ '-' + dbo.PR503AP.PRBDMM+ '-' + dbo.PR503AP.PRBDDD as datetime),101) from ...;

Open in new window

0
 

Author Comment

by:edrz01
ID: 39254813
Thank you DOSLover, I will try this tomorrow when I'm back at work.
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 39254922
You are almost there. I'm assuming those 3 fields are integers. Below I've used a cte and indentation for clarity, but I believe you really just needed a "wrapper"

convert(varchar, <<what you have here>>, format-mask)

;with cte as (
              select 1980 as PRBDYY, 1 as PRBDMM, 17 as PRBDDD
              )
select   
  -- using recommended, unambiguous, YYYY-MM-DD
  convert(varchar,
          CONVERT (DateTime, 
                      CONVERT(varchar, cte.PRBDYY) 
                    + '-' 
                    + CONVERT (varchar, cte.PRBDMM) 
                    + '-' 
                    + CONVERT (varchar, cte.PRBDDD)
                  )  -- moved format mask from here
        , 110) -- change format mask to suit here
  AS Birth_Date
from cte
;

Open in new window

try this out at: http://sqlfiddle.com/#!3/1fa93/7707
note it isn't essential that you use YYY-MM-DD sequence - but I do recommend it.
0
 

Author Comment

by:edrz01
ID: 39256641
PortletPaul, I took your coding and modified it:

CONVERT (varchar, CONVERT (DateTime, CONVERT (varchar, dbo.PR503AP.PRBDYY) + '-' + CONVERT (varchar, dbo.PR503AP.PRBDMM) + '-' + CONVERT (varchar, dbo.PR503AP.PRBDDD)), 110) As Birth_Date

This gave me the date sequence that I was looking for i.e. MM-DD-YYYY
------
DOSLover, I did try your code and it gave rendered SQL errors.
0
 

Author Closing Comment

by:edrz01
ID: 39256648
Thank you for your recommendation, it pointed me in the direction I needed.
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.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

743 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

12 Experts available now in Live!

Get 1:1 Help Now