SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how to select partial part of column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

AdamWest
Constraint Violating Yak Guru

USA
353 Posts

Posted - 02/20/2014 :  17:40:04  Show Profile  Reply with Quote
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

USA
36983 Posts

Posted - 02/20/2014 :  18:10:45  Show Profile  Visit tkizer's Homepage  Reply with Quote
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

Canada
71 Posts

Posted - 02/21/2014 :  14:17:33  Show Profile  Reply with Quote
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

USA
36983 Posts

Posted - 02/21/2014 :  14:31:13  Show Profile  Visit tkizer's Homepage  Reply with Quote
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

Canada
71 Posts

Posted - 02/24/2014 :  12:07:41  Show Profile  Reply with Quote
Yes, They both perform similar. Opt for any of them.



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

SwePeso
Patron Saint of Lost Yaks

Sweden
30249 Posts

Posted - 02/24/2014 :  13:14:19  Show Profile  Visit SwePeso's Homepage  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000