postgresql 日期显示为数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14496985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:45:11  来源:igfitidea点击:

Date shows up as number

rpostgresqldate

提问by MortenSickel

I have fetched a set of dates from postgresql, they look correct:

我从 postgresql 中获取了一组日期,它们看起来是正确的:

[1] "2007-07-13" "2007-07-14" "2007-07-22" "2007-07-23" "2007-07-24"
[6] "2007-07-25" "2007-08-13" "2007-08-14" "2007-08-15" "2007-08-16"
etc.

Then I want to run a loop on them to make new sql sentences to fetch some other data sets (yes, I know what I am doing, it would not have been possible to do all the processing in the database server)

然后我想对它们运行一个循环来创建新的 sql 语句来获取其他一些数据集(是的,我知道我在做什么,不可能在数据库服务器中进行所有处理)

So I tried

所以我试过了

for(date in geilodates)
 mapdate(date,geilo)
Error in postgresqlExecStatement(conn, statement, ...) : 
  RS-DBI driver: (could not Retrieve the result : ERROR:  invalid input syntax for type date: "13707"
LINE 1: ...id_date_location where not cowid is null and date='13707' or...

mapdate is a function I have written, the use of date within that is

mapdate 是我写的一个函数,里面日期的使用是

sql=paste('select * from gps_coord where cowid=',cowid," and date='",date,"'",sep='')

So, what has happened is that R silently converted my formatted dates to their integer representations before i tried to paste the sql together.

所以,发生的事情是在我尝试将 sql 粘贴在一起之前,R 默默地将我的格式化日期转换为它们的整数表示。

How do I get the original textual representation of the date? I tried

如何获得日期的原始文本表示?我试过

for(date in geilodates){
  d=as.Date(date,origin="1970-01-01")
  mapdate(d,geilo)
}
Error in charToDate(x) : 
character string is not in a standard unambiguous format

And I have not managed to find any other functions to create a datestring (or to "serve" the date as the string I get when listing the variable

而且我还没有找到任何其他函数来创建日期字符串(或将日期“提供”为我在列出变量时得到的字符串

回答by wush978

try ?format.Date

尝试 ?format.Date

x <- Sys.Date()
class(x)
class(format(x))

In R, the data of class Dateis a numeric type. An official way to represent Dateas string is to call format.

在 R 中,类的数据Date是数字类型。表示Date为字符串的官方方法是调用format.

I doubt that the format of dateis defined in your case, so pastedoes something unexpected.

我怀疑date在你的情况下定义了格式,所以paste有一些意想不到的事情。

Maybe you need to put format(x, "%Y-%m-%d")in your pastefunction instead of dateto tell R how which format you want for Date.

也许你需要format(x, "%Y-%m-%d")输入你的paste函数而不是date告诉 R 你想要日期的格式。

回答by MortenSickel

Thanks to wush978 for pointing me in the right direction, In the end I had to do:

感谢 wush978 为我指明了正确的方向,最后我不得不这样做:

for(d in geilodates){
 date=format(as.Date(d,origin="1970-01-01"))
 mapdate(date,geilo)
}

For some reason, inside the loop the "date" variable was seen as an integer, so I explicitely had to convert it to a date and then format it...

出于某种原因,在循环内部,“日期”变量被视为一个整数,因此我必须明确地将其转换为日期,然后对其进行格式化...