Solved

SQL Server: Can i use the distinct keyword on a portion of a column?

Posted on 2010-08-16
4
223 Views
Last Modified: 2012-05-10
I have a column in a table with data such as:

001/01
001/02
002/01
002/02

I'd like to return distinct values that occur in the table on the left hand side of the "/" for slash. So the query should return the values:

001
002

I appreciate it would suggest having seperated it in 2 columns but please presume that that is not possible at this moment. The application code could do this but wanted to know if SQL could query this information.

Many thanks =)
0
Comment
Question by:paddycobbett
[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
  • 2
4 Comments
 
LVL 16

Expert Comment

by:vdr1620
ID: 33449746
Try the below SQL

SELECT Distinct SUBSTRING('001/01',1,PATINDEX('%/%','001/01')-1)
0
 
LVL 16

Accepted Solution

by:
vdr1620 earned 300 total points
ID: 33449754
you can change it accordingly to

SELECT Distinct SUBSTRING(COLUMNNAME,1,PATINDEX('%/%',COLUMNNAME)-1)
0
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 200 total points
ID: 33449815
The above may give you errors if no / exist in the string.

SELECT Distinct left(col1, charindex('/',col1+'/')-1)
from tbl
0
 
LVL 1

Author Closing Comment

by:paddycobbett
ID: 33461612
Thanks guys =)
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

724 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