[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2010-08-16
4
Medium Priority
?
231 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
  • 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 1200 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 800 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

872 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