title: Inserting empty string or null into postgres as null using jackc/pgx
body: <p>I'm using an external json API that's inconsistent in the way it handles missing values. Sometimes json values show up as empty strings and other times as null. For example...</p>
  <p>Case1: <code>datedec</code> and <code>curr</code> are both empty strings.</p>
  <pre class="lang-json prettyprint-override"><code>{
      &quot;symbol&quot;: &quot;XYZ&quot;,
      &quot;dateex&quot;: &quot;2020-09-01&quot;,
      &quot;datedec&quot;: &quot;&quot;,
      &quot;amount&quot;: &quot;1.25&quot;,
      &quot;curr&quot;: &quot;&quot;,
      &quot;freq&quot;: &quot;annual&quot;
  }
  </code></pre>
  <p>Case2: <code>datedec</code> is null. <code>curr</code> is populated.</p>
  <pre class="lang-json prettyprint-override"><code>{
      &quot;symbol&quot;: &quot;XYZ&quot;,
      &quot;dateex&quot;: &quot;2020-09-01&quot;,
      &quot;datedec&quot;: null,
      &quot;amount&quot;: &quot;1.25&quot;,
      &quot;curr&quot;: &quot;USD&quot;,
      &quot;freq&quot;: &quot;annual&quot;
  }
  </code></pre>
  <p>Here is the struct I'm using to represent a dividend:</p>
  <pre class="lang-golang prettyprint-override"><code>type Dividend struct {
      symbol   string `json:&quot;symbol&quot;`
      dateex   string `json:&quot;dateex&quot;`
      datedec  string `json:&quot;datedec&quot;`
      amount   string `json:&quot;amount&quot;`
      curr     string `json:&quot;curr&quot;`
      freq     string `json:&quot;freq&quot;`
  }
  </code></pre>
  <p>The problem I'm having is how to insert either an empty string or null, into the database as <code>NULL</code>. I know I could use an <code>omitempty</code> json tag, but then how would I write a function to handle values I don't know will be missing? For example, Here is my current function to insert a dividend into postgresql using the jackc/pgx package:</p>
  <pre class="lang-golang prettyprint-override"><code>func InsertDividend(d Dividend) error {
      sql := `INSERT INTO dividends 
      (symbol, dateex, datedec, amount, curr, freq)
      VALUES ($1, $2, $3, $4, $5, $6)`
      conn, err := pgx.Connect(ctx, &quot;DATABASE_URL&quot;)
      // handle error 
      defer conn.Close(ctx)
      tx, err := conn.Begin()
      // handle error
      defer tx.Rollback(ctx)
      _, err = tx.Exec(ctx, sql, d.symbol, d.dateex, d.datedec, d.amount, d.curr, d.freq)
      // handle error
      }
      err = tx.Commit(ctx)
      // handle error
      return nil
  }
  </code></pre>
  <p>If a value (e.g. datedec or curr) is missing, then this function will error. From this post <a href="https://stackoverflow.com/questions/40266633/golang-insert-null-into-sql-instead-of-empty-string">Golang Insert NULL into sql instead of empty string</a> I saw how to solve Case1. But is there a more general way to handle both cases (null or empty string)?</p>
  <p>I've been looking through the database/sql &amp; jackc/pgx documentation but I have yet to find anything. I think the sql.NullString has potential but I'm not sure how I should be doing it.</p>
  <p>Any suggestions will be appreciated. Thanks!</p>