How to generate Null or int in a column? - sql-server-2005

I need to fill null or int randomly in an int field.
How ??

OK, this may be a partial answer but...
SELECT CASE WHEN RAND() > 0.5 THEN 'YourInt' ELSE NULL END
Will give you either 'YouInt' or NULL based on a random value.
You could then perform an INSERT like this...
INSERT INTO MyTable
SELECT CASE WHEN RAND() > 0.5 THEN 'YourInt' ELSE NULL END
Or, of course an UPDATE
UPDATE MyTable
SET MyField = CASE WHEN RAND() > 0.5 THEN 'YourInt' ELSE NULL END
WHERE MyCondition = MyCriteria
But it does not generate an INT for you. Do you already have some INT's somewhere or do you need to generate them?

Related

Choose multiple cases condition

I want to get multipule choises after then in case statment as
#value
select * from [dbo].[Currency_Tbl]
WHERE [Currency_Active_YN]=
CASE WHEN #value = 1 THEN
( 1 or 0)
ELSE
#Value = 0 then 0
END
it didn't accept the first line in col1 but accept the col2
how can I select multiple numbers after THEN?
You don't use case in where clauses. Use boolean logic
select * from [dbo].[Currency_Tbl]
WHERE (#value = 1 and [Currency_Active_YN] in (0,1))
OR (#value = 0 and [Currency_Active_YN] = 0)
You dont need a case to do what you're trying to do. Assuming Currency_Active_YN is a not null bit field the following logic should suffice.
select * from [dbo].[Currency_Tbl]
WHERE (#value=1 OR [Currency_Active_YN]=#Value)

How to set bool value in SQL

I have a column which is bool. How can I set true, false values for that? Here is my query :
Update [mydb].[dbo].[myTable]
SET isTrue =
(
CASE WHEN Name = 'Jason' THEN 1
END
)
I don't know what to write after THEN keyword. Should I write 1 or true or 1 AS BIT or something else?
Sql server does not expose a boolean data type which can be used in queries.
Instead, it has a bit data type where the possible values are 0 or 1.
So to answer your question, you should use 1 to indicate a true value, 0 to indicate a false value, or null to indicate an unknown value.
Update [mydb].[dbo].[myTable]
SET isTrue =
CASE WHEN Name = 'Jason' THEN
1
ELSE
0
END
The query you added will work fine, but it will you have to take care of "FALSE" part as well otherwise it will try to enter NULL in your column.
Do you have any default value constrain on isTrue column?
Update [mydb].[dbo].[myTable]
SET isTrue =
(
CASE WHEN Name = 'Jason' THEN 1 ELSE 0
END
)
You need case statement with when and else if not any condition satisfied
Update [mydb].[dbo].[myTable]
SET isTrue = ( CASE WHEN Name = 'Jason'
THEN 1 else 0
END)
Use IIF function of sql server
DECLARE #a int = 45, #b int = 40;
DECLARE #a int = 45, #b int = 40;
SELECT IIF ( #a > #b, 'TRUE', 'FALSE' ) AS Result;
Result
--------
TRUE
(1 row(s) affected)
For Your Problem
Update [mydb].[dbo].[myTable]
SET isTrue = ( Name = 'Jason', 'TRUE', 'FALSE' )

Change aggregate functions to output NULL when a element is NULL

Every question I search for about the warning
Warning: Null value is eliminated by an aggregate or other SET operation.
Typically people want to treat the NULL values as 0. I want the opposite, how do I modify the following stored procedure to make it return NULL instead of 1?
CREATE PROCEDURE TestProcedure
AS
BEGIN
select cast(null as int) as foo into #tmp
insert into #tmp values (1)
select sum(foo) from #tmp
END
GO
I thought it would be SET ANSI_NULLS ON (I tried before the declaration, within the procedure itself, and before executing the procedure in my test query) but that did not appear to change the behavior of SUM(.
The sum() function automatically ignores NULL. To do what you want, you need an explicit checK:
select (case when count(foo) = count(*) then sum(foo) end)
from #tmp;
If you want to be explicit, you could add else NULL to the case statement.
The logic behind this is that count(foo) counts the number of non-NULL values in foo. If this is equal to all the rows, then all the values are non-NULL. You could use the more verbose:
select (case when sum(case when foo is null then 1 else 0 end) > 0
then sum(foo)
end)
And, I want to point out that the title is quite misleading. 1 + NULL = NULL. The issue is with the aggregation functions, not the arithmetic operators.
Looking for a null value with EXISTS may be the fastest:
SELECT
CASE WHEN EXISTS(SELECT NULL FROM tmp WHERE foo IS NULL)
THEN NULL
ELSE (SELECT sum(foo) from tmp)
END
Just say
select case sign(sum(case when foo is null then 1 else 0 end))
when 1 then null
else sum(foo)
end
from some_table
...
group by
...
That's about all you need.

Use comparison signs inside a sql case statement

I'm looking for a way to build case statements in a sql select query using less than and greater than signs. For example, I want to select a ranking based on a variable:
DECLARE #a INT
SET #a = 0
SELECT CASE
WHEN #a < 3 THEN 0
WHEN #a = 3 THEN 1
WHEN #a > 3 THEN 2
END
I'd like to write it as:
DECLARE #a INT
SET #a = 0
SELECT CASE #a
WHEN < 3 THEN 0
WHEN 3 THEN 1
WHEN > 3 THEN 2
END
...but SQL doesn't let me use the < and > signs in this way. Is there a way that I can do this is SQL 2005, or do I need to use the code like in the first one.
The reason for only wanting the code there once is because it would make the code a lot more readable/maintainable and also because I'm not sure if SQL server will have to run the calculation for each CASE statement.
I'm looking for a VB.NET case statement equivelent:
Select Case i
Case Is < 100
p = 1
Case Is >= 100
p = 2
End Select
Maybe it's not possible in SQL and that's ok, I just want to confirm that.
You can use the SIGN function as
DECLARE #a INT
SET #a = 0
SELECT CASE SIGN(#a - 3)
WHEN -1 THEN 0
WHEN 0 THEN 1
WHEN 1 THEN 2
END
If #a is smaller than 3, then #a - 3 results in a negative int, in which SIGN returns -1.
If #a is 3 or greater, then SIGN returns 0 or 1, respectively.
If the output you want is 0, 1 and 2, then you can simplify even more:
DECLARE #a INT
SET #a = 0
SELECT SIGN(#a - 3) + 1
Using SIGN as suggested by #Jose Rui Santos seems a nice workaround. An alternative could be to assign the expression an alias, use a subselect and test the expression (using its alias) in the outer select:
SELECT
…,
CASE
WHEN expr < 3 THEN …
WHEN expr > 3 THEN …
END AS …
FROM (
SELECT
…,
a complex expression AS expr
FROM …
…
)
SELECT
CASE
WHEN ColumnName >=1 and ColumnName <=1 THEN 'Fail'
WHEN ColumnName >=6 THEN 'Pass'
ELSE 'Test'
END
FROM TableName

How to create a ternary condition on a bit field in T-SQL

I have a SQLExpress table that includes a bit field for storing TRUE/FALSE state.
Something like:
+----+---------+
| ID | IsAlive |
+----+---------+
| 1 | 1 |
| 2 | 0 |
| 3 | NULL |
| 4 | 1 |
+----+---------+
Using that table as our example, I want to create one Stored Procedure that will do any one of the following:
Retrieve all records.
Retrieve only the records with IsAlive=1.
Retrieve only the records with IsAlive=0 or NULL.
I am trying to think of how I can create my query without having to write IF/ELSE conditions - It seems to me there is a better/cleaner way than to do something like this:
-- The ternary logic...
-- 0 or NULL retrieves records where IsAlive = 0 or NULL
-- 1 retrieves records where IsAlive = 1
-- Otherwise return all records
-- sproc .....
#IsAlive tinyint = 2 -- Return all records by default
AS
BEGIN
IF(#SentToNTService = 0 OR #SentToNTService = 1)
BEGIN
SELECT *
FROM MyTable
WHERE IsAlive = #IsAlive;
END
ELSE -- Lame redundancy
BEGIN
SELECT *
FROM MyTable
END
END
Is there another way of creating the same results without having to create two different queries as I did above?
2 suggestions of how to do this:
Assuming your variable #isalive is declared as 'bit' as well (which is should be)
SELECT * FROM #t
WHERE #isalive is null or #isalive = coalesce(isalive, 0)
If you want to use a 'bit compare' solution that doesn't require #isalive to be 'bit' (it will work for bit as well as tinyint)
SELECT * FROM #t
WHERE coalesce((1-coalesce(isalive, 0)) ^ #isalive, 1) > 0
Second solution is for nerds like me. Some hardcore people may find it interesting (or at least amusing) as I think it offer the best possible performance (please, someone correct me if i am wrong). It is a powerful solution but hard to read.
This will do what you want:
SELECT *
FROM MyTable
WHERE COALESCE(IsAlive, 0) = COALESCE(#IsAlive, COALESCE(IsAlive, 0))
Based on the value of #IsAlive:
If NULL, then will return everything (because the condition is always true)
If 1, then will return those rows with IsAlive = 1
If 0, then will return those rows with IsAlive = 0 or NULL
COALESCE is a function that returns it's first argument, unless it's NULL, in which case it returns its second argument.
So the LHS returns 0 if IsAlive is NULL or 0 and 1 if IsAlive is 1.
The RHS returns the same when the stored procedure argument #IsAlive is NULL and just returns the #IsAlive argument otherwise.
EDIT:
This assumed that #IsAlive is BIT. In the case of tinyint you can add a case statement:
SELECT *
FROM MyTable
WHERE COALESCE(IsAlive, 0) = CASE #IsAlive
WHEN 0 THEN 0
WHEN 1 THEN 1
ELSE COALESCE(IsAlive, 0)
END
try this:
SELECT * FROM MyTable WHERE ISNULL (IsAlive, 0) = ISNULL (#IsAlive, 0)
UNION
SELECT * FROM MyTable WHERE ISNULL (#IsAlive, 0) > 1
This isnt exact, but pretty close to what you can do:
SELECT *
FROM MyTable
WHERE CASE #IsAlive
WHEN 0 THEN IsAlive = #IsAlive
WHEN 1 THEN IsAlive = #IsAlive
ELSE 1=1 --dummy true value, when null or anything else
END
Something like this should also work.
SELECT *
FROM MyTable
WHERE (#IsAlive = 0 and IsAlive=0)
OR (#IsAlive =1 and IsAlive =1)
OR (#IsAlive is null)