SELECT statement

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2005
Posts: 92
Reputation: michael123 is an unknown quantity at this point 
Solved Threads: 0
michael123 michael123 is offline Offline
Junior Poster in Training

SELECT statement

 
0
  #1
Oct 16th, 2009
I have a table for example: tb1
with data:
  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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,251
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 578
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #2
Oct 17th, 2009
Try this:
  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]%'
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC