944,164 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 834
  • MS SQL RSS
Oct 16th, 2009
0

SELECT statement

Expand Post »
I have a table for example: tb1
with data:
MS SQL Syntax (Toggle Plain Text)
  1. cse-12m
  2. cse-343k
  3. cse-mka
  4. cse-ptu
  5. cse-jpy

How can I write a SELECT statement to retrieve data in one of these formats:
1) cse-three digits number
2) cse-p%

and exclude other format data, any idea?
Last edited by michael123; Oct 16th, 2009 at 8:41 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
michael123 is offline Offline
93 posts
since Jun 2005
Oct 17th, 2009
0
Re: SELECT statement
Try this:
MS SQL Syntax (Toggle Plain Text)
  1. IF OBJECT_ID('tempdb..#Test', 'U') IS NOT NULL DROP TABLE #TEST
  2. CREATE TABLE #Test
  3. (
  4. RecordId int identity(1000, 1) PRIMARY KEY,
  5. Value varchar(100)
  6. )
  7. SET NOCOUNT ON
  8. INSERT INTO #Test (Value) Values ('cse-12m')
  9. INSERT INTO #Test (Value) Values ('cse-343k')
  10. INSERT INTO #Test (Value) Values ('cse-mka')
  11. INSERT INTO #Test (Value) Values ('cse-ptu')
  12. INSERT INTO #Test (Value) Values ('cse-jpy')
  13. INSERT INTO #Test (Value) Values ('cse-123')
  14. SET NOCOUNT OFF
  15.  
  16. --#1 - Matches cse-NNN, Does NOT match cse-123k
  17. SELECT *
  18. FROM #Test
  19. WHERE Value LIKE 'cse-[0-9][0-9][0-9]'
  20.  
  21. --#2 - Matches cse-NNN*, so anything beginning with "cse-NNN" is matched
  22. SELECT *
  23. FROM #Test
  24. WHERE Value LIKE 'cse-[0-9][0-9][0-9]%'
  25.  
  26. --#3 - Invert #1
  27. SELECT *
  28. FROM #Test
  29. WHERE Value NOT LIKE 'cse-[0-9][0-9][0-9]'
  30.  
  31. --#4 - Invert #2
  32. SELECT *
  33. FROM #Test
  34. WHERE Value NOT LIKE 'cse-[0-9][0-9][0-9]%'
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Convertion of Column name into Rows
Next Thread in MS SQL Forum Timeline: Select only items that don't exist in another table





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC