473,385 Members | 1,622 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,385 software developers and data experts.

Return string in query

Hi,
I have a inconvenient with a query, I'll try to explain me of the best way.
I have accounts like current account, savings account, investments, loans, but everybody doesnīt have all the accounts. So that, for example a person has CA, SC and investments, but when i seek loans, I want to receive a string like "no loans", because in the example the person only has 3 accounts and he doesnīt have loans. Please help me
Jan 16 '09 #1
9 1789
ck9663
2,878 Expert 2GB
Would it be possible for you to post some sample data of the tables you are working with and your desired output?

-- CK
Jan 16 '09 #2
Really i can't do it because i only have access to few fields, but an example could be
Person 1 has accounts: current account, savings account, investments and loans, so that I search a CC, SC, Inv or Loans and i obtain a number account

Person 2 has accounts: current account, savings account and investments, but not loans and for example i want to bring loans, but like person 2 doesnīt have loans, I want to present a message. My query is:


select @value=count(account) from dbdata..loans where customer = @id
IF(@value > 0)
select account as CUENTA from dbdata..loans where customer = @id
ELSE
select 'NO POSEE CUENTA AHORROS' as CUENTA

I want to optimize my query, and exist tables different for every account
Jan 16 '09 #3
ck9663
2,878 Expert 2GB
If you have a look up table for the account type, you can use it as main table - left join to your "Persons" table and check if the join keys are NULL. If it is, then return a hard coded string.

Good luck.


-- CK
Jan 16 '09 #4
sometime like it:

select ISNULL(CC.cuenta,'NO POSEE CUENTA CORRIENTE')
from dbdata..cc CC left join dbdata..cifmst_1 CL on CC.cliente = CL.cliente where CL.cliente = @id

Unfortunately, it doesnīt resolve my problem, canīt help me
Person
Name1, Name2, id (PK)
SC
number account, id (FK)
CC
number account, id (FK)
INV
number account, id (FK)
LOANS
number account, id (FK)

but when i have to search loans, and there isnīt data of loans, I donīt receive nothing, in this case i want to display a message
Jan 16 '09 #5
ck9663
2,878 Expert 2GB
@esteban

Try this:

Expand|Select|Wrap|Line Numbers
  1. select person.id, name1, name2,
  2. case when sc.id is null then 'NO SC ' else 'WITH SC' end,
  3. case when cc.id is null then 'NO CC ' else 'WITH SC' end,
  4. case when inv.id is null then 'NO INV ' else 'WITH INV ' end,
  5. case when loans.id is null then 'NO LOANS ' else 'WITH LOANS' end
  6. from persons
  7. left join sc on sc.id = persons.id
  8. left join cc on cc.id = person.id
  9. left join inv on inv.id = person.id
  10. left join loans on loans.id = person.id
  11.  
Or something like that....I did not test the code, but it should give you ideas on how to do it.

Happy coding!

-- CK
Jan 20 '09 #6
it is ok, but you have to remember that I need the number account of course if exist the data, in other case the message, other friend told me to use this expression
if exists(select account from customer where customerid=@id)
message
else
another message
but even i need the number account and not a message
Jan 20 '09 #7
ck9663
2,878 Expert 2GB
Then return the [number account] column

Expand|Select|Wrap|Line Numbers
  1.  
  2. select person.id, name1, name2,
  3. case when sc.id is null then 'NO SC ' else [number account] end as scaccount,
  4. case when cc.id is null then 'NO CC ' else [number account] end as ccaccount,
  5. case when inv.id is null then 'NO INV ' else [number account] end as invaccount,
  6. case when loans.id is null then 'NO LOANS ' else [number account] end as loanaccount
  7. from persons
  8. left join sc on sc.id = persons.id
  9. left join cc on cc.id = person.id
  10. left join inv on inv.id = person.id
  11. left join loans on loans.id = person.id
  12.  
  13.  
However, [number account] should be a string.

Happy coding!

-- CK
Jan 21 '09 #8
it could be, but I followed your indication in my query and when i donīt have a account, in the response I donīt get nothing, only shows the name of column but nothing more

declare @typeAccount varchar(30)
set @typeAccount='CA'
select case when cu.customer is null then 'NO CA' else cast(ca.account as varchar(10)) end as ACCOUNT
from customer cu
left join ca ca on ca.customer = ca.customer
where ca.customer = @id

I have tried another solution, is it ok?

declare @valor, @exist varchar(15)
select @valor=count(CA.account), @exist=CA.account from ca CA where CA.customer = @id group by CA.account
IF(@valor > 0)
select @exist as ACCOUNT
ELSE
select 'NO CA' as ACCOUNT
Jan 21 '09 #9
ck9663
2,878 Expert 2GB
Are you saying the ID exists in both table but the number account is blank?

Then this should work:


Expand|Select|Wrap|Line Numbers
  1. select person.id, name1, name2,
  2. case when sc.id is null or sc.[number account] is null or sc.[number account] = '' then 'NO SC ' else [number account] end as scaccount,
  3. case when cc.id is null or cc.[number account] is null or cc.[number account] = '' then 'NO CC ' else [number account] end as ccaccount,
  4. case when inv.id is null or inv.[number account] is null or inv.[number account] = '' then 'NO INV ' else [number account] end as invaccount,
  5. case when loans.id is null or loans.[number account] is null or loans.[number account] = ''  then 'NO LOANS ' else [number account] end as loanaccount
  6. from persons
  7. left join sc on sc.id = persons.id
  8. left join cc on cc.id = person.id
  9. left join inv on inv.id = person.id
  10. left join loans on loans.id = person.id
  11.  
Happy Coding!

-- CK
Jan 21 '09 #10

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

Similar topics

4
by: David Walker | last post by:
Hi I have a Microsoft SQL database I can use (also mySQL, so if you know how to do this in mySQL that is just as useful). The database can only be accessed from webpages hosted on the same server...
3
by: Phil Powell | last post by:
My first time working with a PHP class, and after 6 hours of working out the kinks I am unable to return a value from the class, so now I appeal to the general audience what on earth did I do wrong...
8
by: Adrian Parker | last post by:
Hi. I would like to query a database, given several where clauses to refine my search, and return the value of one single field in the database. eg: I have a table that lists teachers. Their...
3
by: dgaucher | last post by:
Hi, I want to consume a Web Service that returns a choice, but my C++ client always receives the same returned type. On the other hand, when I am using a Java client, it is working fine (of...
4
by: Bob Bedford | last post by:
We have no access to a mysql NG on my provider's server, so we ask here: We have a long query (long in text) with a UNION between 2 select. We have been informed that some times the query...
1
by: bdt513 | last post by:
I am trying to extract the values from a query using VBA. Specifically, I want to concatenate all the values of the "rosEmail" field from query "qselRosterEmailList" into one string (strEmails). I...
6
by: Grant | last post by:
I am connecting to an access database using a datareader in C#. I get results when I run a certain query from Access but when I run it from Code it does not retrieve any results. I have put a stop...
25
by: wd | last post by:
I want my server to send a 404 header if a URL with a query string is requested. So if a browser or spider requests something like www. my_site .com?p=chair they would get a 404... But if they...
1
by: CAG | last post by:
Hi, In my application, i'm storing sql query as value of table field in database. Like in my NewHireEmployee table, value of JoinDateQuery field can be (select hiredate from employeeInfo) or...
3
by: jambonjamasb | last post by:
Hi I have two tables: email_tbl Data_table Data table is is used to create a Form Data_form
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?

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.