Solved

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

Posted on 2013-05-15
3
599 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Read about why website design really matters in today's demanding market.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now