Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I would like to copy a certain range within q SQL table and input into new table. I also need to exclude part of the results.

I have seen on some forums and some SQL tutorial websites that they mostly use BETWEEN STATEMENT but this only can use in single condition.

My condition is copy the certain range with 2 conditions such as start copy with specific part number and end with type that is "A" but this data must be after ,so it can be a range.

UPDATED-- Below Link is the full question.

How to exclude a certain range in SQL results? - Stack Overflow[^]

Thanks All.

[EDIT - one way of showing sample data]
NO	TYPE	Part	Quantity	other
1	Z       1001	1	
2	A       1002	1	
3	T       1003	1	
4	C       1004	1	
5	F       1005	1	
6	V       1006	1	
7	C       1007	1	
8	B       1008	1	
9	D       1009	1	
10	S       1010	1	
11	V       1011	1	
12	Z       1012	1	
13	X       1013	1	
14	A       1014	1	
15	H       1015	1	
16	V       1016	1	
17	L       1017	1	
18	A       1018	1	

Another way of showing some sample data
SQL
create table demo (NO int identity(1,1)
,	TYPE	char(1)
,   Part	int
,	Quantity int
, other varchar(10) NULL)
insert into demo (TYPE, Part, Quantity) VALUES
('Z',	1001,	1	),
('A',	1002,	1	),
('T',	1003,	1	),
('C',	1004,	1	),
('F',	1005,	1	),
('V',	1006,	1	),
('C',	1007,	1	),
('B',	1008,	1	),
('D',	1009,	1	),
('S',	1010,	1	),
('V',	1011,	1	),
('Z',	1012,	1	),
('X',	1013,	1	),
('A',	1014,	1	),
('H',	1015,	1	),
('V',	1016,	1	),
('L',	1017,	1	),
('A',	1018,	1	)

Expected results
NO	TYPE	Part	Quantity	other
2	A       1002	1	
3	T       1003	1	
4	C       1004	1	
5	F       1005	1	
6	V       1006	1	
7	C       1007	1	
8	B       1008	1	
9	D       1009	1	
10	S       1010	1	
11	V       1011	1	
12	Z       1012	1	
13	X       1013	1	
14	A       1014	1


What I have tried:

I have seen on some forums and some SQL tutorial websites
Posted
Updated 8-Nov-18 1:12am
v3
Comments
CHill60 8-Nov-18 6:35am    
Don't be lazy - copy your full question here and get rid of the link!
Show us what you have tried.
At the very least give some sample data and your expected results
kind1212 8-Nov-18 6:44am    
sry, i also want to put full question at here, but it cant be attach any image.
CHill60 8-Nov-18 6:46am    
There is absolutely no need for an image for a question like this
kind1212 8-Nov-18 6:55am    
the image is the SQL table and the expected outcome.thanks.
CHill60 8-Nov-18 7:02am    
I know it is. That is not how you should present data and expected results. Help us to help you!
I have updated your post with your sample data and expected results.
Now you need to update your post with the actual logic that you applied to get your expected results, because the way you have explained it is not clear

1 solution

First work out the point at which you want to stop e.g.
SQL
DECLARE @endNO INT = (SELECT MIN(Part) FROM demo WHERE Part > 1002 AND [TYPE]='A')
PRINT @endNO
Which in this case returns 1014
Then plug that into your range for BETWEEN e.g.
SQL
SELECT   NO, TYPE, Part, Quantity, other 
FROM demo
WHERE Part BETWEEN 1002 AND @endNO
It all seems a bit contrived though and looks as if you are depending on the order of the part numbers. I'm not saying what you are doing is wrong, but it does seem a little strange.
 
Share this answer
 
v2
Comments
kind1212 8-Nov-18 7:25am    
very thank you!!!!! i will try it,and one small question is when set the part > 1002 if some time my part is not Ascending order or the part number format like 20-0-1,10-0-2 and so on.will "part >" can functional as well?
W Balboos, GHB 8-Nov-18 9:26am    
What you can take away from the above answer to the question and your own comment is that you need to plan your data storage carefully. The difference can be between making things easy and making things very difficult or impossible.

It is one of the most important steps.
CHill60 9-Nov-18 4:27am    
My virtual 5 sir! Well said.
kind1212 8-Nov-18 21:29pm    
i had tried this code, but it will come out the data that is error like return the data is out of the range.thanks.
CHill60 9-Nov-18 4:27am    
When run against the sample data you provided the code I supplied produced the expected results you provided. However all I have given you is the principle of how to do it as you described it.
Look at the comment from Balboos and my final comment in my solution ... you are making things difficult for yourself. You also appear to be trying to treat data as if it has an order within the table - this is a database not an excel spreadsheet! There should be no concept of "after" in the Part Numbers.
Perhaps if you explain what you are actually trying to achieve you will get a better answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900