Solved

Creating a string that includes Single Quote

Posted on 2008-06-18
24
1,012 Views
Last Modified: 2010-04-21
Hi everyone,

I am querying an AS400 and trying to build a date from separate 2 digit numeric fields Century (WAAPCC)  Year (WAAPYY) Month (WAAPMM) and Day (WPAADD). I came up with a wild concat formula that won't work because I don't know how to escape the single quote but here is what I want to work:

MONTH( DATE( CONCAT( CONCAT( CONCAT( CONCAT( CONCAT( CONCAT( '"', DIGITS( WAAPMM ) ), '/' ), DIGITS( WAAPDD ) ), '/' ), DIGITS( WAAPYY ) ), '"' ) ) )

I have double quotes now enclosed with single quotes which returns a nice date but when I add the date function I can't filter with it.

First is there a better way and second how do I escape the single quote if there is not?

Thanks
0
Comment
Question by:FindlayIT
[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
  • 11
  • 6
  • 6
  • +1
24 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 21815211
Try:
MONTH( DATE( CONCAT( CONCAT( CONCAT( CONCAT( CONCAT( CONCAT( '''', DIGITS( WAAPMM ) ), '/' ), DIGITS( WAAPDD ) ), '/' ), DIGITS( WAAPYY ) ), '''' ) ) )

Open in new window

0
 

Author Comment

by:FindlayIT
ID: 21815580
Are those 4 single quotes? If so, it returns nothing.
0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 21815635
that is just wrong...
you escape a single qutoe using a single quote, so the string
' my name is mc''donald'
is actual - my name is mc'donald

try this
MONTH( DATE( ''''|| DIGITS( WAAPMM ) || '/'|| DIGITS( WAAPDD ) || '/' || DIGITS( WAAPYY ) || '''')

|| allows you to concat strings so
a||b||c is actually abc
digits won't help you here this way since they return string with leading zeros, try this

MONTH( DATE( char( WAAPMM ) || '/'|| char( WAAPDD ) || '/' || char( WAAPYY ))
this should work
although its wierd you do that, since you have the month part in WAAPMM so i don't understand what you are trying to do here...
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

Author Comment

by:FindlayIT
ID: 21815753
The MONTH was just to see if the AS400 saw my formlua as a vaild date. I don't need the month function.

Someone is using the remote connection so I will test when I can and get back to you. Thanks.
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 21815760
Hi FindlayIT,

There's a lot easier way to do this.  :)  Here are a few suggestions that you might try.

Calling the CONCAT function is identical to using the CONCAT operator (||).  It makes for a lot neater SQL to stack the concatenated items beside each other instead of multiple nested calls to CONCAT.  At least is this is true with V6.

  DATE (DIGITS(WAAPMM) || '/' || DIGITS(WAAPDD) || '/' || DIGITS(WAAPYY))

The second parameter to CONCAT can be a string or integer.  DB2 will automatically convert integer types to string types, so calling DIGITS() may not be necessary.  Of course, the conversion can't automatically know how many significant digits you want.  :)

Going to all of this trouble to generate a date value and passing it to the MONTH() function is an obscene thing to do.  :)

Hielo is right about repeating the single quote mark within a string generates a single quote.




Good Luck,
Kent

0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 21815826

Wow -- I get distracted by a phone call and the thread gets busy.  :)

0
 

Author Comment

by:FindlayIT
ID: 21816438
Ok guys. I've tried every variation of every suggestion possible and I get no errors but I also get nothing returned. I did get an error removing DIGITS and hoping DB2 converts.

The AS400 may be running an old version so.... It's in a car dealership in Michigan and no one there knows anything..Any ideas?
0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 21816544
try this

MONTH( DATE( char( WAAPYY ) || '.'|| char( WAAPMM ) || '.' || char( WAAPDD ))

if that does not work - how many digits you have in WAAPYY ? 2 or 4 ?
what is the return value of the statement
select current date from sysibm.sysdummy1 ?
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 21816569
If you're getting nothing, let'sbreak the problem down into pieces.

  SELECT   WAAPMM   from mytable;
  SELECT   DIGITS(WAAPMM)   from mytable;
  SELECT   DIGITS(WAAPMM) || '/'    from mytable;
  SELECT   DIGITS(WAAPMM) || '/' || DIGITS(WAAPDD) || '/' || DIGITS(WAAPYY)    from mytable;
  SELECT   DATE (DIGITS(WAAPMM) || '/' || DIGITS(WAAPDD) || '/' || DIGITS(WAAPYY))   from mytable;


Kent
0
 

Author Comment

by:FindlayIT
ID: 21816925
The periods don't work. WAAPYY is 2 digits there is a WAAPCC which is the century. I just created a nice date with the century in MM/DD/YYYY format and the string returned looks good i.e., '03/25/2008' but when I wrap it with the date function I get NULL.

Here's what I used to do this: DATE( '''' || DIGITS( WAAPMM ) || '/' || DIGITS( WAAPDD ) || '/' || DIGITS( WAAPCC ) || DIGITS( WAAPYY ) || '''' )

So the date function doesn't like what it sees. I just created this string '3/25/2008' (wihtout the leading 0 and no go.

I plugged in CURDATE() into the date function and got 6/17/2008 so we know the date function works, we just don't know what it consumes eh?
0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 21817277
what happens when you run
select current date from sysibm.sysdummy1 ?
0
 

Author Comment

by:FindlayIT
ID: 21817800
it says sysibm.sysdummy1 is not found in the data source.
0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 21819481
so try
select current date from sysdummy1
select current date from qsys.sysdummy1
select current date from qsys2.sysdummy1

one of them should work
what is curdate() ?
0
 

Author Comment

by:FindlayIT
ID: 21822879
Hi momi. I get 6/19/2008 with CURRENT DATE as a column in the query. The other tables don't work. If it isn't in QGPL, I can't get to it. curdate() is an ODBC function that returns the current system date in a date data type. Its portable, runs against data sources other than iseries. The server specific equivalent is the special register CURRENT DATE.

0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 21822999
Hi FindlayIT,

The DATE function should convert a string containing a date such that a date value is returned.  (The internal representation is actually an integer, decimal, or long integer.)

If you pass an arbitrary string to the DATE function an error should be thrown.

Getting nothing back suggests that the function is returning a NULL.  This is hard to fathom.

Expanding your query a bit, can you try this:

  SELECT waapmm, waapdd, waapcc, waapyy,
    DIGITS( WAAPMM ) || '/' || DIGITS( WAAPDD ) || '/' || DIGITS( WAAPCC ) || DIGITS( WAAPYY ),
    DATE( '''' || DIGITS( WAAPMM ) || '/' || DIGITS( WAAPDD ) || '/' || DIGITS( WAAPCC ) || DIGITS( WAAPYY ) || '''' ),
    MONTH (DATE( '''' || DIGITS( WAAPMM ) || '/' || DIGITS( WAAPDD ) || '/' || DIGITS( WAAPCC ) || DIGITS( WAAPYY ) || '''' ))

  FROM mytable;

That should force DB2 and the ODBC driver to return something for the column.


Kent

0
 

Author Comment

by:FindlayIT
ID: 21823215
Hi Kent,

Here's the result of that one. I can't imagine why we get NULL

[Promised Month],      [Promised Day],      [Promised Century],      [Promised Year],      DIGITS( [Promised Month] ) || '/' || DIGITS( [Promised Day] ) || '/' || DIGITS( [Promised Century] ) || DIGITS( [Promised Year] ),      DATE( '''' || DIGITS( [Promised Month] ) || '/' || DIGITS( [Promised Day] ) || '/' || DIGITS( [Promised Century] ) || DIGITS( [Promised Year] ) || '''' ),      MONTH( DATE( '''' || DIGITS( [Promised Month] ) || '/' || DIGITS( [Promised Day] ) || '/' || DIGITS( [Promised Century] ) || DIGITS( [Promised Year] ) || '''' ) )
3      19      19      93      03/19/1993      NULL      NULL
7      7      19      93      07/07/1993      NULL      NULL
4      28      19      98      04/28/1998      NULL      NULL
4      29      19      98      04/29/1998      NULL      NULL
4      29      19      98      04/29/1998      NULL      NULL
4      29      19      98      04/29/1998      NULL      NULL
0      0      0      0      00/00/0000      NULL      NULL
1      4      19      99      01/04/1999      NULL      NULL
3      2      19      99      03/02/1999      NULL      NULL
3      4      19      99      03/04/1999      NULL      NULL
0
 
LVL 37

Accepted Solution

by:
momi_sabag earned 500 total points
ID: 21823800
well
beside the all 0 row, it is weird that this is the error you get

try this
DATE( char( || DIGITS( [Promised Month] ) || '/' || DIGITS( [Promised Day] ) || '/' || DIGITS( [Promised Century] ) || DIGITS( [Promised Year] ) ) )

and
month(DATE( char( || DIGITS( [Promised Month] ) || '/' || DIGITS( [Promised Day] ) || '/' || DIGITS( [Promised Century] ) || DIGITS( [Promised Year] ) ) ))
0
 

Author Comment

by:FindlayIT
ID: 21824020
Did you mean to include the two pipes at the beginning after char(?
0
 

Author Comment

by:FindlayIT
ID: 21824074
Momi, I removed the errant two pipes and it looks like you did it. Its the char func, what is it doing?

[Promised Month]      [Promised Day]      [Promised Century]      [Promised Year]      DIGITS( [Promised Month] ) || '/' || DIGITS( [Promised Day] ) || '/' || DIGITS( [Promised Century] ) || DIGITS( [Promised Year] )      DATE( char( DIGITS( [Promised Month] ) || '/' || DIGITS( [Promised Day] ) || '/' || DIGITS( [Promised Century] ) || DIGITS( [Promised Year] ) ) )      month( DATE( char( DIGITS( [Promised Month] ) || '/' || DIGITS( [Promised Day] ) || '/' || DIGITS( [Promised Century] ) || DIGITS( [Promised Year] ) ) ) )
3      19      19      93      03/19/1993      3/19/1993      3
7      7      19      93      07/07/1993      7/7/1993      7
4      28      19      98      04/28/1998      4/28/1998      4
4      29      19      98      04/29/1998      4/29/1998      4
4      29      19      98      04/29/1998      4/29/1998      4
4      29      19      98      04/29/1998      4/29/1998      4
0      0      0      0      00/00/0000      NULL      NULL
1      4      19      99      01/04/1999      1/4/1999      1
3      2      19      99      03/02/1999      3/2/1999      3
3      4      19      99      03/04/1999      3/4/1999      3
0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 21824117
it convert a value to char
0
 

Author Closing Comment

by:FindlayIT
ID: 31468494
Thanks momi
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 21824259

That's a bug or some really, really bad documentation.  The SQL reference material for DB2 for iSeries and LUW both state:

  The DIGITS function returns a character-string representation of the absolute value of a number.


Kent
0
 

Author Comment

by:FindlayIT
ID: 21824295
Thanks for working on this  Kent. I'm kind of new to this. I think you should get some points on this because it was definitely a group effort. I don't know how though.
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 21825607
Hi FindlayIT,

Thanks for the thought, but I don't mind collaborating.  :)  I'm certainly not going to reopen this just to get points.

Just so you know though, sharing credit is easy.  If you click on "Accept as Solution", EE assigns full credit to the author of the corresponding post.  If you click on "Accept Multiple Solutions" EE assigns primary credit to the author of the post and prompts you for the posts that should share in the glory.

I read your profile and suggest that converting the database to DB2/LUW may be a very cost effective thing to consider.  That depends, of course, on how tightly your applications are tied to AW/400.


Welcome to Experts Exchange.  :) :) :)

Kent
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm n…
Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 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