Solved

Query - Need Different Results

Posted on 2013-02-01
15
344 Views
Last Modified: 2013-02-01
Select
 LTRIM(RTRIM(UPPER(ISNULL(dbo.Street.Prefix_Full, '')))
+  ' ' +     RTRIM(UPPER(ISNULL(dbo.Street.Street_Name, '')))
+ ' ' +      RTRIM(UPPER((COALESCE(CASE WHEN dbo.Street.Suffix_Cnty IS NOT NULL THEN Suffix_Full
                                 ELSE dbo.Street.Suffix_Bell
                          END, ''))))) AS Name
      FROM dbo.Segment INNER JOIN dbo.Street ON dbo.Segment.Street_ID = dbo.Street.Street_ID
  Order By Name

When Running this Query i get close to the right results but i need to tweak something. I'm stuck at this part.

Description
Suffix_Bell is the abbrviation of Suffix_Full.  
Suffix_Cnty is also the abbrviation of Suffix_Full.
When Suffix_Bell is populated ( example: Rd(in Suffix_Bell) = Road(in Suffix_Full) Suffix_Cnty will be Null. and Vise Versa.

Suffix_Bell & Suffix_Cnty connot both be populated, its one or the other or niether.

The wierd part of this request is.

When Suffix_Cnty is not null i need to populate the Name field with
Prefix_Full + Street_Name + Suffix_Full (When Suffix_Cnty IS NOT NULL)

When Suffix_Bell is not null i need to populate the Name field with only
Prefix_Full + Street_Name. The Suffix_Bell Column gets put in the Type Field.
Which reading back i think i explained this part wrong to start. Sorry about that
0
Comment
Question by:PtboGiser
[X]
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
15 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 38843927
do you mean this?

Select
 Upper(LTRIM(RTRIM(ISNULL(st.Prefix_Full, '')) 
+  ' ' +     RTRIM(ISNULL(st.Street_Name, '')) 
+ ' ' +    ltrim(rtrim(case when st.suffix_cnty is not null then suffix_full else '' end))) as Name
, case when st.suffix_cnty is not null then '' else suffix_bell end as type

      FROM dbo.Segment as seg
     INNER JOIN dbo.Street  as st
           ON seg.Street_ID = st.Street_ID
  Order By Name 

Open in new window


if not please explain , with some example data....
and confirm suffix_full already contains the expansion of rd->road that doesn't require another lookup..
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 38843936
you mean something like this?
Select
 LTRIM(RTRIM(UPPER(ISNULL(dbo.Street.Prefix_Full, '')))
+  ' ' +     RTRIM(UPPER(ISNULL(dbo.Street.Street_Name, '')))
+ ' ' +      RTRIM(UPPER((COALESCE(CASE WHEN dbo.Street.Suffix_Cnty IS NOT NULL THEN Suffix_Full
                                 ELSE ''
                          END, ''))))) AS Name
    , CASE WHEN dbo.Street.Suffix_Cnty IS NOT NULL THEN '' 
ELSE  Suffix_Bell  END as Type
      FROM dbo.Segment INNER JOIN dbo.Street ON dbo.Segment.Street_ID = dbo.Street.Street_ID
  Order By Name 

Open in new window

0
 
LVL 32

Expert Comment

by:awking00
ID: 38843953
Sample data and the expected results would be most helpful.
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:PtboGiser
ID: 38843963
Angel
The results are showing me the attached document.
Line 229 - Allens Alley (This is proper) as Suffix_Cnty is not null
Line 223 -Alexander Street (Wrong) As Suffix _Cnty is null.
I having trouble understanding when its not pick the data properly.

I need Alexander Street to show only as Alexander which is the Prefix_Full + Street_Name Part. I DO NOT WANT THE STREET
Capture.PNG
0
 

Author Comment

by:PtboGiser
ID: 38843983
Example Data Attached
Thx
Street.txt
Segment.txt
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 38844034
what does this give?

Select
 Upper(LTRIM(RTRIM(ISNULL(st.Prefix_Full, '')) 
+  ' ' +     RTRIM(ISNULL(st.Street_Name, '')) 
+ ' ' +    ltrim(rtrim(case when nullif(st.suffix_cnty,' ') is not null then suffix_full else '' end))) as Name
, case when st.suffix_cnty is not null then '' else suffix_bell end as type

      FROM dbo.Segment as seg
     INNER JOIN dbo.Street  as st
           ON seg.Street_ID = st.Street_ID
  Order By Name 

Open in new window


ps we can't work just from the output you need to show us the actual data rows...
... how do you know what comprises(source data) line 223 as opposed to line 222?
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 38844047
ok how are we supposed to read your street and segment files? what is the layout?
0
 

Author Comment

by:PtboGiser
ID: 38844254
It would not let me load them as a dbf. Sorry let me try a Excel file.
0
 

Author Comment

by:PtboGiser
ID: 38844320
Excel Versions
Segment.xlsx
Street.xlsx
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 38844432
try this

Select
 Upper(LTRIM(RTRIM(ISNULL(st.Prefix_Full, '')) 
+  ' ' +     RTRIM(ISNULL(st.Street_Name, '')) 
+ ' ' +    ltrim(rtrim(case when st.suffix_cnty is not null then suffix_full when st.suffix_bell is not null then '' else suffix_full end))) as Name
, case when st.suffix_cnty is not null then '' else suffix_bell end as type

      FROM dbo.Segment as seg
     INNER JOIN dbo.Street  as st
           ON seg.Street_ID = st.Street_ID
  Order By Name 

Open in new window


please confim that any offending rows do actually contain NULLS or otherwise (i.e could a row actually contain a text N/A?)
0
 

Author Comment

by:PtboGiser
ID: 38844447
Currently rows without a value are null. I would prefer not using n/a if i can help it
0
 

Author Comment

by:PtboGiser
ID: 38844454
Image attached, Sorry Rows are BLANK is it possible they can be blank but not be a NULL value?
Maybe thats my problem.
nulls.PNG
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 38844478
Blank is not NULL
try this

Select
 Upper(LTRIM(RTRIM(ISNULL(st.Prefix_Full, '')) 
+  ' ' +     RTRIM(ISNULL(st.Street_Name, '')) 
+ ' ' +    ltrim(rtrim(case when nullif(st.suffix_cnty,'') is not null then suffix_full when nullif(st.suffix_bell,'') is not null then '' else suffix_full end))) as Name
, case when nullif(st.suffix_cnty,'') is not null then '' else suffix_bell end as type

      FROM dbo.Segment as seg
     INNER JOIN dbo.Street  as st
           ON seg.Street_ID = st.Street_ID
  Order By Name 

Open in new window


but really you need to police your data better if spaces/blank is not valid then you should force it to null.... maybe you should consider triggers or constraints to enforce your data integrity... otherwise you need excessive use of functions like nullif ,, which will have an impact on query performance..
0
 

Author Comment

by:PtboGiser
ID: 38844904
I am new to SQL Server picking up where someone before me left off and took another job. Thanks for the tips i will look into forcing the values to be a null.
0
 

Author Closing Comment

by:PtboGiser
ID: 38844923
Thx, Problem was my blank Field where not a null value.
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

738 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