{"id":9867,"date":"2017-03-30T05:36:56","date_gmt":"2017-03-30T03:36:56","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/can-i-do-it-with-postgresql-11-nls_date_format\/"},"modified":"2017-03-30T05:36:56","modified_gmt":"2017-03-30T03:36:56","slug":"can-i-do-it-with-postgresql-11-nls_date_format","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/can-i-do-it-with-postgresql-11-nls_date_format\/","title":{"rendered":"Can I do it with PostgreSQL? \u2013 11 \u2013 NLS_DATE_FORMAT"},"content":{"rendered":"<p>Following the <a href=\"https:\/\/www.dbi-services.com\/blog\/can-i-do-it-with-postgresql-10-timezones\/\" target=\"_blank\" rel=\"noopener\">last post about time zones<\/a> this post is about the display and default format of date and time values. What people usually do to get a consistent display of date and time values in Oracle is to set <a href=\"http:\/\/docs.oracle.com\/database\/122\/REFRN\/NLS_DATE_FORMAT.htm#REFRN10119\" target=\"_blank\" rel=\"noopener\">NLS_DATE_FORMAT<\/a>:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\nSQL&gt; show parameter nls_date_format;\n\nNAME\t\t\t\t     TYPE\t VALUE\n------------------------------------ ----------- ------------------------------\nnls_date_format \t\t     string\t YYYY-MON-DD HH24:MI:SS\n\nSQL&gt; alter session set nls_date_format='DD.MM.YYYY HH24:MI:SS';\n\nSession altered.\n\nSQL&gt; select sysdate from dual;\n\nSYSDATE\n-------------------\n29.03.2017 16:26:11\n\nSQL&gt; alter session set nls_date_format='YYYY-MON-DD HH24:MI:SS';\n\nSession altered.\n\nSQL&gt; select sysdate from dual;\n\nSYSDATE\n--------------------\n2017-MAR-29 16:31:01\n<\/pre>\n<p>Obviously PostgreSQL does not know anything about NLS_DATE_FORMAT, so what does it provide for doing the same? <\/p>\n<p><!--more--><\/p>\n<p>First of all (and this is true for Oracle as well as for PostgreSQL) this is all about displaying date and time values and interpreting strings that shall be converted to date and time values. This has nothing to do with the internal storage.<\/p>\n<p>What controls the display of date and time specific values in PostgreSQL is the parameter <a href=\"https:\/\/www.postgresql.org\/docs\/current\/static\/runtime-config-client.html#GUC-DATESTYLE\" target=\"_blank\" rel=\"noopener\">datestyle<\/a>, the default is <a href=\"https:\/\/en.wikipedia.org\/wiki\/ISO_8601\" target=\"_blank\" rel=\"noopener\">ISO<\/a>:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# show datestyle;\n DateStyle \n-----------\n ISO, MDY\n(1 row)\n<\/pre>\n<p>MDY here represents: MONTH-DAY-YEAR. If you want to change this on the session level you can:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# set datestyle='SQL, MDY';\nSET\npostgres=# select now();\n              now               \n--------------------------------\n 03\/29\/2017 18:13:15.475693 GMT\n(1 row)\n<\/pre>\n<p>You can do the same by setting the <a href=\"https:\/\/www.postgresql.org\/docs\/9.6\/static\/libpq-envars.html\" target=\"_blank\" rel=\"noopener\">PGDATESTYLE<\/a> environment variable before starting a new connection:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres@pgbox:\/home\/postgres\/ [PG962] export PGDATESTYLE='SQL, MDY'\npostgres@pgbox:\/home\/postgres\/ [PG962] psql postgres\npsql (9.6.2 dbi services build)\nType \"help\" for help.\n\npostgres=# select now();\n              now               \n--------------------------------\n 03\/29\/2017 18:15:29.765396 GMT\n(1 row)\n<\/pre>\n<p>When you have a string and you want to insert that string into a column which is defined as date, how can you do this? PostgreSQL is very flexible here:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# create table t1 ( a date );\nCREATE TABLE\npostgres=# insert into t1 (a) values ('2017.01.01');\nINSERT 0 1\npostgres=# insert into t1 (a) values ('2017-01-01');\nINSERT 0 1\npostgres=# insert into t1 (a) values ('2017-JAN-01');\nINSERT 0 1\npostgres=# insert into t1 (a) values ('2017-JAN-01 11:11:11');\nINSERT 0 1\npostgres=# insert into t1 (a) values ('2017\/01\/01 11:11:11');\nINSERT 0 1\npostgres=# insert into t1 (a) values ('20170101 111111');\nINSERT 0 1\n<\/pre>\n<p>All these work without specifying a format mask as you would need to do it in Oracle. The rules how a string is interpreted are documented <a href=\"https:\/\/www.postgresql.org\/docs\/current\/static\/datetime-input-rules.html\" target=\"_blank\" rel=\"noopener\">here<\/a>. <\/p>\n<p>When you want to convert a string into a date or timestamp you can use one of the <a href=\"https:\/\/www.postgresql.org\/docs\/current\/static\/functions-datetime.html\" target=\"_blank\" rel=\"noopener\">make_* functions<\/a>:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# select make_date(2017,01,01);\n make_date  \n------------\n 2017-01-01\n(1 row)\npostgres=# select make_timestamp (2017,01,01,00,00,00);\n   make_timestamp    \n---------------------\n 2017-01-01 00:00:00\n(1 row)\n<\/pre>\n<p>&#8230; or use the <a href=\"https:\/\/www.postgresql.org\/docs\/current\/static\/functions-formatting.html\" target=\"_blank\" rel=\"noopener\">data type formatting functions<\/a>:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# select to_date('2017.01.01','YYYY.MM.DD');\n  to_date   \n------------\n 2017-01-01\n(1 row)\n\npostgres=# select to_date('2017-JAN-01','YYYY.Mon.DD');\n  to_date   \n------------\n 2017-01-01\n(1 row)\n<\/pre>\n<p>Hope this helps &#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Following the last post about time zones this post is about the display and default format of date and time values. What people usually do to get a consistent display of date and time values in Oracle is to set NLS_DATE_FORMAT: SQL&gt; show parameter nls_date_format; NAME TYPE VALUE &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; &#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; nls_date_format string YYYY-MON-DD HH24:MI:SS [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[229],"tags":[77],"type_dbi":[],"class_list":["post-9867","post","type-post","status-publish","format-standard","hentry","category-database-administration-monitoring","tag-postgresql"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v28.8 (Yoast SEO v28.8) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Can I do it with PostgreSQL? \u2013 11 \u2013 NLS_DATE_FORMAT - dbi Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dbi-services.com\/blog\/can-i-do-it-with-postgresql-11-nls_date_format\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Can I do it with PostgreSQL? \u2013 11 \u2013 NLS_DATE_FORMAT\" \/>\n<meta property=\"og:description\" content=\"Following the last post about time zones this post is about the display and default format of date and time values. What people usually do to get a consistent display of date and time values in Oracle is to set NLS_DATE_FORMAT: SQL&gt; show parameter nls_date_format; NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ nls_date_format string YYYY-MON-DD HH24:MI:SS [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/can-i-do-it-with-postgresql-11-nls_date_format\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2017-03-30T03:36:56+00:00\" \/>\n<meta name=\"author\" content=\"Open source Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Open source Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/can-i-do-it-with-postgresql-11-nls_date_format\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/can-i-do-it-with-postgresql-11-nls_date_format\\\/\"},\"author\":{\"name\":\"Open source Team\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\"},\"headline\":\"Can I do it with PostgreSQL? \u2013 11 \u2013 NLS_DATE_FORMAT\",\"datePublished\":\"2017-03-30T03:36:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/can-i-do-it-with-postgresql-11-nls_date_format\\\/\"},\"wordCount\":254,\"commentCount\":0,\"keywords\":[\"PostgreSQL\"],\"articleSection\":[\"Database Administration &amp; Monitoring\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/can-i-do-it-with-postgresql-11-nls_date_format\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/can-i-do-it-with-postgresql-11-nls_date_format\\\/\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/can-i-do-it-with-postgresql-11-nls_date_format\\\/\",\"name\":\"Can I do it with PostgreSQL? \u2013 11 \u2013 NLS_DATE_FORMAT - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\"},\"datePublished\":\"2017-03-30T03:36:56+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/can-i-do-it-with-postgresql-11-nls_date_format\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/can-i-do-it-with-postgresql-11-nls_date_format\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/can-i-do-it-with-postgresql-11-nls_date_format\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Can I do it with PostgreSQL? \u2013 11 \u2013 NLS_DATE_FORMAT\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\",\"name\":\"dbi Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\",\"name\":\"Open source Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g\",\"caption\":\"Open source Team\"},\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/author\\\/open-source-team\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Can I do it with PostgreSQL? \u2013 11 \u2013 NLS_DATE_FORMAT - dbi Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.dbi-services.com\/blog\/can-i-do-it-with-postgresql-11-nls_date_format\/","og_locale":"en_US","og_type":"article","og_title":"Can I do it with PostgreSQL? \u2013 11 \u2013 NLS_DATE_FORMAT","og_description":"Following the last post about time zones this post is about the display and default format of date and time values. What people usually do to get a consistent display of date and time values in Oracle is to set NLS_DATE_FORMAT: SQL&gt; show parameter nls_date_format; NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ nls_date_format string YYYY-MON-DD HH24:MI:SS [&hellip;]","og_url":"https:\/\/www.dbi-services.com\/blog\/can-i-do-it-with-postgresql-11-nls_date_format\/","og_site_name":"dbi Blog","article_published_time":"2017-03-30T03:36:56+00:00","author":"Open source Team","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Open source Team","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/can-i-do-it-with-postgresql-11-nls_date_format\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/can-i-do-it-with-postgresql-11-nls_date_format\/"},"author":{"name":"Open source Team","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202"},"headline":"Can I do it with PostgreSQL? \u2013 11 \u2013 NLS_DATE_FORMAT","datePublished":"2017-03-30T03:36:56+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/can-i-do-it-with-postgresql-11-nls_date_format\/"},"wordCount":254,"commentCount":0,"keywords":["PostgreSQL"],"articleSection":["Database Administration &amp; Monitoring"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/can-i-do-it-with-postgresql-11-nls_date_format\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/can-i-do-it-with-postgresql-11-nls_date_format\/","url":"https:\/\/www.dbi-services.com\/blog\/can-i-do-it-with-postgresql-11-nls_date_format\/","name":"Can I do it with PostgreSQL? \u2013 11 \u2013 NLS_DATE_FORMAT - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2017-03-30T03:36:56+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202"},"breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/can-i-do-it-with-postgresql-11-nls_date_format\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/can-i-do-it-with-postgresql-11-nls_date_format\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/can-i-do-it-with-postgresql-11-nls_date_format\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Can I do it with PostgreSQL? \u2013 11 \u2013 NLS_DATE_FORMAT"}]},{"@type":"WebSite","@id":"https:\/\/www.dbi-services.com\/blog\/#website","url":"https:\/\/www.dbi-services.com\/blog\/","name":"dbi Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202","name":"Open source Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g","caption":"Open source Team"},"url":"https:\/\/www.dbi-services.com\/blog\/author\/open-source-team\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/9867","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=9867"}],"version-history":[{"count":0,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/9867\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=9867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=9867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=9867"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=9867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}