How to use Regex to replace square brackets from date field in Google Data Studio?

北城余情 提交于 2020-07-09 06:54:45

问题


I am trying to remove square brackets around a date field in Google Data Studio so I can properly treat it as a proper date dimension.

It looks like this:

[2020-05-20 00:00:23]

and I am using the RegEx of REGEXP_REPLACE(Date, "/[\[\]']+/g", "") and I want it to look like this for the output:

2020-05-20 00:00:23

It keeps giving me error results and will not work. I can not figure out what I am doing wrong here, I've used https://www.regextester.com/ to verify that it should work


回答1:


Regarding Dates, it can be achieved with a single TODATE Calculated Field:

TODATE(Date, "[%Y-%m-%d %H:%M:%S]", "%Y%m%d%H%M%S")

The Date Type can then be set as required:

  • YYYYMMDD: Date
  • YYYYMMDDhh: Date Hour
  • YYYYMMDDhhmm: Date Hour Minute

Google Data Studio Report and GIF to elaborate:




回答2:


You need to use a plain regex pattern, not a regex literal notation (/.../g).

Note that REGEXP_REPLACE removes all occurrences found, thus, there is no need for a g flag.

Use

REGEXP_REPLACE(Date, "[][]+", "")

to remove all square brackets in Date.



来源:https://stackoverflow.com/questions/61973919/how-to-use-regex-to-replace-square-brackets-from-date-field-in-google-data-studi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!