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

prefixing a query result

Is it possible within the sql statement to prefix with a piece of text?

For example, if i had a table with a column firstname and lastname, and a
sql statemtn like

select firstname,lastname from tblnames

could i have the sql return something like "Mr John Smith" so it is all in
one string?

Thanks for any help

Dave
Sep 18 '06 #1
6 2499

Dave wrote:
Is it possible within the sql statement to prefix with a piece of text?

For example, if i had a table with a column firstname and lastname, and a
sql statemtn like

select firstname,lastname from tblnames

could i have the sql return something like "Mr John Smith" so it is all in
one string?

Thanks for any help

Dave
Well, you could:

SELECT CONCAT('Mr ',firstname,' ',lastname) FROM tblnames;

But that kind of defeats the purpose of a database, doesn't it? :-(

Sep 18 '06 #2
Thanks for that.

I don't think it defeats it really, it just saves me writing that logic
elsewhere, but still leaves the structure there so I can query just for
first names, or sort by last name.

"strawberry" <za*******@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
>
Dave wrote:
>Is it possible within the sql statement to prefix with a piece of text?

For example, if i had a table with a column firstname and lastname, and a
sql statemtn like

select firstname,lastname from tblnames

could i have the sql return something like "Mr John Smith" so it is all
in
one string?

Thanks for any help

Dave

Well, you could:

SELECT CONCAT('Mr ',firstname,' ',lastname) FROM tblnames;

But that kind of defeats the purpose of a database, doesn't it? :-(

Sep 18 '06 #3
On Mon, 18 Sep 2006 14:07:22 GMT, in mailing.database.mysql "Dave"
<da***@nospam.co.uk>
<uK*******************@newsfe4-gui.ntli.netwrote:
>| Is it possible within the sql statement to prefix with a piece of text?
|
| For example, if i had a table with a column firstname and lastname, and a
| sql statemtn like
|
| select firstname,lastname from tblnames
|
| could i have the sql return something like "Mr John Smith" so it is all in
| one string?
|
| Thanks for any help
|
| Dave
What happens if it is a female your responding to?
I think it would be more preferable to do
SELECT concat(firstname,' ',lastname) as FullName, Gender FROM myTable
Then in code add the prefix according to gender (you might also want
to add an age range so you can have Master and Miss as a salutation).
---------------------------------------------------------------
jn******@yourpantsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
Sep 18 '06 #4

Jeff North wrote:
On Mon, 18 Sep 2006 14:07:22 GMT, in mailing.database.mysql "Dave"
<da***@nospam.co.uk>
<uK*******************@newsfe4-gui.ntli.netwrote:
| Is it possible within the sql statement to prefix with a piece of text?
|
| For example, if i had a table with a column firstname and lastname, and a
| sql statemtn like
|
| select firstname,lastname from tblnames
|
| could i have the sql return something like "Mr John Smith" so it is all in
| one string?
|
| Thanks for any help
|
| Dave

What happens if it is a female your responding to?
I think it would be more preferable to do
SELECT concat(firstname,' ',lastname) as FullName, Gender FROM myTable
Then in code add the prefix according to gender (you might also want
to add an age range so you can have Master and Miss as a salutation).
---------------------------------------------------------------
jn******@yourpantsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
yes, exactly - although it's possible that the OP's statement was just
an example

personally, if i thought that it was going to be important to include
information like this then I'd put it in the db to begin with, and then
use CONCAT_WS (which handles null results more elegantly) like so:

