Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL View Question

Posted on 2007-11-29
5
Medium Priority
?
167 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 1000 total points
ID: 20376628
select substring(advertiser_name, 1, charindex(' ', Advertiser_Name)) from tablename
0
 
LVL 17

Expert Comment

by:Chris Mangus
ID: 20376867
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
ID: 20379594
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
ID: 20379602
cmangus, give it up...He/She read it the first time...stop reaching...
0
 
LVL 17

Expert Comment

by:Chris Mangus
ID: 20381987
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .

916 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