?
Solved

SQL View Question

Posted on 2007-11-29
5
Medium Priority
?
166 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
[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
  • 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

752 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