Solved

Convert 3 Date Columns into 1 Date Column

Posted on 2014-07-18
12
230 Views
Last Modified: 2014-07-24
SQL 2008R2:

I'm pulling data from a Linked Server (DB2) and creating views.  The tables in the link servers have dates in 3 fields - let's just call the 3 fields (YYYY) (MM) (DD).  I don't know the data type of these fields on the DB2 side.

When I use the + in my query it adds up the 3 columns and gives me a total.  The only way I have been able to accomplish getting these 3 fields into 1 field is using the following:

CONVERT(nvarchar(50),YYYY)+'-'+CONVERT(nvarchar(50),MM)+'-'+CONVERT(nvarchar(50),DD) as PO_Date

This works but I want the new "PO_Date" field to be a date field.

Any help is greatly appreciated.
0
Comment
Question by:CityofDelrayBeach
12 Comments
 
LVL 11

Expert Comment

by:John_Vidmar
ID: 40205575
DATEFROMPARTS ( year, month, day )
0
 
LVL 32

Expert Comment

by:awking00
ID: 40206382
datefromparts requires integer parameters so, since you don't know the data types from DB2, you may need to convert them to integer first.
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40207006
datefromparts() is only available in SQL Server 2012 onward, convert() with a date style works in all versions.

Assuming all those fields are integers

CONVERT(date,CONVERT(varchar(4),YYYY)+CONVERT(varchar(2),MM)+CONVERT(varchar(2),DD) ,112)  as PO_Date

If all those fields are aleady strings:

CONVERT(date,YYYY+MM+DD ,112)  as PO_Date

for date styles numbers see:
http://www.experts-exchange.com/Database/MS-SQL-Server/A_12315-SQL-Server-Date-Styles-formats-using-CONVERT.html
0
 

Author Comment

by:CityofDelrayBeach
ID: 40208864
Hi everyone, and thanks for the responses.

PortletPaul: Your first statement worked, but when I try to run the "View" I get the following error:

"Error Message: Conversion failed when converting date and/or time from character string."
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40208892
when you run ... what? have you created a view, and it is failing?

Perhaps you could post the sql you are using?

Have you determined what the data types are of those 3 fields from DB2?
are they ALWAYS suitable as YYYY MM DD ?
0
 

Author Comment

by:CityofDelrayBeach
ID: 40209031
Here's my query:

CREATE VIEW POs_FY_2013_2014 as
SELECT [PIPONO] as PO_Num
,CONVERT(date,CONVERT(varchar(4),PIODY)+CONVERT(varchar(2),PIODM)+CONVERT(varchar(2),PIODD) ,110)  as PO_Date
,[PIOVN] as Vend_Num
,[PIREQR] as Requestor
,[PIOEA] as Acct_No
,[PIOUC] as Amount1
,[PIFOUC] as Amount2
,[PIOQ] as Amount3
FROM [DB2SRV].[DELRAYPD].[HTEDTA].[PI320AP]
where PIODY=2013 AND PIODM>=10 or PIODY=2014
GO

It creates the view fine (no errors). But when I try to run the view (return top 200 rows) I get the error I posted.

I am trying to find out the data types of the 3 fields in DB2. I've left a message for my contact. As soon as I know I will post.

Thanks so much Paul!
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:CityofDelrayBeach
ID: 40209114
Paul - the source columns are decimal. I tried changing the query from varchar to decimal and it says "Explicit conversion from data type decimal to date is not allowed."  This http://technet.microsoft.com/en-us/library/ms187928(v=sql.105).aspx says I need an implicit statement using the datetime function. I'm searching but if you can help I greatly appreciate it.

CREATE VIEW POs_FY_2013_2014 as
SELECT [PIPONO] as PO_Num
 ,[PIODY] as PO_Year
 ,[PIODM] as PO_Month
 ,[PIODD] as PO_Day
 ,[PIOVN] as Vend_Num
 ,[PIREQR] as Requestor
 ,[PIOEA] as Acct_No
 ,[PIOUC] as Amount1
 ,[PIFOUC] as Amount2
 ,[PIOQ] as Amount3
FROM [DB2SRV].[DELRAYPD].[HTEDTA].[PI320AP]
  where PIODY=2013 AND PIODM>=10 or PIODY=2014
GO
0
 

Author Comment

by:CityofDelrayBeach
ID: 40209325
Should I repost this question with all the info I have now?
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40210217
Creating the view (without errors) simply means the syntax is ok.

When applying the syntax to the data some problem is discovered. Now we know the source data is decimal that error might simply be "data truncation" but I'm guessing. Try the following and inspect the results.

All 3 have to actually conform to logical rules for conversion to a date. e.g.
we would expect year to be YYYY (only, no decimal part) not 5 digits, not 3
month can only be in range 1 to 12 (not zero to 11)
day can only be in range 1 to 31, but (for example, 31 with month 6 is wrong)

select
        convert(varchar(50),PIODY)
      , convert(varchar(50),PIODM)
      , convert(varchar(50),PIODD)
      , convert(varchar(4),convert(int,PIODY)) as yyyy
      , convert(varchar(2),convert(int,PIODM)) as mm
      , convert(varchar(2),convert(int,PIODD)) as dd
      , PIODY
      , PIODM
      , PIODD
from adb2source

Open in new window

Do columns yyyy mm dd look ok?

note it only takes one whacky value to break the conversion of those fields into a date
0
 
LVL 30

Expert Comment

by:hnasr
ID: 40210448
"Should I repost this question with all the info I have now? "

If no comment is a suitable answer, then yes. You may add a new comment explaining the issue but don't deviate from the original question. If you intend any modification, then accept the best comment in this thread and post a new question.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40210483
I don't think we have reached the point of needing a new question.
0
 

Author Comment

by:CityofDelrayBeach
ID: 40217202
I posted another question (prior to your response) explaining my need more clearly.  I finally got the solution as follows:

cast (case when day = 0 or month = 0 or year = 0 then NULL
else dateadd(DAY, [FIELD] - 1, dateadd(MONTH, [FIELD] - 1, dateadd(YEAR, [FIELD]-1900, '19000101')))
end as Date) as PO_Date

Thanks for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

23 Experts available now in Live!

Get 1:1 Help Now