Skip to content

Commit

Permalink
builtins: implement to_char(date, string)
Browse files Browse the repository at this point in the history
Release note (sql change): Introduce the `to_char(date, format)` builtin
variant, which converts a given date to a string using the given format
string.
  • Loading branch information
otan committed May 9, 2023
1 parent 7a8be52 commit 7a4bbaa
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/generated/sql/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,8 @@ has no relationship with the commit order of concurrent transactions.</p>
</span></td><td>Stable</td></tr>
<tr><td><a name="to_char"></a><code>to_char(date: <a href="date.html">date</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Convert an date to a string assuming the ISO, MDY DateStyle.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="to_char"></a><code>to_char(date: <a href="date.html">date</a>, format: <a href="string.html">string</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Convert a timestamp with time zone to a string using the given format.</p>
</span></td><td>Stable</td></tr>
<tr><td><a name="to_char"></a><code>to_char(interval: <a href="interval.html">interval</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Convert an interval to a string assuming the Postgres IntervalStyle.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="to_char"></a><code>to_char(interval: <a href="interval.html">interval</a>, format: <a href="string.html">string</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Convert an interval to a string using the given format.</p>
Expand Down
5 changes: 3 additions & 2 deletions pkg/sql/logictest/testdata/logic_test/datetime
Original file line number Diff line number Diff line change
Expand Up @@ -1936,12 +1936,13 @@ ORDER BY pk
statement error only ISO style is supported
SELECT to_char_with_style(now()::date, 'postgres')

query TT
query TTT
SELECT
to_char('2020-01-02 01:02:03'::date),
to_char('2020-01-02'::date, 'YYYY-MM-DD HH24:MI:SS.FF6'),
to_char_with_style('2020-01-02 01:02:03'::date, 'DMY')
----
2020-01-02 2020-01-02
2020-01-02 2020-01-02 00:00:00.000000 2020-01-02

statement ok
CREATE TABLE time_datestyle_parse(pk SERIAL PRIMARY KEY, s string);
Expand Down
16 changes: 16 additions & 0 deletions pkg/sql/sem/builtins/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -2411,6 +2411,22 @@ var regularBuiltins = map[string]builtinDefinition{
Info: "Convert an date to a string assuming the ISO, MDY DateStyle.",
Volatility: volatility.Immutable,
},
tree.Overload{
Types: tree.ParamTypes{{Name: "date", Typ: types.Date}, {Name: "format", Typ: types.String}},
ReturnType: tree.FixedReturnType(types.String),
Fn: func(_ context.Context, ctx *eval.Context, args tree.Datums) (tree.Datum, error) {
d := tree.MustBeDDate(args[0])
f := tree.MustBeDString(args[1])
t, err := d.ToTime()
if err != nil {
return nil, err
}
s, err := tochar.TimeToChar(t, ctx.ToCharFormatCache, string(f))
return tree.NewDString(s), err
},
Info: "Convert a timestamp with time zone to a string using the given format.",
Volatility: volatility.Stable,
},
),

"to_char_with_style": makeBuiltin(
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/sem/builtins/fixed_oids.go
Original file line number Diff line number Diff line change
Expand Up @@ -2381,6 +2381,7 @@ var builtinOidsArray = []string{
2408: `crdb_internal.job_execution_details(job_id: int) -> jsonb`,
2409: `st_bdpolyfromtext(str: string, srid: int) -> geometry`,
2410: `crdb_internal.pretty_value(raw_value: bytes) -> string`,
2411: `to_char(date: date, format: string) -> string`,
}

var builtinOidsBySignature map[string]oid.Oid
Expand Down

0 comments on commit 7a4bbaa

Please sign in to comment.