mardi 24 juillet 2018

How to select a random string from a list of strings in SQL Server

Im trying to generate some dummy data and need to re-apply the logic on every new row.

For Example, The words are 'Comm' and 'Resi'.

The query I am using is as follows;

    SELECT SubSkill = (
    SELECT TOP 1 Name
    FROM (
        SELECT RandomGUID = cast(round(RAND(CHECKSUM(NEWID())) * (1 - 10) + 10, 2) AS INT) 
            ,'Comm' AS Name 
    UNION ALL   
        SELECT cast(round(RAND(CHECKSUM(NEWID())) * (1 - 10) + 10, 2) AS INT)
            ,'Resi'
        ) NAMES
    ORDER BY RandomGUID
    )
,* FROM Sales2

However this gives me either 'Comm' for every record, or 'Resi' for every record on execution. I need it to be randomly selected on each row.

Any ideas?




Aucun commentaire:

Enregistrer un commentaire