I have a table with 4 columns what I need to do is a query that set values to SP_REFSTART2, SP_REFSTOP2 if SP_REFSTART and SP_REFSTOP ARE NOT NULL else if the 2 first columns are null I want those values to fill the first two columns.
SP_REFSTART | SP_REFSTOP | SP_REFSTART2 | SP_REFSTOP2
1 1 null null
null null 1 1
Thats a sample of the table.
I want to get values 1 and 1 on the 3rd and 4th column since first 2 are not null
but if there were null I want 1 and 1 in the first 2 (row 2nd example).
UPDATE STORETRADELINES
SET SP_REFRSTART2 = CASE WHEN SP_REFRSTART IS NOT NULL THEN 1 ELSE NULL END
I did that my it will take me many lines since the columns are more than the example so I was thinking if there is any if to set multiple values.
Do you simply want coalesce()?
update STORETRADELINES
set SP_REFSTART = coalesce(SP_REFSTART, SP_REFSTART2),
SP_REFSTOP = coalesce(SP_REFSTOP, SP_REFSTOP2),
SP_REFSTART2 = coalesce(SP_REFSTART2, SP_REFSTART),
SP_REFSTOP2 = coalesce(SP_REFSTOP2, SP_REFSTOP)
Related
I'm writing a sql constraint about values on columns based on some conditions in Oracle database. My table is like below.(assume id is auto increment, also 'alpha' and 'beta' columns are numbers)
id alpha beta
--------------------------
1 1 0
2 1 1
3 0 0
4 0 0
5 2 3
6 4 1
If alpha value in two rows are same, only one row can be inserted with beta value of 1. In other words, i shouldn't insert a row with (1,1) values because there is already a row with beta value of 1.(look at the row with id=2). Any value besides 1 can be inserted freely. I need a useful control about that situation.
You can use a function-based index:
create unique index xxx on t(a, case when b = 1 then -1 else id end)
Here is a db<>fiddle.
You can use unique index with condition-based column as follows:
create unique index u123
on your_table (alpha, case when beta = 1 then beta else id end)
I'm using SQL Server 2016 and I have a view setup for novice end users.
To start, let's say there is a table like the following:
id number
=========
1 2
2 4
3 6
4 NULL
5 12
If a user makes a query on the view such as, select * from view1 where number <> 12, the view is setup to return NULL values as -99 using coalesce(number,-99):
Result of 'select * from view1 where number <> 12':
id number
=========
1 2
2 4
3 6
4 -99
Is there anyway to have the view return NULL instead of -99 (or whatever value), without the end user having to include ... or where is null in their query?
I understand NULLs and why it behaves like this, but for convenience I'd rather these end users not have to do this.
No.
The best you can do is fix the result so it decodes -99 as NULL:
SELECT id, CASE WHEN number = -99 THEN NULL ELSE number END AS number
FROM view1
WHERE number != 12
which I believe defeats the purpose of not exposing NULL values to the end user, or approach the data by accounting NULL as a valid data, using OR number IS NULL in that matter.
Try this:
select * from view1 where number <> 12 or number is null
I need to compare the cells of a column of a table based on the value of another column value in sql.
Id A B
1 Ram 50
2 Ram 50
3 Siva 123
4 Siva 25
5 Rose 75
6 Rose 75
7 Siva 123
I have the above table, i need to check whether Ram in Column A has same value in column B, if not i should return false.
In the above case it should return false as Siva in(Column A) has different values in Column B.
I don't know how to do this. Kindly help on the same.
Result Expected
Return "False", as there is value mismatch for Siva
Here is one trick
select A,
case when min(B)=max(B) then 'True' else 'False' end as Flag
From yourtable
Group by A
or If you want to display the flag column for every row then
select A,
case when min(B)over(partition by A)=max(B)over(partition by A) then 'True' else 'False' end as Flag
From yourtable
Update : If you want to display False if at least one mismatch is present then
SELECT CASE
WHEN EXISTS(SELECT 1
FROM yourtable
GROUP BY A
HAVING Min(B) <> Max(B)) THEN 'False'
ELSE 'True'
END
I wrote the following SQL to create a column that I can use to populate check boxes in a Grid to manage user permissions.
SELECT access_b2b.access_id,
access_b2b.description,
'active'= CASE
WHEN access_group.group_id IS NOT NULL THEN 1
ELSE 0
END
FROM access_b2b
LEFT JOIN access_group
ON access_group.access_id = access_b2b.access_id
WHERE ( access_group.group_id = 10
OR access_group.group_id IS NULL )
However, it does not select all of the entries from access_b2b. The issues is with the last line:
where (access_group.group_id=10 or access_group.group_id is null)
Without it, i get duplicate entries returned with different active values. Also, I realized that this is not the proper condition, because an entry in access_group might exist for a different access_group.group_id, meaning that not all the remaining entries will be pulled in with the access_group.group_id is null.
I am trying to write my condition so that if does something along the lines of:
This is the format I was trying to follow:
Where For Each unique access_id in access_group
select the one where group_id=10
if no group_id=10
select any other one
end
end
Ultimately, the goal is to have a column returned with 1 or 0 denoting if the access_id exists for a predetermined group id.
Please note that throughout this explanation I used group_id=10 for simplification, it will be later replaced with a SqlParameter.
Any help is appreciated, thank you so much!
SAMPLE DATA (only useful columns shown to simplify data)
access_group
access_id group_id
27 1
27 11
28 1
28 11
33 1
33 3
33 11
43 11
44 1
44 10
44 11
...
access_b2b
access_id description
1 Add
2 Edit
3 Delete
4 List
5 Payments
6 Open Files
7 Order
8 Mod
...
Change the query to and it should work:
SELECT access_b2b.access_id,
access_b2b.description,
'active'= CASE
WHEN access_group.group_id IS NOT NULL THEN 1
ELSE 0
END
FROM access_b2b
LEFT JOIN access_group
ON access_group.access_id = access_b2b.access_id
AND ( access_group.group_id = 10
OR access_group.group_id IS NULL )
If you don't want the records to be filtered by the WHERE clause, move the condition in the JOIN.
The JOIN will keep the lines and populate them with NULL if the condition is not met, while the WHERE clause will filter the result set.
New to stackoverflow and SQL, so please be gentle. I'm attempting to create a stored proc that will change variable value from 1 to 0 for every nth row of identity column in table
SET #randomBit = IF(SELECT ID FROM [dbo].[js_xxxx]
WHERE ID%5 AND ID > 0) THEN SET #randomBit = 0 ELSE 1 END
The purpose #randomBit value is to be used to set a bit value, which will then be combined with other fields, then use a while to loop 50 times and then insert into a table.
I
Below would be the ouptut:
Uni
1
1
1
1
0
1
1
SELECT id, CASE WHEN ID%5=0 THEN 1 else 0 end [RandomBit] FROM [dbo].[js_xxxx]
This will give you every 5th ID as 1