[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


SQL SELECT CASE & REPLACE() for text change / sorting

Posted on 2013-05-15
Medium Priority
Last Modified: 2013-05-15
I've assigned a coloum called ptmorphraw to rework the post_title value for sorting. However, when I apply the query the replace values come up as null.

Essentially I need to replace the value so the text...Single Stream, Double Stream etc, become
1 Stream, 2 Stream

SELECT CASE WHEN a.post_title LIKE  '%Single Stream%'
THEN REPLACE( a.post_title,  'Single Stream',  '1 Stream' ) 
WHEN a.post_title LIKE  '%Double Stream%'
THEN REPLACE( a.post_title,  'Double Stream',  '2 Stream' ) 
END AS ptmorphraw, a.post_title AS ptmorph, a . * , d . * 
FROM wp_prefix_posts a
INNER JOIN wp_prefix_term_relationships e ON a.ID = e.object_id
INNER JOIN wp_prefix_term_taxonomy c ON c.term_taxonomy_id = e.term_taxonomy_id
INNER JOIN wp_prefix_terms d ON c.term_id = d.term_id
WHERE e.object_id = a.ID
AND a.post_type =  'recyclingbins'
AND a.post_status =  'publish'
AND d.slug =  'steel'
ORDER BY ptmorphraw ASC 

Open in new window

Question by:m2ew
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 75

Accepted Solution

Aneesh Retnakaran earned 2000 total points
ID: 39169163
You missed to put an else condition on your case statement

CASE WHEN a.post_title LIKE  '%Single Stream%'
THEN REPLACE( a.post_title,  'Single Stream',  '1 Stream' )
WHEN a.post_title LIKE  '%Double Stream%'
THEN REPLACE( a.post_title,  'Double Stream',  '2 Stream' )
ELSE a.post_title
END AS ptmorphraw,
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 39169179
and your order by to work , should contain the same case statement instead of the column name

Author Comment

ID: 39169208

Brilliant! Knew it had to be something silly. Been staring at the query too long.

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question