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
dave [ Mo, 18 September 2006 16:07 ] [ ID #1469887 ]

Re: prefixing a query result

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? :-(
zac.carey [ Mo, 18 September 2006 16:50 ] [ ID #1469888 ]

Re: prefixing a query result

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" <zac.carey [at] gmail.com> wrote in message
news:1158591004.835799.201290 [at] m7g2000cwm.googlegroups.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? :-(
>
dave [ Mo, 18 September 2006 16:56 ] [ ID #1469889 ]

Re: prefixing a query result

On Mon, 18 Sep 2006 14:07:22 GMT, in mailing.database.mysql "Dave"
<david [at] nospam.co.uk>
<uKxPg.28761$SH2.19258 [at] newsfe4-gui.ntli.net> 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

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).
------------------------------------------------------------ ---
jnorthau [at] yourpantsyahoo.com.au : Remove your pants to reply
------------------------------------------------------------ ---
Jeff North [ Mo, 18 September 2006 17:36 ] [ ID #1469890 ]

Re: prefixing a query result

Jeff North wrote:
> On Mon, 18 Sep 2006 14:07:22 GMT, in mailing.database.mysql "Dave"
> <david [at] nospam.co.uk>
> <uKxPg.28761$SH2.19258 [at] newsfe4-gui.ntli.net> 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
>
> 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).
> ------------------------------------------------------------ ---
> jnorthau [at] 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;
zac.carey [ Mo, 18 September 2006 18:11 ] [ ID #1469891 ]

Re: prefixing a query result

strawberry wrote:
> Jeff North wrote:
> > On Mon, 18 Sep 2006 14:07:22 GMT, in mailing.database.mysql "Dave"
> > <david [at] nospam.co.uk>
> > <uKxPg.28761$SH2.19258 [at] newsfe4-gui.ntli.net> 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
> >
> > 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).
> > ------------------------------------------------------------ ---
> > jnorthau [at] 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.
zac.carey [ Mo, 18 September 2006 18:14 ] [ ID #1469892 ]

Re: prefixing a query result

On 18 Sep 2006 09:11:14 -0700, in mailing.database.mysql "strawberry"
<zac.carey [at] gmail.com>
<1158595874.417156.28960 [at] m73g2000cwd.googlegroups.com> wrote:

>|
>| Jeff North wrote:
>| > On Mon, 18 Sep 2006 14:07:22 GMT, in mailing.database.mysql "Dave"
>| > <david [at] nospam.co.uk>
>| > <uKxPg.28761$SH2.19258 [at] newsfe4-gui.ntli.net> 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
>| >
>| > 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).
>| > ------------------------------------------------------------ ---
>| > jnorthau [at] 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 :-)
------------------------------------------------------------ ---
jnorthau [at] yourpantsyahoo.com.au : Remove your pants to reply
------------------------------------------------------------ ---
Jeff North [ Di, 19 September 2006 03:30 ] [ ID #1471111 ]
Datenbanken » mailing.database.mysql » prefixing a query result

Vorheriges Thema: ResultSet is from UPDATE. No Data.
Nächstes Thema: "unknown column image_location_country_alt" but this column must exist!