douchunji1885 2015-04-12 13:40
浏览 161
已采纳

将MySQL日期转换为PostgreSQL日期

UPDATED (SOLVED): '2000-00-00' is not a valid date. You must specify a value for the month and day - i.e. '2000-01-01'

I'm newbie with PostgreSQL. I wonder how to convert a MySQL date (YYYY-MM-DD) to PostgreSQL. What type of value should i use in PostgreSQL to set date like YYYY-MM-DD?

If it is impossible then how can i convert YYYY-MM-DD to a proper value in PHP (what is the proper type\value for PostgreSQL)?

Tnx a lot.

Query example (issue in 2000-00-00)

INSERT INTO "user" ("user_uni", "user_id", "name", "city", "country", "sex", "birthday", "referal_id", "social_network", "offer_balance", "customer_balance", "registration_time", "last_visit_time", "last_ip", "banned", "fit_actions", "token", "mail", "neverban", "last_api_request", "notify", "vip", "filters_free", "friends", "rate", "discount", "invite_day", "have_invites", "like_public_day", "have_like_public", "last_login", "suspected", "group_counter", "password") VALUES ('1', '19922', 'Иван Иванов', '243', '1', '1', '1988-01-07', '0', '1', '201.770', '500.000', '1352880961', '13533290', '178.150.243.214', '0', '0', '', '', '0', '0', '0', '0', '0', '0', '100.000', '0', '2000-00-00', '0', '2000-00-00', '0', '13533290', '0', '0', '')
  • 写回答

2条回答 默认 最新

  • douwei1921 2015-04-13 02:28
    关注

    Your issue isn't converting MySQL date format to PostgreSQL date format. Both use the ISO date format (YYYY-MM-DD) by default.

    The issue is that older MySQL versions were lax about what is considers a valid date. Feb 31st? Sure, no problem. The zero'th date of the zero'th month? Whatever.

    Newer versions "fix" this by silently converting invalid dates to 0000-00-00 (which is its self also an invalid date, but MySQL accepts it). In strict mode, which should always be used with MySQL, it generates an error instead. See the manual.

    PostgreSQL enforces date validity, so it won't accept 2000-00-00. That isn't a date, it's nonsense. It also rejects 0000-00-00 which causes lots of problems for MySQL users, since MySQL uses 0000-00-00 as a sort of pseudo-null or NaN for dates, a "valid" date that is not actually valid.

    Your best option is to correct these dates before importing your data. If you're using them as a placeholder for unknown dates, use NULL. (You'll need to tweak queries that rely on <, >, =, etc, because they'll behave differently).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