473,396 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

If/else construct returns integer instead of varchar

I'm trying to get the 3rd query to return a varchar, but it returns an integer.

Query one to prove that a varchar can be returned:
Expand|Select|Wrap|Line Numbers
  1. SELECT IF(1=1,0,'one'), IF(1=0,0,'one'); 
  2. +-----------------+-----------------+ 
  3. | IF(1=1,0,"one") | IF(1=0,0,"one") | 
  4. +-----------------+-----------------+ 
  5. | 0 | one | 
  6. +-----------------+-----------------+ 
  7.  
Query two to prove that I can return an integer(CoachedByContactID):
Expand|Select|Wrap|Line Numbers
  1. rs = st.executeQuery("SELECT Title, Author, OwnerID, ResponsiblePersonID, Synopsys, if(ResponsiblePersonID=0,(SELECT CoachedByContactID FROM details WHERE ContactID=6),OwnerID) as Myfield FROM book WHERE BookID=" + BookID);
  2.  
Query three is my problem because I want to return a varchar(UserName instead of CoachedByContactID ) but it returns an integer:
Expand|Select|Wrap|Line Numbers
  1. rs = st.executeQuery("SELECT Title, Author, OwnerID, ResponsiblePersonID, Synopsys, if(ResponsiblePersonID=0,(SELECT UserName FROM details WHERE ContactID=6),OwnerID) as Myfield FROM book WHERE BookID=" + BookID);
  2.  
Jun 13 '08 #1
3 1550
chaarmann
785 Expert 512MB
The question is not clear.
If the last query returns an integer, then maybe UserName is an integer, or the expression ResponsiblePerson=0 evaluates to false, so that always OwnerID is returned. (the name OwnerID sounds like an integer). That's most likely why always an integer and no varchar is returned.

Please provide more details, especially some sample data where we can see the values of ResponsiblePersonID, OwnerId and UserName. Then we can answer the question without guessing around.
Jun 13 '08 #2
Thanks for the guidance. I will give 2 examples:

1.)Values: ResponsiblePersonID:6 OwnerID:50 Username='Clem'
Expand|Select|Wrap|Line Numbers
  1. rs = st.executeQuery("SELECT Title, Author, OwnerID, ResponsiblePersonID, Synopsys, if(ResponsiblePersonID='0',(SELECT Username FROM details WHERE ContactID=6),OwnerID) as Myfield FROM book WHERE BookID=" + BookID);
  2.  
returns the following value: Myfield:50 (this works fine)

2.) Values: ResponsiblePersonID:6 OwnerID:50 Username='Clem'
(I change ResponsiblePersonID='0' to ResponsiblePersonID='6')
Expand|Select|Wrap|Line Numbers
  1. rs = st.executeQuery("SELECT Title, Author, OwnerID, ResponsiblePersonID, Synopsys, if(ResponsiblePersonID='6',(SELECT Username FROM details WHERE ContactID=6),OwnerID) as Myfield FROM book WHERE BookID=" + BookID);
  2.  
returns the following value: Myfield:0 (this doesn't work at all, because the Username WHERE ContactID=6 is 'Clem' )
However, if I change 'SELECT Username' to something like 'SELECT Age' it will actually return the age value, because it is an integer, but as soon as I requist a field of type varchar with a string value like 'Clem', it returns 0.
Jun 13 '08 #3
chaarmann
785 Expert 512MB
Sorry for the late response,but I had no time to respond earlier because of a stressful work project.

Your query seems to be correct, syntactically and semantically.
Also you verified the fact in the first posting that mySqls if-statement can return a string and does not convert everything automatically to an integer. However if you still get 0 instead of the string, then it may come from the way you read the result set. Maybe you get a string back, but you read it later on in your program as an integer and not as a string? Because converting something like 'Clem' to an integer may result in 0. You can test that by executing
Expand|Select|Wrap|Line Numbers
  1. rs = st.executeQuery("SELECT Title, Author, OwnerID, ResponsiblePersonID, Synopsys, 'Clem' as Myfield FROM book WHERE BookID=" + BookID);
  2.  
If you still get 0 back as Myfield here, then it's the case described above, and you should list your programming code how you copy the value from the resultset into a variable, so we can see what's wrong there, why it converts it to an integer afterwards.
Jun 19 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: dmbkiwi | last post by:
I am new to this group, and relatively new to python programming, however, have encountered a problem I just cannot solve through reading the documentation, and searching this group on google. I...
3
by: Nikola | last post by:
Hi all, I have a problem converting datetime to integer (and than back to datetime). Depending whether the time is AM or PM, same date is converted to two different integer representations,...
2
by: Edward | last post by:
SQL Server 7.0 If I run the following in Query Analyzer I get no records returned: exec GetLeadsOutcome_Dealer '1/1/2003','12/2/2003',10, '176, 183' If, however, I run either : exec...
6
by: BlueDragon | last post by:
Grrr! I'm trying to run a script: print 'Declaring cursor' declare cInv cursor forward_only static for select distinct inv.company, inv.contact, inv.address1, inv.city,
7
by: purushneel | last post by:
Hi, I have a following issue in DB2 UDB v8.2. How can I build some data and store it in a temp table and return as a TABLE to a function.. This function will be called in a SELECT stmt....
4
by: Henning M | last post by:
Hej All Im relativ new to VB.net and im trying to collect som device information using cfgmgr32.dll I use - Declare Function GetListLength Lib "cfgmgr32.dll" Alias...
1
by: =?Utf-8?B?RG9u?= | last post by:
Hello, I'm creating a web service that will allow people to enter their contact information into a SQL Server table. I get it to work when I enter all of the fields and press the invoke button,...
6
by: yin_n_yang74 | last post by:
I am trying to create a report in Crystal Reports (v 8.5). I have a stored procedure to pull data from two databases and parameters. There are multiple one-to-many relationships and the stored...
4
by: GabeM | last post by:
I am trying to insert records from one table to another using the WHILE construct, I have managed to move all of the data with the exception of one concatenated field. I can't figure out how to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.