patternsqlMinor
Replacing NULL values with one string from provided list
Viewed 0 times
withnullprovidedonereplacingvalueslistfromstring
Problem
I am trying to replace NULL values in table by using SELECT with one (randomized?) string from the set that I provide to the compiler.
Example:
I want the
Unfortunately
Thanks in advance.
Example:
|id | date |
+---+------+
| 1 | 2017 |
| 2 | NULL |
| 3 | NULL |I want the
NULL values to be '2015', '2012', etc, taken randomly from the set I specify.Unfortunately
COALESCE returns only first not null value rather than one from the specified. Is there some function that works like: IF column_value = 'NULL (or something)' THEN RAND(string1, string2, string3, string4).Thanks in advance.
Solution
You can use something like
The reference to
Select a column that is unique for this.
BTW: There is a
Don't use this
Because it is evaluated as
(And
SELECT Id,
ISNULL(Date, (SELECT TOP 1 *
FROM (VALUES(2015),
(2012)) R(Ryear)
ORDER BY CRYPT_GEN_RANDOM(DATALENGTH(Id))))
FROM YourTableThe reference to
Id in the ORDER BY is just to make it correlated so that SQL Server is less likely to spool the result and replay it for multiple rows.Select a column that is unique for this.
BTW: There is a
CHOOSE function that looks tempting but when this gets expanded out to CASE the random number function gets copied out too and so this is not suitable for the task.Don't use this
SELECT Id,
CHOOSE(1 + ABS(CRYPT_GEN_RANDOM(4) % 2),2015,2012)
FROM YourTableBecause it is evaluated as
CASE
WHEN (1 + ABS(CRYPT_GEN_RANDOM(4) % 2)) = 1
THEN 2015
ELSE
CASE
WHEN (1 + ABS(CRYPT_GEN_RANDOM(4) % 2)) = 2
THEN 2012
ELSE NULL
END
END(And
CASE ABS(CRYPT_GEN_RANDOM(4) % 2) WHEN 0 THEN 2012 WHEN 1 THEN 2015 END would have the same problem)Code Snippets
SELECT Id,
ISNULL(Date, (SELECT TOP 1 *
FROM (VALUES(2015),
(2012)) R(Ryear)
ORDER BY CRYPT_GEN_RANDOM(DATALENGTH(Id))))
FROM YourTableSELECT Id,
CHOOSE(1 + ABS(CRYPT_GEN_RANDOM(4) % 2),2015,2012)
FROM YourTableCASE
WHEN (1 + ABS(CRYPT_GEN_RANDOM(4) % 2)) = 1
THEN 2015
ELSE
CASE
WHEN (1 + ABS(CRYPT_GEN_RANDOM(4) % 2)) = 2
THEN 2012
ELSE NULL
END
ENDContext
StackExchange Database Administrators Q#191621, answer score: 7
Revisions (0)
No revisions yet.