Link to home
Start Free TrialLog in
Avatar of FindlayIT
FindlayITFlag for United States of America

asked on

Creating a string that includes Single Quote

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
Avatar of hielo
hielo
Flag of Wallis and Futuna image

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

Open in new window

Avatar of FindlayIT

ASKER

Are those 4 single quotes? If so, it returns nothing.
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...
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.
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


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

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?
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 ?
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
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?
what happens when you run
select current date from sysibm.sysdummy1 ?
it says sysibm.sysdummy1 is not found in the data source.
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() ?
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.

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

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
ASKER CERTIFIED SOLUTION
Avatar of momi_sabag
momi_sabag
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Did you mean to include the two pipes at the beginning after char(?
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
it convert a value to char
Thanks momi

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
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.
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