Jump to content

Archived

This topic is now archived and is closed to further replies.

esport

query help

Recommended Posts

Hey guys,
Having some problems trying to get my query to work.
I have a query where it select the first character of the company feild, but if there is no company it then selects the first character of the surname. I was wondering how to go about it. Below is a snippet of what i currently have.

[code]SELECT * FROM contacts
WHERE  left(company,1)='$letter'[/code]

How do i do it if compnay is null so then search in surname.

Thanks

Daniel

Share this post


Link to post
Share on other sites
WHERE LEFT( IF(company<>'',company,surname), 1 ) = '$letter'

Share this post


Link to post
Share on other sites
Hey thanks for the prompt reply. It works great. Now if both company and surname where null and if I want it to check another feild can I just simply do it like this.

[code]WHERE LEFT( IF(company<>'',company,surname,name), 1 ) = '$letter'[/code]

Thanks

Daniel

Share this post


Link to post
Share on other sites
No, you'd need another nested IF:

[code]WHERE LEFT( IF( company<>'', company, IF( surname <> '', surname, name ) ), 1 ) = '$letter'[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.