Solved

SQL View Question

Posted on 2007-11-29
5
162 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
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL HELP 2 94
format nvarchar field as mm/dd/yyyy 4 78
Can Unique column have more than one Null? 8 54
Replace Dates in query 14 51
There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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