SQL Spreadsheets: Cherry Picking Contacts

SQL Spreadsheets: Cherry Picking Contacts

The following SQL command will generate a spreadsheet that can be used to cherry-pick specific contacts.

  1. CREATE OR ALTER FUNCTION SafeSingleLine (
  2. @Input NVARCHAR(MAX)
  3. ) RETURNS NVARCHAR(MAX) AS BEGIN
  4. DECLARE @Ret NVARCHAR(MAX) = COALESCE(@Input, '')
  5. SET @RET = REPLACE(@RET, CHAR(13), ' ')
  6. SET @RET = REPLACE(@RET, CHAR(10), ' ')
  7. SET @RET = REPLACE(@RET, CHAR(9), ' ')
  8. SET @RET = REPLACE(@RET, '''', ' ')
  9. SET @RET = REPLACE(@RET, ',', ' ')

  10. WHILE(CHARINDEX('  ', @RET) != 0) BEGIN
  11. SET @RET = REPLACE(@RET, '  ', ' ')
  12. END

  13. SET @Ret = TRIM(@RET)

  14. RETURN @RET
  15. END
  16. GO

  17. SELECT
  18.     '' as 'Keep? (Y)',
  19.     V1.Id, 
  20.     dbo.SafeSingleLine(V1.Final_ReferenceCode) as 'Final_ReferenceCode', 
  21.     dbo.SafeSingleLine(V1.Final_FullName) as 'Final_FullName', 
  22.     V1.Final_Status,
  23.     Script = CONCAT(''
  24.       --IF cell A### = "Y"
  25.       , '=IF(INDIRECT("A"&ROW()) = "Y",'
  26.   , '"'
  27.   , ' UPDATE'
  28.   , '   __M_Contacts'
  29.   , ' SET'
  30.   , '   Final_Status = Original_Status '
  31.   , ' WHERE 1=1'
  32.   , '   AND Id = '
  33.   , '     ''" & '
  34.   , '       INDIRECT("B"&ROW()) '
  35.   , '     & "'''
  36.   , '"'
  37.   --/THEN
  38.       , ', '
  39.   --ELSE
  40.   , '"'
  41.   , ' '
  42.   , '"'
  43.   --/ELSE
  44.       , ')'
  45.   )
  46. FROM
  47.     __M_Contacts V1
  48. ORDER BY
  49.     V1.Final_ReferenceCode, V1.Final_FullName

    • Related Articles

    • Convert Users into Contacts

      --Create contacts out of our users INSERT INTO __M_Contacts (Id, Final_Kind, Final_FirstName, Final_MiddleName, Final_LastName) SELECT CONCAT('__M_Users --- ', Id), 'Person', Final_FirstName, Final_MiddleName, Final_LastName FROM __M_Users
    • Synthesize Missing Client Contacts for Matters

      This script below will create dummy contacts where the Matter is assigned to a contact that no longer exists. --Some of the Matters are linked to contacts that no longer exist. --This will create dummy contacts from the missing links. INSERT INTO ...
    • Other: Global SQL Search

      The following query will create a stored procedure named SearchAllTables which will let you search all tables in the database for a value. It can be used the following ways: --Search for a column that exactly contains 'Test': exec SearchAllTables ...
    • Create Contact Categories from Matter Participants

      --The following script will create Contact Categories from the roles --that Contacts are linked to on Matters and then assign the contacts to those categories. GO INSERT INTO __M_Contacts_Categories ( Id, Final_Subject ) SELECT DISTINCT ...
    • Create an "Employer" Contact from a Custom Field.

      In some systems, contacts do not have an Employer relation and only have a text field that represents the employer. This script will help when moving firms into platforms that allow an actual contact to be specified for the employer. --Create Company ...