Sql Query for range - sql

i am developing a website
there are two form searchscreen and serachresultscreen
in first form i select a range from two dropdown list (i.e From value1 to value 2)
range is for diamond clarity like in order respectively
FL,IF,VVS,VVS1,VVS2,SI,SI1,I1,I2
when i range query it on next form like
clarity>=FL and clarity<=VVS1
then it show result with all values between alphabet F to V (i.e FL,IF,VVS,VVS1,VV2,SI,I1,I2
please suggest me some answere
My required output is(when selected between from=FL to=VVS1)
then result output value FL,IF,VVS,VVS1 only

You can not use range operator BETWEEN for your String values. To address this issue create table with corresponding numeric weighting for those Diamond Clarity values.
while filtering based on Clarity fetch its order and then put all those ORDER (Numeric Number)
within Range Operator
EDIT :
CREATE TABLE DIAMOND_CLARITY
(CLARITY VARCHAR(10),
CLARITY_ORDER int
)
INSERT INTO DIAMOND_CLARITY values ('FL',1)
INSERT INTO DIAMOND_CLARITY values ('IF',2)
INSERT INTO DIAMOND_CLARITY values ('VVS',3)
INSERT INTO DIAMOND_CLARITY values ('VVS1',4)
INSERT INTO DIAMOND_CLARITY values ('VVS2',5)
INSERT INTO DIAMOND_CLARITY values ('SI',6)
INSERT INTO DIAMOND_CLARITY values ('SI1',7)
INSERT INTO DIAMOND_CLARITY values ('I1',8)
INSERT INTO DIAMOND_CLARITY values ('I2',9)
Query to fetch records between ranges
SELECT CLARITY from DIAMOND_CLARITY where CLARITY_ORDER
BETWEEN
(SELECT CLARITY_ORDER FROM DIAMOND_CLARITY where CLARITY='FL')
AND
(SELECT CLARITY_ORDER FROM DIAMOND_CLARITY where CLARITY='VVS')

Related

Sql, validation to check a list allowing a delimiter

I want to create a validation in SQL that will check for a value or combination of values that can be delimited with a semi-colon from a list of 3 possible combinations of 'Apples''Oranges' 'Bananas'.
In the example below the validation should pass for the first options but fail for the last
CREATE TABLE Test1(A varchar (50));
INSERT INTO Test1 VALUES ('Apples;Oranges;Bananas');
INSERT INTO Test1 VALUES ('Apples');
INSERT INTO Test1 VALUES ('Apples;Eggs');

How to write a WHERE clause for NULL value in ARRAY type column?

I created a table which contains a column of string ARRAY type as:
CREATE TABLE test
(
id integer NOT NULL,
list text[] COLLATE pg_catalog."default",
CONSTRAINT test_pkey PRIMARY KEY (id)
)
I then added rows which contain various values for that array, including an empty array and missing data (null):
insert into test (id, list) values (1, array['one', 'two', 'three']);
insert into test (id, list) values (2, array['four']);
insert into test (id, list) values (3, array['']);
insert into test (id, list) values (4, array[]::text[]); // empty array
insert into test (id, list) values (5, null); // missing value
pgAdmin shows table like this:
I am trying to get a row which contains a null value ([null]) in the list column but:
select * from test where list = null;
...returns no rows and:
select * from test where list = '{}';
...returns row with id = 4.
How to write WHERE clause which would target NULL value for column of ARRAY type?
demo:db<>fiddle
... WHERE list IS NULL
select * from test where list IS null;
Like this:
select * from test where list IS NULL;

Oracle SQL Plus not all table columns displayed

I've created this table :
CREATE TABLE PRODUCT (
P_CODE CHAR(8),
P_DESCRIPT CHAR(200),
P_INDATE CHAR(20),
P_ONHAND INTEGER,
P_MIN INTEGER,
P_PRICE NUMBER,
P_DISCOUNT NUMBER,
V_CODE CHAR(5)
);
And I've inserted the following data in the table:
INSERT INTO PRODUCT VALUES ('11QER/31','Power painter, 15 psi, 3-nozzle','12/2/96',8,5,109.99,0.00,25595);
INSERT INTO PRODUCT VALUES ('13-Q2/P2','7.25-in. pwr. Saw blade','11/12/96',32,15,14.99,0.05,21344);
INSERT INTO PRODUCT VALUES ('14-Q1/L3','9.00-in. pwr. Saw blade','11/12/96',18,12,17.49,0.00,21344);
INSERT INTO PRODUCT VALUES ('156-QQ2','Hrd. Cloth, 1/4-in., 2x50','8/14/96',15,8,39.95,0.00,23119);
INSERT INTO PRODUCT VALUES ('1558-QW1','Hrd. Cloth, 1/2-in., 3x50','8/14/96',23,5,43.99,0.00,23119);
INSERT INTO PRODUCT VALUES ('2232/QTY','B&D jigsaw, 12-in, blade','10/29/96',8,5,109.92,0.05,24288);
INSERT INTO PRODUCT VALUES ('2232/QWE','B&D jigsaw, 8-in, blade','9/23/96',6,5,99.87,0.05,24288);
INSERT INTO PRODUCT VALUES ('2238/QPD','B&D cordless drill, 1/2-in.','10/19/96',12,5,38.95,0.05,25595);
INSERT INTO PRODUCT VALUES ('23109-HB','Claw hammer','11/19/96',23,10,9.95,0.10,21225);
INSERT INTO PRODUCT VALUES ('23114-AA','Sledge hammer, 12 lb.','12/1/96',8,5,14.40,0.05,null);
INSERT INTO PRODUCT VALUES ('54778-2T','Rat-tail file, 1/8-in. fine','6/14/96',43,20,4.99,0.00,'21344');
INSERT INTO PRODUCT VALUES ('89-WRE-Q','Hicut chain saw, 16 in.','7/6/96',11,5,256.99,0.05,'24288');
INSERT INTO PRODUCT VALUES ('PVC23DAT','PVC pipe, 3.5-in., 8-ft','12/19/96',188,75,5.87,0.00,null);
INSERT INTO PRODUCT VALUES ('SM-18277','1.25-in. metal screw, 25','11/28/96',172,75,6.99,0.00,'21225');
INSERT INTO PRODUCT VALUES ('SW-23116','2.5-in. wd. screw, 50','9/23/96',237,100,8.45,0.00,'21231');
INSERT INTO PRODUCT VALUES ('WR3/TT3','Steel matting, 4''x8''x1/6", .5" mesh','11/16/96',18,5,119.95,0.10,'25595');
INSERT INTO PRODUCT VALUES ('11QER/31','Power painter, 15 psi, 3-nozzle','12/2/96',8,5,109.99,0.00,25595);
INSERT INTO PRODUCT VALUES ('13-Q2/P2','7.25-in. pwr. Saw blade','11/12/96',32,15,14.99,0.05,21344);
INSERT INTO PRODUCT VALUES ('14-Q1/L3','9.00-in. pwr. Saw blade','11/12/96',18,12,17.49,0.00,21344);
INSERT INTO PRODUCT VALUES ('156-QQ2','Hrd. Cloth, 1/4-in., 2x50','8/14/96',15,8,39.95,0.00,23119);
INSERT INTO PRODUCT VALUES ('1558-QW1','Hrd. Cloth, 1/2-in., 3x50','8/14/96',23,5,43.99,0.00,23119);
INSERT INTO PRODUCT VALUES ('2232/QTY','B&D jigsaw, 12-in, blade','10/29/96',8,5,109.92,0.05,24288);
INSERT INTO PRODUCT VALUES ('2232/QWE','B&D jigsaw, 8-in, blade','9/23/96',6,5,99.87,0.05,24288);
INSERT INTO PRODUCT VALUES ('2238/QPD','B&D cordless drill, 1/2-in.','10/19/96',12,5,38.95,0.05,25595);
INSERT INTO PRODUCT VALUES ('23109-HB','Claw hammer','11/19/96',23,10,9.95,0.10,21225);
INSERT INTO PRODUCT VALUES ('23114-AA','Sledge hammer, 12 lb.','12/1/96',8,5,14.40,0.05,null);
INSERT INTO PRODUCT VALUES ('54778-2T','Rat-tail file, 1/8-in. fine','6/14/96',43,20,4.99,0.00,'21344');
INSERT INTO PRODUCT VALUES ('89-WRE-Q','Hicut chain saw, 16 in.','7/6/96',11,5,256.99,0.05,'24288');
INSERT INTO PRODUCT VALUES ('PVC23DAT','PVC pipe, 3.5-in., 8-ft','12/19/96',188,75,5.87,0.00,null);
INSERT INTO PRODUCT VALUES ('SM-18277','1.25-in. metal screw, 25','11/28/96',172,75,6.99,0.00,'21225');
INSERT INTO PRODUCT VALUES ('SW-23116','2.5-in. wd. screw, 50','9/23/96',237,100,8.45,0.00,'21231');
INSERT INTO PRODUCT VALUES ('WR3/TT3','Steel matting, 4''x8''x1/6", .5" mesh','11/16/96',18,5,119.95,0.10,'25595');
now when I do the following:
SELECT * FROM PRODUCT;
only the first 4 columns is returned!
I tried setting the following:
SET WRAP ON
SET TERMOUT OFF
SET VERIFY OFF
SET TRIMSPOOL OFF
SET LINESIZE 2000
SET LONG 200000
SET PAGES 0
And now only two columns appear.
Please help.
If the column should be CHAR(200), then you should leave it alone - don't change it to CHAR(50) only because you can't display it properly.
Instead, you are probably OK by showing only the first 50 characters when you query the table. You could do that in the query itself, like this:
select p_code, substr(p_descript, 1, 50) as p_descript, p_indate, ...
Alternatively, you can tell SQL Plus to display the column in 50-character width, with a SQL Plus command - you were trying to do that, you just didn't find the right command. It is the column command; specifically in this case:
column p_descript format a50
(note this is NOT followed by semicolon ; since it is not a SQL command) This will wrap the values that are actually longer than 50 characters. If instead you want them to be truncated, issue an additional SQL Plus command:
set wrap off

Confusion about Stored Procedure

I have written a stored procedure for inserting data into my table. These are my table's columns with their datatype:
Ad nvarchar(150),
Yazar nvarchar(150),
SayfaSayisi smallint,
KategoriId int
Gmc datetime,
HostName nvarchar(150)
The problem is that Gmc and HostName have their own default values. So I can't use these two in the stored procedure.
Gmc ---> GetDate() (to get insert date)
HostName --> Host_Name( )
So when I execute the query I am getting this error.
There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement
This is the query
Create proc Kitap_Insert
#Ad nvarchar(150),
#Yazar nvarchar(150),
#SayfaSayisi smallint,
#KategoriId int
Gmc datetime,
HostName nvarchar(150)
as
Insert into Kitap(Id, Ad, Yazar, SayfaSayisi, KategoriId)
values(#Ad, #Yazar, #SayfaSayisi, #KategoriId)
What is the proper way of doing this?
You need remove ID from insert list
Insert into Kitap(Ad,Yazar,SayfaSayisi,KategoriId)
values(#Ad,#Yazar,#SayfaSayisi,#KategoriId)
or add a value for it as below
Insert into Kitap(Id,Ad,Yazar,SayfaSayisi,KategoriId)
values(#ID, #Ad,#Yazar,#SayfaSayisi,#KategoriId)
Instead of :
Insert into Kitap(Id,Ad,Yazar,SayfaSayisi,KategoriId)
values(#Ad,#Yazar,#SayfaSayisi,#KategoriId)
Use:
INSERT INTO Kitap(Ad,Yazar,SayfaSayisi,KategoriId)
VALUES (#Ad,#Yazar,#SayfaSayisi,#KategoriId)
You are asking SQL engine that you will provide id (an additional field) as well (field that doesn't exist in the table or is an auto increment field) and you are not providing the value for the same and hence your error here are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement
So remove additional Id from your insert query.
The error you are getting because you tried to insert value into more column names than specified in Values Clause.
If you have ID column as Auto-increment field in table so you dont have to include that ID column in so your insert query will be like this:-
Insert into Kitap
(Ad,Yazar,SayfaSayisi,KategoriId)
values
(#Ad,#Yazar,#SayfaSayisi,#KategoriId)
If you don't have ID column as Auto-increment field in table so you provide value to that id column also in Value Clause so your insert query will be like this:-
NOTE:-
You have to calculate and Set Value to #Id variable before using it in Insert Query
Declare #Id as INT
SET #ID = ---- set here with some value which will become Primary key(I think)
Insert into Kitap
(Id,Ad,Yazar,SayfaSayisi,KategoriId)
values
(#Id, #Ad,#Yazar,#SayfaSayisi,#KategoriId)

SSRS: How do I display a list of customers, from A - K then L to Z, without having two separate reports?

I would like one report with a drop down menu, showing:
Customers A-K,
Customers L-Z
All Customers
I have an SQL table for customers, with the standard columns (Acc No., name, address, etc).
I would like this split by name as it takes a long time to bring the result of the entire table down.
I would prefer this to be in one report. Is this possible? I am using SQL Server 2008 R2 with reporting services.
I have tried a few different methods with no luck, so I'm open to any suggestions!
Thanks for reading my question and thanks in advance if you can find the time to help!
Please feel free to ask any questions.
create table #alphab
( alphaname varchar(15));
insert into #alphab VALUES ('ANT')
insert into #alphab values ('CAT')
insert into #alphab values ('pAT')
insert into #alphab values ('mAT')
insert into #alphab values ('dAT')
insert into #alphab values ('rAT')
insert into #alphab values ('dAT')
insert into #alphab values ('lAT')
insert into #alphab values ('cAT')
insert into #alphab values ('zAT')
insert into #alphab values ('xAT')
insert into #alphab values ('wAT')
insert into #alphab values ('oAT')
insert into #alphab values ('sAT')
insert into #alphab values ('yAT')
insert into #alphab values ('uAT')
select alphaname
from #alphab
where alphaname LIKE
CASE
WHEN #alphabetorder = 1 THEN ('[A-K | a-k]%')
WHEN #alphabetorder= 2 THEN ('[L-Z | l-z]%')
END
order by alphaname