If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > cast to varchar

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-23-10, 10:37
db2girl db2girl is offline
∞∞∞∞∞∞
 
Join Date: Aug 2008
Location: Toronto, Canada
Posts: 1,816
cast to varchar

On v9.7:


db2 "select varchar(count(0)) from (select count(c1) from test) as tmp"

1
-----------
1

1 record(s) selected.




On v8.2:

db2 "select varchar(count(0)) from (select count(c1) from test) as tmp"
SQL0440N No authorized routine named "VARCHAR" of type "FUNCTION" having
compatible arguments was found. SQLSTATE=42884



I need to cast the result of the following query into a varchar:

db2 "select count(0) from (select count(c1) from test) as tmp"


v8 doesn't accept integer as an input to varchar(). Is there a way to accomplish this on v8?
Reply With Quote
  #2 (permalink)  
Old 03-23-10, 10:40
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Try CAST(blah as VARCHAR(88))
Reply With Quote
  #3 (permalink)  
Old 03-23-10, 10:42
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
By the way, "select '1' from sysibm.sysdummy1" will also work, as the query always returns 1...
Reply With Quote
  #4 (permalink)  
Old 03-23-10, 10:45
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by n_i View Post
Try CAST(blah as VARCHAR(88))
Make it CAST(CAST(blah as CHAR(88)) as VARCHAR(88))
Reply With Quote
  #5 (permalink)  
Old 03-23-10, 10:51
db2girl db2girl is offline
∞∞∞∞∞∞
 
Join Date: Aug 2008
Location: Toronto, Canada
Posts: 1,816
Thanks, Nick. Using two CAST worked:

db2 "select cast(cast(count(0) as char(88)) as varchar(88)) from (select count(c1) from test) as tmp"

1
----------------------------------------------------------------------------------------
1

1 record(s) selected.


I'll try using sysibm.sysdummy1
Reply With Quote
  #6 (permalink)  
Old 03-23-10, 11:36
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by db2girl View Post
I need to cast the result of the following query into a varchar:

db2 "select count(0) from (select count(c1) from test) as tmp"
why even run a query? the answer is always going to be 1
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 03-23-10, 11:55
Cougar8000 Cougar8000 is offline
Registered User
 
Join Date: Nov 2005
Location: IL
Posts: 554
Shh, Bella is booking over time
__________________
--
IBM Certified DBA on DB2 for Linux, UNIX, and Windows

DB2 v9.1.0.2 os 5.3.0.0
Reply With Quote
  #8 (permalink)  
Old 03-23-10, 12:02
db2girl db2girl is offline
∞∞∞∞∞∞
 
Join Date: Aug 2008
Location: Toronto, Canada
Posts: 1,816
Quote:
Originally Posted by r937 View Post
why even run a query? the answer is always going to be 1

This is not my query and I'm still trying to understand the purpose of count(0)... doing multiple things at the same time so had no time to really think about your response to my other posting but I will.

Could you please explain why the following query (attaching it here) returns 0.
Attached Files
File Type: txt select_c.txt (725 Bytes, 31 views)

Last edited by db2girl; 03-23-10 at 13:43.
Reply With Quote
  #9 (permalink)  
Old 03-23-10, 12:06
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by db2girl View Post
Could you please explain why the following query (attaching it here) returns 0.
most likely it is because the subquery returned no rows -- try running the subquery by itself

by the way, apologies to n_i, who had the same answer as i did in post #3 (i.e. it's always going to return 1)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 03-23-10, 12:06
db2girl db2girl is offline
∞∞∞∞∞∞
 
Join Date: Aug 2008
Location: Toronto, Canada
Posts: 1,816
Or how to replace "SELECT COUNT(0) AS "COUNT" " without changing the purpose of it. This count(0) is causing the query to fail with sql0901n which is what I'm trying to resolve.
Reply With Quote
  #11 (permalink)  
Old 03-23-10, 12:07
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
change it to SELECT COUNT(*) ...
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #12 (permalink)  
Old 03-23-10, 13:28
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
I think logically the entire query is equivalent to something like
Code:
count (distinct POL_STATUS_CD) from < that table> where <those conditions
By the way, since the subquery is an outer join, the subtraction one of the SUM()s can be problematic, because both values could be null.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On