in Education by
I am struggling trying to update a row in an Azure SQL database. What I am trying to do is to update a row with some input variables along with a fresh datestamp. If I input the following (for test purposes), my database is updated, but the date is way off: "UPDATE TABLENAME set COL1 = ".$_POST[VAL1].", COL2 = ".$_POST[VAL2].", COL3 = 2020-03-20 WHERE COL0 = 'VAL0'" giving me a datestamp looking like this: 1905-06-21T00:00:00.0000000 I have been trying just around a hundred ways of formatting the date() variable, putting it in my SQL statement like this: "UPDATE TABLENAME set COL1 = ".$_POST[VAL1].", COL2 = ".$_POST[VAL2].", COL3 = ".date()." WHERE COL0 = 'VAL0'" Needless to say, COL3 is my datestamp column. But I cannot get the database to accept my datestamp formatting. I have tried YYYY-mm-dd xyz1234 in countless variants inside date(), but to no avail. The database has the following collation set: SQL_Latin1_General_CP1_CI_AS. Any pointers? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
First, about your incorrect date value. It is correct, because when you miss '' around 2020-03-20, SQL Server makes implicit data type conversion. Next example demonstrates this: Implicit conversion: DECLARE @date datetime SELECT @date = 2020-03-20 SELECT @date SELECT DATEADD(day, 2020-03-20, 0) Output: 1905-06-21 00:00:00.000 Second, if you want to pass date and time values, just use appropriate format - 'yyyy-MM-dd', 'yyyyMMdd hh:nn:ss' or 'yyyy-MM-ddThh:nn:ss': "UPDATE TABLENAME set COL1 = ".$_POST[VAL1].", COL2 = ".$_POST[VAL2].", COL3 = '2020-03-20' WHERE COL0 = 'VAL0'" I don't know how you make your connection to SQL Server, but try to use prepared statements. Update (Retrieve and send date and time values from and to SQL Server): Based on driver that you use to connect to SQL Server, you may retrieve date and time values as text or as PHP datetime object (if you use PHP Driver for SQL Server), but you need to send these values as text. In your case values are returned as text. So you need to generate universal datetime value (in 'yyyy-MM-ddThh:nn:ss' for example) as text. Next example shows some valid and invalid combinations for UPDATE T-SQL statement for your datetime column. It is tested with PHP Driver for SQL Server 4.0.3. <?php # Value from database as text $row['COL3'] = '2019-03-29T11:35:30.0000000'; # Valid statement $tsql = "UPDATE TABLENAME SET COL3 = '".substr($row['COL3'], 0, 19)."' "; # Valid statement - date("Y-m-d\Th:i:s") will return current datetime $tsql = "UPDATE TABLENAME SET COL3 = '".date("Y-m-d\Th:i:s")."' "; # Invalid statement - date("d-m-Y h:i:s", $row['COL3']) expects int as second parameter, # generates warning and returns '01-01-1970 12:33:39' as result $tsql = "UPDATE TABLENAME SET COL3 = '".date("d-m-Y h:i:s", $row['COL3'])."' "; ?>

Related questions

0 votes
    This feels like a noobish question to which I should be able to find the answer on the web, but ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    answer in one word by using which option we can make our table more attractive and presentable in Ms word Select the correct answer from above options...
asked Dec 21, 2021 in Education by JackTerrance
0 votes
    write the steps to create a table in MS Acess using blank table (create table) option Select the correct answer from above options...
asked Dec 17, 2021 in Education by JackTerrance
0 votes
    Data can be inserted, updated and deleted from a table by using a __________ object in ms access Select the correct answer from above options...
asked Dec 14, 2021 in Education by JackTerrance
0 votes
    data can be inserted, updated and deleted from a table by using _______ object on MS access Select the correct answer from above options...
asked Dec 14, 2021 in Education by JackTerrance
0 votes
    I am retrieving messages from an Outlook account. I am trying to get inline files and attachments from ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I am running following PHP code to interact with a MS Access database. $odbc_con = new COM("ADODB. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I am running following PHP code to interact with a MS Access database. $odbc_con = new COM("ADODB. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I am running following PHP code to interact with a MS Access database. $odbc_con = new COM("ADODB. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I already can update my data from database but the problem is i just want to update one data, but when I enter the ... all of data in the database table like this this is View:...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    looking for a bit of advice on SQL indexing. Whats the best type of index to create on a non- ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 20, 2022 in Education by JackTerrance
0 votes
    I have an Entity (QuoteSheet) that contains a child entity (QuoteTask), which is loaded using the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    DROP TYPE Position; CREATE OR REPLACE TYPE Position AS OBJECT (longitude NUMBER(11,7), lattitude NUMBER(11, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    In Ms Access Which query allow to create a table or alter data or database? Select the correct answer from above options...
asked Dec 17, 2021 in Education by JackTerrance
0 votes
    I am working on a project with spark and scala and I am new to both but with lot of help from ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
...