Solved

Convert AS400 fields to SQL Date

Posted on 2013-06-17
5
371 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
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.

752 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