? Earlier 8 items total Later ?

On this page:?

find duplicates

SELECT column,COUNT(*) c FROM table
GROUP BY column HAVING COUNT(*) > 1

results show the column value and the number of duplicates for that value

Dynamic ordering without dynamic sql

DECLARE @order int
SET @order = 1
SELECT * FROM users
ORDER BY 
        CASE WHEN @order = 1 THEN [name] END DESC,
        CASE WHEN @order = 2 THEN [name] END ASC,
        CASE WHEN @order = 3 THEN [email] END DESC,
        CASE WHEN @order = 4 THEN [email] END ASC

set the @order to 1-4 to get specific order by clause, setting it to anything else give results as if no order by clause supplied

remember dynamic SQL (creating SQL in a string and then executing) can not be effectively cached so this is a much better option

Randomly ordered results in TSQL

SELECT TOP 10 userID
FROM user
ORDER BY NEWID()

or
SELECT TOP 10
        NEWID() [egg],
        userID
FROM user
ORDER BY [egg]

or
SELECT TOP 10
        NEWID(),
        userID
FROM user
ORDER BY 1

only tried the above with TSQL

SQL UPDATE FROM example

UPDATE emailFormat 
SET format=2 
FROM emailFormat EF
INNER JOIN user U ON EF.userID = U.userID 

I have seen people use temporay tables and loops to do this sort of thing in the past

UDF to add a sql_variant to a list

--Adds a sql_variant element to the end of a sql_variant list, after first inserting a delimiter (nvarchar)
--If both element and list are nvarchars, using dbo.fnAddToList will be faster
ALTER FUNCTION dbo.fnAddVarToList (@VarList sql_variant, @VarNew sql_variant, @Del nvarchar(10))
RETURNS nvarchar(4000)
AS  
BEGIN 
        DECLARE @List nvarchar(4000), @New nvarchar(4000)
        SELECT @List = NULLIF(CONVERT(nvarchar(4000), @VarList), ''), 
                @New = NULLIF(CONVERT(nvarchar(4000), @VarNew), '')
        --First try the concatened string, if null then just the list, 
        --if it too is null, just the new element
        RETURN COALESCE(@List + @Del + @New, @List, @New)
END

UDF to add items to a list, specifying the delimiter

--Adds an element (nvarchar) to the end of a list (nvarchar), after first inserting a delimiter (nvarchar)
ALTER FUNCTION dbo.fnAddToList (@List nvarchar(4000), @New nvarchar(4000), @Del nvarchar(10))
RETURNS nvarchar(4000)
AS  
BEGIN 
        --Treat ''s as NULLs
        SELECT @List = NULLIF(@List, ''), @Del = NULLIF(@Del, ''), @New = NULLIF(@New, '')
        --First try the concatened string, if null then just the list, 
        --if it too is null, just the new element
        RETURN COALESCE(@List + @Del + @New, @List, @New)
END

UDF to Add items to a comma delimited list

/*
_A_dd to _C_omma delimited _L_ist - simplified version of AddToList
Adds an element (nvarchar) to the end of a comma delimited list (nvarchar)
*/
ALTER FUNCTION dbo.fnACL (@List nvarchar(4000), @New nvarchar(4000))
RETURNS nvarchar(4000)
AS  
BEGIN 
        --Treat ''s as NULLs
        SELECT @List = NULLIF(@List, ''), @New = NULLIF(@New, '')
        --first try returning the concatened string, if null then try just the list, if it too is null, just the new element
        RETURN COALESCE(@List + ',' + @New, @List, @New)
END

A query from ActiveRecord that isn't working

SELECT
t.*, j.* FROM terms_values j, values t WHERE t.id = j.value_id AND j.term_id = 310

? Earlier 8 items total Later ?