Solved

Convert 3 Date Columns into 1 Date Column

Posted on 2014-07-18
12
229 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
Comment Utility
DATEFROMPARTS ( year, month, day )
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:CityofDelrayBeach
Comment Utility
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
Comment Utility
Should I repost this question with all the info I have now?
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
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
Comment Utility
"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
Comment Utility
I don't think we have reached the point of needing a new question.
0
 

Author Comment

by:CityofDelrayBeach
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

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

17 Experts available now in Live!

Get 1:1 Help Now