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
Solved

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

Posted on 2013-05-15
3
636 Views
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'
GROUP BY a.id
ORDER BY ptmorphraw ASC 

Open in new window

0
Comment
Question by:m2ew
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 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,
0
 
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
0
 

Author Comment

by:m2ew
ID: 39169208
@aneeshattingal

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

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to dynamically set the form action using jQuery.

839 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