Python中SQL Server的阅读日期列中使用Pymssql软件包的问题

发布于 2025-02-09 18:19:44 字数 995 浏览 1 评论 0原文

我在SQL Server表中有一个日期列(TRNDATE),该表像DB


2021-11-29 09:48:05.0000000 +04:00


在Jupyter Notebook中读取此内容,我正在使用Pymsssql package。我的代码在下面的结果集下方看起来像是这样,

import pymssql
import pandas as pd
import datetime as datetime
conn = pymssql.connect(server, user, password, "HecPoll5")
df = pd.read_sql_query('select * from dbo.v_beeah_data where customerid=379691', conn )
print(df)

但是我在日期中获得十六进制字符的输出(以粗体标记)。

tid ----数字----- trndate ----- livemandatorId ----数量---- customerId
63221 ---- 731 ---- b'\ x00 $ \ xd5 \ xffb \ XFFB \ x00 \ x00 \ x00 \ x00 \ xbc \ xae \ xae \ x00 \ x00 \ x00 \ x00 \ x ... ---- 1 ---- 80.0 ---- 379691

我尝试添加

parse_dates = {“ trndate”:“%y%m%d%h:%m:%m:%s”}& parse_dates = true read_sql_query &在 Connect 中,添加charset =“ UTF-8”参数,到目前为止还没有任何效果。

这是我第一次使用此库,除了日期列外,结果集中的所有其他列都很好。如果有人以前遇到过这样的问题,请帮助我在这里缺少的东西。

I have a date column (trnDate) in an SQL Server Table, which is stored like this in DB


2021-11-29 09:48:05.0000000 +04:00


To read this in Jupyter Notebook, I am using the pymssql package. My code looks like this below

import pymssql
import pandas as pd
import datetime as datetime
conn = pymssql.connect(server, user, password, "HecPoll5")
df = pd.read_sql_query('select * from dbo.v_beeah_data where customerid=379691', conn )
print(df)

The result set however gives me the output where i get hex characters in my dates (marked in bold).

TID----Number----trnDate----LiveMandatorID----Quantity----CustomerID
63221----731----b'\x00$\xd5\xffB\x00\x00\x00\xbc\xae\x00\x00\x...----1----80.0----379691

I have tried adding

parse_dates={"trnDate":"%Y%m%d %H:%M:%S"} &
parse_dates=True in read_sql_query & adding charset="UTF-8" parameters in connect as well, nothing have so far worked well.

This is the first time I am using this library, all other columns in the result set works well except the date column. If anyone has previously encountered such issue, kindly help with what I am missing here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文