我正在尝试从一个DB(db1)中获取日期格式的date(“ jobDate”),然后将其插入到Date(“ resolvedDate”)为纪元格式的另一个DB(db2)中。现在,我将日期从db1传递给db2的select查询。为了匹配它们的数据类型,我将time.Time转换为纪元格式并传递给db2。我不需要时间戳,只是YYYY-MM-DD格式。但是这里的问题是,db2中的纪元日期包含日期和时间。结果,当我通过jobDate选择查询时,它不能与resolveDate中的任何条目匹配。
我的代码如下:
dsn := "server=********.md3q.***.com;user id=*******;password=******"
db, err := sql.Open("mssql", dsn)
if err != nil {
log.Fatal(err)
}
err = db.Ping()
if err != nil {
log.Fatal(err)
}
rows,err := db2.Query("select top 1 jobDate from mdesk.dbo.tbl_jobstatus where LastStatus = 'Success' ORDER BY jobDate DESC")
if err != nil {
log.Println(err)
}
defer db.Close()
for rows.Next() {
err := rows.Scan(&jobDate)
if err != nil {
log.Fatal(err)
}
log.Println("jobDate",jobDate) //2013-01-11 00:00:00 +0000 UTC (I'm getting this)
}
input:= jobDate.Unix()
jobDate:= input3
log.Println("EpochDate",jobDate) // 1357948800 this is the epoch format of the above date
dsn1 := "server=*******.ecc4q.******.com; user id=*******; password=*********"
db1, err := sql.Open("mssql", dsn1)
if err != nil {
log.Fatal(err)
}
err = db1.Ping()
if err != nil {
log.Fatal(err)
}
rows1, err := db1.Query("select Incident_Number, resolvedDate, Corporate_ID from ITSM.dbo.HPD_Help_Desk_Classic where resolvedDate in (?)",jobDate) //main problem is here. 1357948800 is not matching with any entry in this query because all of the entries in resolvedDate are in datetime epoch format whereas I am trying to search in date epoch format.
if err != nil {
log.Println(err)
}
defer db.Close()
有人可以暗示我在做什么吗?
答案 0 :(得分:0)
如果“ resolvedDate”列的日期类型为DATE,则尝试将time.Time转换为SQL中的DATE(请参见下文)。 如果“ resolvedDate”是一个整数或字符串,请使用time.Format()方法对其进行预处理。
并始终担心时区。如果您的数据库不在UTC中,则可能会遇到意外行为。
rows1, err := db1.Query(
"select Incident_Number, resolvedDate, Corporate_ID from ITSM.dbo.HPD_Help_Desk_Classic " +
"where resolvedDate in DATE(?)", // <--- See cast in SQL
jobDate)