Skip to content

parseSql

parseSql(value: string): string
import { parseSql } from "@northguild/gmt/plain/parse";

Parse an ANSI SQL / ODBC datetime literal — the YYYY-MM-DD HH:MM:SS form

  • MySQL, SQLite, and standard SQL DATETIME/TIMESTAMP columns (without a time zone) use — into a plain ISO 8601 datetime string.
  • Plain only. SQL’s offset-carrying TIMESTAMPTZ literal is out of scope; see formatSql’s JSDoc.
  • Requires the space separator and 2-digit zero-padded month/day/hour; unlike parseRfc2822’s lenient 1-2 digit day, a single-digit field is rejected — SQL’s literal grammar has no variable-width form to accept.
  • Fractional seconds, when present, are preserved as-is; the regex only proves shape — Temporal.PlainDateTime.from proves the value is a real calendar date/time.
Parameter Type Description
value string SQL datetime literal (e.g. “2024-03-15 14:30:00”)

plain ISO 8601 datetime string, or “” on invalid input

parseSql("2024-03-15 14:30:00") // "2024-03-15T14:30:00"
parseSql("2024-03-15 14:30:00.5") // "2024-03-15T14:30:00.5"
parseSql("2024-03-15T14:30:00") // "" (wrong separator)
parseSql("not a date") // ""

plain/parse/parseSql.ts