Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: Update a colum with Random Value

I have a table Product and I have a column in it called Genre which has null/unwanted values.

I want to update that column with a set of values:

Documentary
Comedy
Adventure/Action
Drama
Thriller
SF/Fantasy
Animation/Family
Others

The update can be in any order but I want every row in the column updated. How should I go about this?

like image 585
Tauseef Hussain Avatar asked Aug 01 '26 06:08

Tauseef Hussain


1 Answers

Try something like this

UPDATE P
SET    genre = rand_values
FROM   Product p
       CROSS apply (SELECT TOP 1 rand_values
                    FROM   (VALUES ('Documentary'),
                                   ('Comedy'),
                                   ('Adventure/Action'),
                                   ('Drama'),
                                   ('Thriller'),
                                   ('SF/Fantasy'),
                                   ('Animation/Family'),
                                   ('Others')) tc (rand_values)
                    WHERE  p.productid = p.productid -- This is just to correlate the query 
                    ORDER  BY Newid()) cs 
like image 181
Pரதீப் Avatar answered Aug 03 '26 21:08

Pரதீப்



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!