Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SELECT string by exact match of first 6 characters

Author  Topic 

hurdy
Starting Member

17 Posts

Posted - 2010-03-02 : 20:02:54
Hi all,

Thank you for your time out to read this post.

Is there a SQL statement that I can use to return items from a database table where the search parameter is the first 6 characters of a database column?

For example, I have 1 table with 3 colums.

Column1 data:
1, 2, 3

Column2 data: (this column is in the string datatype, not int)
101111741 *note - 9 characters long
101222852
101333963

Column3 data:
Rob
John
Steve

I need an SQL statement similar to the follow;

SELECT Column1, Column3
FROM Table
WHERE Column2=@Column2 -- but I want to be able to search by the first 6 characters as they are always unique. Please note column2 is in the string format.

So, if I search using 101222 it should return - 2, John.

Thank you for your time and all advice is greatly appreciated.

Rob

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2010-03-03 : 00:15:44
SELECT Column1, Column3
FROM Table
WHERE left(column2,6)=@Column2

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-03-03 : 01:15:29
Better approach for index usage

SELECT Column1, Column3
FROM Table
WHERE column2 like @Column2+'%'

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -