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
 how to select partial part of column

Author  Topic 

AdamWest
Constraint Violating Yak Guru

360 Posts

Posted - 2014-02-20 : 17:40:04
HI I have a PURCHASE ORDER NUMBER which is character
24 pos. and I only want to select in the query those which start with
'101' from 1 to 3 = '101'
how to do this in the querry?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-02-20 : 18:10:45
where left(yourcolumn1, 3) = '101'

Hopefully your table isn't very big as this is a performance concern.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

maunishq
Yak Posting Veteran

71 Posts

Posted - 2014-02-21 : 14:17:33
SUBSTRING(column_name,1,3)='101'

If possible, try to use SUBSTRING function for such selections.

!!_(M)_!!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-02-21 : 14:31:13
And why oh why would you advise SUBSTRING over LEFT in this situation, maunishq? They achieve equivalent results for the left characters. LEFT saves some typing.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

maunishq
Yak Posting Veteran

71 Posts

Posted - 2014-02-24 : 12:07:41
Yes, They both perform similar. Opt for any of them.



!!_(M)_!!
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-02-24 : 13:14:19
WHERE Col1 LIKE '101%'

will use an index if present.



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page
   

- Advertisement -