Solved

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

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

Suggested Solutions

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

815 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

11 Experts available now in Live!

Get 1:1 Help Now