Solved

Creating a string that includes Single Quote

Posted on 2008-06-18
24
1,005 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
  • 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
 

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:Kdo
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:Kdo
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:Kdo
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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:Kdo
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:Kdo
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:Kdo
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 (it really isn't that hard to do) Recursive SQL is most often used to convert columns to rows or rows to columns.  A previous article described the process of converting rows to columns.  This article will build off of th…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

10 Experts available now in Live!

Get 1:1 Help Now