473,395 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Combine SQL queries for same column.

Hello all this is my first post.
I am trying to strip some unwanted text from a column containing department names.

This first query strips all characters after the colon in the name:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2. CASE WHEN CHARINDEX(':', DB.Table.DEPT)>0 
  3. THEN
  4. LEFT(DB.Table.DEPT, CHARINDEX(':', DB.Table.DEPT)-1) 
  5. ELSE 
  6. DB.Table.DEPT
  7. END
  8. FROM
  9. DB.Table
The second query strips the prefix from the name:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2. REPLACE(
  3. REPLACE(
  4. REPLACE (DB.Table.DEPT,'[NA1] ','') 
  5. ,'[NA2] ', '')
  6. ,'[NA3] ', '')
  7. FROM
  8. DB.Table
Both of these work great independent of each other, but when I try to combine them it fails.
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2. CASE WHEN CHARINDEX(':', DB.Table.DEPT)>0 
  3. THEN
  4. LEFT(DB.Table.DEPT, CHARINDEX(':', DB.Table.DEPT)-1) 
  5. ELSE 
  6. DB.Table.DEPT
  7. END
  8. FROM
  9. (SELECT
  10. REPLACE(
  11. REPLACE(
  12. REPLACE (DB.Table.DEPT,'[NA1] ','') 
  13. ,'[NA2] ', '')
  14. ,'[NA3] ', '')
  15. FROM
  16. DB.Table)
I could really use some guidance with this.
Thanks in advance.
May 13 '14 #1
2 1276
Rabbit
12,516 Expert Mod 8TB
Please use code tags when posting code or formatted data.

The problem with your combined query is that you never give the expression you created an alias. You also didn't alias your subquery.
May 13 '14 #2
With what Rabbit said try something like
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     CASE WHEN CHARINDEX(':', A.DEPT)>0 
  3.     THEN
  4.     LEFT(A.DEPT, CHARINDEX(':', A.DEPT)-1) 
  5.     ELSE 
  6.     A.DEPT
  7.     END
  8. FROM
  9.     (SELECT
  10.     REPLACE( REPLACE( REPLACE (DB.Table.DEPT,'[NA1] ','') ,'[NA2] ', '')    ,'[NA3] ', '') as DEPT
  11.     FROM
  12.     DB.Table) A
  13.  
May 13 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Nick Heppleston | last post by:
I have a concatenation problem and I was wondering if somebody might be able to offer some help :-) I have the following table structure holding product long descriptions: Part...
8
by: pb648174 | last post by:
I have a single update statement that updates the same column multiple times in the same update statement. Basically i have a column that looks like .1.2.3.4. which are id references that need to...
6
by: christopher.secord | last post by:
Is there any advantage to doing this: ALTER TABLE testtable ADD CONSTRAINT PK_sysUser PRIMARY KEY NONCLUSTERED (UserID) WITH FILLFACTOR = 100, CONSTRAINT IX_sysUser UNIQUE NONCLUSTERED...
3
by: Krish | last post by:
I have created 2 tables, both tables have the same column names (property ColumnName) it is priceDate,priceTime and priceValue I get an error saying that Column priceDate already belongs to...
5
by: Marina | last post by:
Hi, Let's say I bind the same column to multiple properties of one control (or even to the same property of several controls). If a user changes the value of the property in one control - I...
0
by: Asfar | last post by:
I have a databound column in DataGridView. Is there a way to have different cell types in the same column? Thanks, -Asfar
2
by: Volition | last post by:
I have looked around and can't find any help for my SQL problem. I have a Table which lists people peopledb. _____________ id | Name 1 | Fred 2 | Bill 3 | Bob
1
by: pl1 | last post by:
Hi everyone, I'm using MS-Access 2003, OS: Window XP Professional sp3. In Northwind database (sample Database),the Suppliers table have column "SupplierID" which is also present in Products...
2
by: perhapscwk | last post by:
vba, how to use vba to search how many distinct amount in same column? for example.. column C have amount 123 123 444 123 445
11
by: spmartin2012 | last post by:
I'm sure this is very simple, but I'm just starting with these types of tasks. I need to copy a cell value to the same column in the next row and cycle thru all data. I can use this twice to do...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.