Question

Convert text field to date field in Cognos

Asked by: IntercareSupport

I am using Cognos.  I have a field in my database that stores a date that I need to pull.  The problem is that this is a text field.

Here is a sample contents: referred 2/2/2008.  Or, sometimes the date is a little different, like "referred 2/2/08".  Rarely, there is additional text in the field.

So my question is this: how can I parse this text to pull out the date in Cognos?  Right now I have a prompt page with a start date and ending date on a different date field, but I need to convert these to this other field.  Any ideas?

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2008-11-24 at 12:40:59ID23932035
Tags

Cognos

,

Cognos PowerPlay

,

ReportNet

Topic

SQL Reporting

Participating Experts
2
Points
500
Comments
25

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. convert int to datetime
    In the application that I am working with, the date text field value has first been converted to an integer before storing it in the database. I now need to retrieve this data and display it as a date format, probably using the style mm/dd/yy. When I tried the convert statem...
  2. Converting.
    How can you convert a string into a decimal?
  3. Converting from PERL to Executable using PAR
    I have a PERL script that begins with the following: use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; When I convert my PERL script to an executable using pp -o file.exe file.pl, there is no problem. But, when I run file.exe I receive the following pop-...
  4. Convert
    Hi Experts :) I have one big problem in my program. I want to convert a HTML/HTM file so that it can easily be imported in my DBF. Is it possible to do so using FoxPro 2.5/2.6 for DOS. For Example :- 1. I downloaded one HTML page from a site now i want this page to be con...
  5. Convert epoch timestamp in a text field in Reporting Services.
    I have a text field that is a work log type field and each entry into this field contains a timestamp of when the entry is submitted. In SSMS and in Reporting services, the timestamp appears in epoch time. I get around this problem in the CreateDate (integer) field by using: ...
  6. Convert a COMP-3 (signed) field to ASCII
    Title: Convert a COMP-3 (signed) field to ASCII Bookmark: Question: I am having problems unpacking this data (I've attached sample file). This code (below) works on another clients file - but is NOT returning the correct data for this data(attached) from another client. ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: RWrigleyPosted on 2008-11-24 at 14:20:01ID: 23030992

This type of fucntionality is really in the domain of a good ETL tool.  The problem you're going to run into is that you obviously can't be sure of what is going to be in the field.  

The only thing you can try is a big string function, using a "find" fucntion to find the location of the first "/" in the string, then using substring to grab the two characters immediatly before that.  Then find the second slash, and substring the characters between it and the frist slash, then substring the characters after the second slash.  Throw those three substrings into a "create_timestamp" function, and voila, you'll have a report that will blow up when you hit a database entry that has random slashes in it, or that puts month before date, or any of a hundred other things that will be next to impossible to plan for in a string manipulation expression.

You're best bet is to use the native database functions for this; that'll ensure that they get pushed to the database.  This type of operation is relatviely slow, so you want the database to do it.

 

by: IntercareSupportPosted on 2008-11-25 at 09:08:12ID: 23036066

At this time I am unable to do this in the database.  Our usage must occur in Cognos.

I'm having trouble find much detail on this function.  Can you provide example syntax?

 

by: RWrigleyPosted on 2008-11-25 at 19:00:16ID: 23039212

It would help if we knew what kind of database you're working with.  However, there are cognos functions that will work too.

This gets you the two characters preceeding the first /:
substring([myDate],Position('/',[myDate])-2,2)

This gets you the characters between the /:
substring([myDate],Position('/',[myDate])+1,Position('/',substring([myDate],position('/',[myDate])+1)))

This gets you the two characters following the second /:
substring([myDate],position('/',[myDate])+Position('/',substring([myDate],position('/',[myDate])+1)),2)

Put 'em all together:
_make_timestamp(
  cast(substring([myDate],Position('/',[myDate])-2,2),integer),
  cast(substring([myDate],Position('/',[myDate])+1,Position('/',substring([myDate],position('/',[myDate])+1))),integer),
  cast(substring([myDate],position('/',[myDate])+Position('/',substring([myDate],position('/',[myDate])+1)),2),integer)

Of course, this will throw an error if any of your fields have fewer then two /, or have a non-numberic in the two spaces before the first slash or after the second, or if any of them aren't in year/month/day format or contain invalid numbers.  Other then that, it's bulletproof.

 

by: IntercareSupportPosted on 2008-11-26 at 07:13:56ID: 23042611

I started just trying out the first function, and I'm sure I'm doing something wrong.  If you look at the screenshot below, you'll see what I'm trying to do.  The "Referral_Auth_Notes" is the field I'm trying to parse.  When I execute this, it errors out.

By the way, I'm using MS SQL server 2005.

 

by: RWrigleyPosted on 2008-11-26 at 09:35:23ID: 23043958

Do any of the records lack a '/'?  What is the actual error message?

 

by: IntercareSupportPosted on 2008-11-26 at 10:28:08ID: 23044392

Here's what I'm getting....

 

by: welchb123Posted on 2008-12-02 at 06:10:50ID: 23076910

Your Cognos administrator need to look at the log files and give you the complete error message.

 

by: IntercareSupportPosted on 2008-12-02 at 06:38:42ID: 23077149

I attached a snippet from the log.  I'm hoping that I don't need to create a ticket with my vendor to fix this problem.  Any thoughts?

 

by: welchb123Posted on 2008-12-02 at 07:35:08ID: 23077741

As Rwrigley pointed out in his earlier posts there are a number of issues here.  Besides the ones he already pointed out the most problamatic from your point of view is the year being either 2 or 4 digits. This will not throw an error but will give you corrpt data.   If you really have to attempt this I would first extract each piece into its own data item.

Once you have done that you can clean up each item before converting them to a number.  ONce you have cleaned each section you should be able to convert to a date without errors.   Here is an example of some possible clean up for the first part.For example
Define  date_Part_1 =
substring ([Referral_Auth_Notes],Position('/',[Referral_Auth_Notes])-2,2)
Then Define Data_Part_1_Clean

IF ( [date_Part_01] = '01' or [date_Part_01] = ' 1' or [date_Part_01] = '1 ') THEN  ( '01' ) ELSE
IF ( [date_Part_01] = '02' or [date_Part_01] = ' 2' or [date_Part_01] = '2 ') THEN  ( '02' ) ELSE
IF ( [date_Part_01] = '03' or [date_Part_01] = ' 3' or [date_Part_01] = '3 ') THEN  ( '03' ) ELSE
.
.
.
IF ( [date_Part_01] = '09' or [date_Part_01] = ' 9' or [date_Part_01] = '9 ') THEN  ( '09' ) ELSE
    ( '00' )

 

by: welchb123Posted on 2008-12-02 at 08:18:43ID: 23078280

The only thought I have on your error is you need to check for Null values before you perform your substring.  

If  ( [Referral_Auth_Notes] <> null) then (substring ([Referral_Auth_Notes],position('/', [Referral_Auth_Notes])-2,2)) else ("00")

 

by: IntercareSupportPosted on 2008-12-02 at 09:59:40ID: 23079171

Your first suggestion looks good, but I can't seem to make it functional.  The next post gives me this error:

Parsing error before or near position: 131 of: "If ( [Referral_Auth_Notes] <> null) then (substring ([Referral_Auth_Notes],position('/', [Referral_Auth_Notes])-2,2)) else ("00")"

 

by: welchb123Posted on 2008-12-02 at 10:53:22ID: 23079624

Sorry typo on my part use single quote at the end

else ('00')

 

by: IntercareSupportPosted on 2008-12-02 at 13:03:04ID: 23080765

Thanks for all your help so far.  I should have thought of that typo, especially considering I just recently had to deal with that.

I'm still getting an error.

DPR-ERR-2082 An error has occurred. Please contact your administrator. The complete error has been logged by CAF with SecureErrorID:2008-12-02-15:31:21.451-#14

The log file is attached.

 

by: welchb123Posted on 2008-12-03 at 05:55:07ID: 23085826

Hmmm I substituted a field from one of my pakages and it works fine.  Try this create a new report(do not reuse the report you have been working on).  Bring two items into the report a key field and Referral_Auth_Notes.  Filter the report so you get one record that has a date in Referral_Auth_Notes.  Drag a new Query calculationquery calculation into the report after Referral_Auth_Notes and name it Date_Part_1.   Copy and past the calculation below into the expression editor.  Run the report.

If ( [Referral_Auth_Notes] <> null) then (substring ([Referral_Auth_Notes],position('/', [Referral_Auth_Notes])-2,2)) else ('00')

 

by: IntercareSupportPosted on 2008-12-08 at 09:47:04ID: 23122304

Sorry for the delay on my response, I've been on another project.

Wow, that works!  It is pulling the '12' out of 12/xx/xxxx.  How do I pull the other dates out?  Thanks for your help!

 

by: welchb123Posted on 2008-12-08 at 09:59:24ID: 23122430

Modified from above comment by Rwrigley

2md part
If ( [Referral_Auth_Notes] <> null) then
substring([Referral_Auth_Notes],Position('/', [Referral_Auth_Notes] )+1,Position('/',substring([Referral_Auth_Notes] ,position('/',[Referral_Auth_Notes])+1)))

3rd part
If ( [Referral_Auth_Notes] <> null) then
substring( [Referral_Auth_Notes],position('/', [Referral_Auth_Notes] )+Position('/',substring([Referral_Auth_Notes],position('/',[Referral_Auth_Notes])+1)),2)

 

by: IntercareSupportPosted on 2008-12-08 at 10:11:57ID: 23122548

It's erroring out.  I know it's because I'm stupid.  See pic.

 

by: IntercareSupportPosted on 2008-12-08 at 10:13:31ID: 23122564

I ultimately want to filter date ranges, so I need to piece these pieces together.  Thanks.

 

by: welchb123Posted on 2008-12-08 at 11:09:35ID: 23123096

You have to create the three variables separately and then put them together because your data is so diverse.   You will also have to dedide what to do with the dates that are missing.  

So once you have created three variables Date_Part1, Date_Part2, and Date_Part3.  You will need to clean them up.  Ie remove any that are not numbers or are null.   See the code I posted above for Date_Part_clean.  For month you will need to check for 1 - 12 for day 1-31 and for year well you get the idea.  

Only once you have eliminated any non-nummeric or invalid characters can you glue the pieces together and do a cast to make it a date.

Also I noticed I typed ,2 on the last date part when that one should be ,4 since you said the year is 4 digits sometimes.

 

by: IntercareSupportPosted on 2008-12-08 at 12:08:38ID: 23123666

Thanks *so* much so far.  There's a error somewhere in the last 2 parsers.
Here's the error:

192.168.10.246:9300      5584      2008-12-08 15:04:53.901      -5                  0      0      5284      QOS      1166      1      Audit.RTUsage.QOS            <message code="901" location="/qs:command/validate/querySet/queries/query/selection/dataItem[4]/expression" severity="error" title="QE-DEF-0459 CCLException" type="expression">QE-DEF-0260 Parsing error before or near position: 51 of: &quot;If ( [Referral_Auth_Notes] <> null) then
substring&quot;
QE-DEF-0261 QFWP - Parsing text: If ( [Referral_Auth_Notes] <> null) then
substring( [Referral_Auth_Notes],position(&apos;/&apos;, [Referral_Auth_Notes] )+Position(&apos;/&apos;,substring([Referral_Auth_Notes], Position(&apos;/&apos;,[Referral_Auth_Notes])+1)),2)
</message>

By the way, can you recommend any good online resources or books on learning more about Cognos?

 

by: welchb123Posted on 2008-12-08 at 13:21:30ID: 23124371

If ( [Referral_Auth_Notes] <> null) then
(substring( [Referral_Auth_Notes],position('/', [Referral_Auth_Notes] )+Position('/',substring([Referral_Auth_Notes],position('/',[Referral_Auth_Notes])+1)),4)) else ('NoGo')

Brackets on if then else are wrong.  Try this

 

by: welchb123Posted on 2008-12-08 at 13:24:47ID: 23124399

Here is PArt 2 with the correct backets I hope.  I am just doing this on the fly.
Define Date_Part_2

If ( [Referral_Auth_Notes] <> null) then
(substring([Referral_Auth_Notes],Position('/', [Referral_Auth_Notes] )+1,Position('/',substring([Referral_Auth_Notes] ,position('/',[Referral_Auth_Notes])+1)))) else ('No')

 

by: IntercareSupportPosted on 2008-12-09 at 08:07:37ID: 23130308

Great.  I now can capture the first and second part of the date, what's the last field?  Can you explain what's going on within the code?  I could finish it up if I understood it.  Thanks.

 

by: welchb123Posted on 2008-12-09 at 08:34:35ID: 23130607

define date part3
If ( [Referral_Auth_Notes] <> null) then
(substring( [Referral_Auth_Notes],position('/', [Referral_Auth_Notes] )+Position('/',substring([Referral_Auth_Notes],position('/',[Referral_Auth_Notes])+1)),4)) else ('NoGo')

This code finds the position of the first '/' and then takes from the slash to the end of the line and searches for another '/'.   Once it has found the second '/' it adds the position of the first slash to the position of the second slash and goes back to the original dataline and grabs 4 characters from there and places them in the variable.  

Yikes this is fun isn't it.

 

by: IntercareSupportPosted on 2008-12-30 at 11:29:52ID: 31519899

I'm going to come back to this question when I get the time.  Thanks for all your help!

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...