0

I have problem with converting a datetimeoffset to datetime. I have tried to insert into my database using postman with method post in body. But the result is EXEC SMSBlast2Procedure '9', '087871723282', 'adsbjkadhdhad', '0001-01-01 00:00:00', '0001-01-01 00:00:00' , 'asjdasjgdjasgdjsad', '2019-02-24 08:17:54' , where in postman I am inputting like picture below :

enter image description here

respon json like picture below :

enter image description here

but when I check in console result WillBeSentDate is 0001-01-01 00:00:00 and 0001-01-01 00:00:00, why ?

Here my code to datetimeoffset to datetime.

    json.NewDecoder(r.Body).Decode(&smsblats)

    var count int64 = 0
    db.Debug().Table("SMSBlast2").Count(&count)
    count++
    fmt.Println(count)

   formatWill, _ := time.Parse(time.RFC3339, smsblats.WillBeSentDate.String())
   formatSent, _ := time.Parse(time.RFC3339, smsblats.SentDate.String())


    db.Debug().Raw("EXEC SMSBlast2Procedure ?, ?, ?, ?, ? , ?, ?",
        count,
        smsblats.MobilePhone,
        smsblats.Output,
        formatWill,
        formatSent,
        smsblats.Status,
        time.Now()).Scan(smsblats)
    w.Header().Set("Content-Type", "application/json")
    w.Header().Set("Response-Code", "00")
    w.Header().Set("Response-Desc", "Success")
    json.NewEncoder(w).Encode(smsblats)

What wrong with my code? The error says

Error converting data type datetimeoffset to datetime

I'm using SQL Server for database, a stored procedure like this:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[SMSBlast2Procedure]
    (@sequenceid INT,
     @mobilephone VARCHAR(20),
     @output VARCHAR(20),
     @willbesentdate DATETIME,
     @sentdate DATETIME, 
     @status VARCHAR(60),
     @dtmupd DATETIME)
AS
    INSERT INTO SMSBlast2 (SequenceID, MobilePhone, Output, WillBeSentDate, SentDate, Status, DtmUpd) 
    VALUES (@sequenceid, @mobilephone, @output, @willbesentdate, @sentdate, @status, @dtmupd)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
dera ta
  • 63
  • 1
  • 2
  • 10
  • Hi @derata - the reason you're not receiving an answer to this question, despite having asked it a number of times, is that its not clear from your question exactly what the issue is. If you are getting an SQL error then we need to see both the definition of the SQL SP *AND* the exact data you are passing to it. As best I can tell from the above you're receiving the correct data in your post, but then breaking it before sending it to SQL. Please clarify. – Dale K Feb 24 '19 at 01:44
  • And as per your last identical question I don't think its an SQL issue, I think its the way you are building the inputs to the SQL SP. Please correct me if I am wrong. There is no point tagging technologies if they are clearly not part of the issue. – Dale K Feb 24 '19 at 01:47
  • @DaleBurrell converting data type datetimeoffset to datetime, I have try many way using time.format and time.parse , but result still same , result is '0001-01-01 00:00:00', '0001-01-01 00:00:00' , thank you for your advice – dera ta Feb 24 '19 at 02:37
  • In that case, please create the *smallest* code sample you can which reproduces the error - rather than having a large code block where most of it isn't necessary https://stackoverflow.com/help/mcve – Dale K Feb 24 '19 at 02:40
  • Are you totally sure you even have a datetimeoffset? The data you have shown as your input appears to be a regular datetime already? Do you actually need to convert it? – Dale K Feb 24 '19 at 02:42
  • @DaleBurrell when I'm not using convert still get same result , I comment variabel t , b , and I change t,b in db.raw with smsblats.WillBeSentDate, smsblats.SentDate , still get same result and error say mssql: Error converting data type datetimeoffset to datetime. you can check my wrong input in postman body for WillBeSentDate and SentDate in here : https://imgur.com/a/kjRSDPB – dera ta Feb 24 '19 at 02:49
  • Sorry I don't know anything about the technologies you are using. But try this https://stackoverflow.com/questions/25845172/parsing-date-string-in-golang – Dale K Feb 24 '19 at 03:13
  • @DaleBurrell hey check my store procedure, maybe I wrong create store procedure – dera ta Feb 24 '19 at 10:46
  • Parameters say `datetime` as your error said. You might need to contact `go-gorm` directly for assistance. – Dale K Feb 24 '19 at 18:59

0 Answers0