Solved

SQL View Question

Posted on 2007-11-29
5
159 Views
Last Modified: 2010-03-19
I have a view with a column ADVERTISER_NAME and it hold values like

Staples.com
MacMall Affiliate Advantage Network
Dell Home & Home Office
Buy.com USA and Buy.com CA

I want to define another column that should only get the characters of column ADVERTISER_NAME  until the first occurance of a space
ie if the above value are as follows:
Staples.com
MacMall
Dell
Buy.com

It is similar to the instr function in VB
0
Comment
Question by:TECH_NET
  • 3
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
SQL_SERVER_DBA earned 250 total points
Comment Utility
select substring(advertiser_name, 1, charindex(' ', Advertiser_Name)) from tablename
0
 
LVL 17

Expert Comment

by:Chris Mangus
Comment Utility
This code:  select substring(advertiser_name, 1, charindex(' ', Advertiser_Name)) from tablename

 will give you a trailing blank.

Try:  select substring(advertiser_name, 1, charindex(' ', Advertiser_Name) - 1) from tablename
0
 
LVL 17

Expert Comment

by:Chris Mangus
Comment Utility
Make sure you're aware that the code you've accepted will leave you with a trailing blank in your data which means you will end up with data that you can't query and find.
0
 
LVL 16

Expert Comment

by:SQL_SERVER_DBA
Comment Utility
cmangus, give it up...He/She read it the first time...stop reaching...
0
 
LVL 17

Expert Comment

by:Chris Mangus
Comment Utility
It's not reaching.  This user will have data they can't find.  They're expecting to find "Dell" and the value stored in the table is "Dell " with a trailing space.  Those two values won't match in a query.

In my last 25+ years working on data systems this has tripped me up more than once.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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