SELECT CONCAT_WS(' ',title,',firstname,lastname) from tblnames;

Sep 18 '06 #5

strawberry wrote:
Jeff North wrote:
On Mon, 18 Sep 2006 14:07:22 GMT, in mailing.database.mysql "Dave"
<da***@nospam.co.uk>
<uK*******************@newsfe4-gui.ntli.netwrote:
>| Is it possible within the sql statement to prefix with a piece of text?
>|
>| For example, if i had a table with a column firstname and lastname, and a
>| sql statemtn like
>|
>| select firstname,lastname from tblnames
>|
>| could i have the sql return something like "Mr John Smith" so it is all in
>| one string?
>|
>| Thanks for any help
>|
>| Dave
What happens if it is a female your responding to?
I think it would be more preferable to do
SELECT concat(firstname,' ',lastname) as FullName, Gender FROM myTable
Then in code add the prefix according to gender (you might also want
to add an age range so you can have Master and Miss as a salutation).
---------------------------------------------------------------
jn******@yourpantsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------

yes, exactly - although it's possible that the OP's statement was just
an example

personally, if i thought that it was going to be important to include
information like this then I'd put it in the db to begin with, and then
use CONCAT_WS (which handles null results more elegantly) like so:

SELECT CONCAT_WS(' ',title,',firstname,lastname) from tblnames;
Oh, I made a typo there - but you get the idea.

Sep 18 '06 #6
On 18 Sep 2006 09:11:14 -0700, in mailing.database.mysql "strawberry"
<za*******@gmail.com>
<11*********************@m73g2000cwd.googlegroups. comwrote:
>|
| Jeff North wrote:
| On Mon, 18 Sep 2006 14:07:22 GMT, in mailing.database.mysql "Dave"
| <da***@nospam.co.uk>
| <uK*******************@newsfe4-gui.ntli.netwrote:
| >
| | Is it possible within the sql statement to prefix with a piece of text?
| |
| | For example, if i had a table with a column firstname and lastname, and a
| | sql statemtn like
| |
| | select firstname,lastname from tblnames
| |
| | could i have the sql return something like "Mr John Smith" so it is all in
| | one string?
| |
| | Thanks for any help
| |
| | Dave
| >
| What happens if it is a female your responding to?
| I think it would be more preferable to do
| SELECT concat(firstname,' ',lastname) as FullName, Gender FROM myTable
| Then in code add the prefix according to gender (you might also want
| to add an age range so you can have Master and Miss as a salutation).
| ---------------------------------------------------------------
| jn******@yourpantsyahoo.com.au : Remove your pants to reply
| ---------------------------------------------------------------
|
| yes, exactly - although it's possible that the OP's statement was just
| an example
|
| personally, if i thought that it was going to be important to include
| information like this then I'd put it in the db to begin with, and then
| use CONCAT_WS (which handles null results more elegantly) like so:
|
| SELECT CONCAT_WS(' ',title,',firstname,lastname) from tblnames;
My sentiments exactly :-)
---------------------------------------------------------------
jn******@yourpantsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
Sep 19 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Wm | last post by:
I have a query that I expect to return 3 or 4 entries -- but I seem to be getting only the most recent entry, repeated 4 times. What am I doing wrong here? $query="SELECT...
0
by: Phil Powell | last post by:
I have a very simple mySqlQuery object that takes two parameters: 1) the string query 2) the db connection resource I tested and was certain everything is passing correctly (the string query...
1
by: Phil Powell | last post by:
Here is the scope of what I need to do; want: enrollment_year allowed (even if null) all of ica criteria:
2
by: Mike Poe | last post by:
Hi, Consider the following: <? $username = "foo"; $password = "bar"; $host = "db";
2
by: jaysonsch | last post by:
Hello! I am having some problems with a database query that I am trying to do. I am trying to develop a way to search a database for an entry and then edit the existing values. Upon submit, the...
1
by: muelli75 | last post by:
Hi! Im getting insane by solving a problem .... I try to define a function which uses a code-snippet from another file. My base are the codes from the great book "WebDataBase-Book by H....
3
by: cover | last post by:
I have a table with 50 fields that receive input depending on whether that input came in from a 'shaker' form or a 'conveyor' form. Input from the 'conveyor' form might populate 25 fields while...
24
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every...
11
by: funky | last post by:
hello, I've got a big problem ad i'm not able to resolve it. We have a server running oracle 10g version 10.1.0. We usually use access as front end and connect database tables for data extraction....
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: 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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.