String to date during INSERT INTO - sql

I need to add a string ('taskdate') using the string to date conversion function, can you tell me how to do it?
Below is the standard add procedure, what do I need to change?
INSERT INTO SHIFTASKS ('taskcounter', 'vehid', 'taskdate', 'shift', 'tabelnum')
VALUES (100293896, 57, '13.01.14 00:00:00', 2, 600607);
P.S. I've read about the "cast" and "convert" functions, but I can't figure out how to use them during the insert into command.
DBMS - Oracle

You can use this:
INSERT INTO SHIFTASKS (taskcounter, vehid, taskdate, shift, tabelnum)
VALUES (100293896, 57, to_date('13.01.14 00:00:00','DD.MM.RR HH24:MI:DD'), 2, 600607);

Related

SQL wont Complete INSERT INTO (incorrect Date)

Im attempting to insert data into a table using the INSERT INTO shown below:
INSERT INTO `paitent`(`PaitentID`, `fName`, `sName`, `DOB`, `Sex`, `NextOfKinFName`, `NextOfKinSName`, `NextOfKinRelationship`, `NextOfKinTelNo`, `DateOfAdmission`, `AdmissionNotes`, `PrescribedMedication`, `Doseage`, `StartMedDate`, `EndMedDate`, `MedFrequency`) VALUES (0001, "Peter", "Gregory", 1997-01-01, "Male", "Mary", "Gregory", "Wife", 09875463762, 2020-01-02, "Broken Leg", "N/A", "N/A", 2019-02-02, 2019-03-03, "N/A")
but when I run the SQL i get this error:
#1292 - Incorrect date value: '1995' for column 'DOB' at row 1
I cant understand why as there isn't even a 1995 date in the data that's being inputted.
Does anyone understand why I'm getting this error?
This is a bit long for a comment.
Date constants need to be enclosed in single quotes, so '2020-01-02'. However, in Standard SQL (your question doesn't have a tag), it is even better to identify it as a date constant by using the prefix date: date '2020-01-02' is the standard syntax for a date literal.

Error at or near square brackets - Postgres

I'm trying to run a PostgreSQL query which is:
insert into client (email, name) values ('johndoe#email.com', 'johnDoe');
insert into client_settings (client_id, data) values (currval('client_id_seq'), 0);
insert into client_verify (client_id, dataFields) values (currval('client_id_seq'), json_build_object('data1', ['a1', 'a2'], 'data2', ['b1', 'b2']) );
But I'm getting an error stating SQL Error [42601]: syntax error at or near "[".
The last json object(i.e., the dataFields) when inserted into the DB it should look like:
{"data1": ["a1", "a2"], "data2": ["b1", "b2"]}
Not sure what I am doing wrong. Is there something that I'm missing or a different way to do that?
After good research I found documentation to put 'Array' in front of those like:
json_build_object('data1', Array['a1', 'a2'], 'data2', Array['b1', 'b2'])
Is this what you need :
insert into client_verify (client_id, dataFields)
values (currval('client_id_seq'), json_build_object('data1', 'a1, a2', 'data2', 'b1, b2') );
Please try this:
insert into client_verify (client_id, dataFields)
values (currval('client_id_seq')
, json_build_object('data1', '["a1", "a2"]', 'data2', '["b1", "b2"]'));
Here you can check how you need to add your string:
https://www.freeformatter.com/json-escape.html#ad-output
The UNESCAPE is the option you need
Here is a demo :
DEMO

Unable to convert data type

SELECT *
FROM Registration
WHERE UserPass = CONVERT(VARBINARY(50),'5avag3',1);
I'm trying to store that password into my database as hash value. Right now the attribute UserPass is in binary and I'm getting this error message:
Implicit conversion from data type varchar to binary is not allowed. Use the CONVERT function to run this query.
Code:
INSERT INTO [dbo].[Registration] ([LoginName], [UserPass], [FirstName],[LastName], [PIC], [DIC])
VALUES ('LogPaul', '', 'Logan', 'Paul', '', '690404-10-5827')
When I try to convert the data type it says I'm unable to convert.
Please help I'm new to SQL, thanks in advance
Just perform the conversion to binary inside the actual INSERT statement:
INSERT INTO [dbo].[Registration] ([LoginName], [UserPass], [FirstName], [LastName],
[PIC], [DIC])
VALUES
('LogPaul', CONVERT(VARBINARY(50),'5avag3',1), 'Logan', 'Paul', '',
'690404-10-5827')

Save datetime in sql table

How I can insert with a query a date like this? 2015-06-02T11:18:25.000
I have tried this:
INSERT INTO TABLE (FIELD) VALUES (convert(datetime,'2015-06-02T11:18:25.000'))
But I have returned:
Conversion failed when converting date and/or time from character string.
I tried also:
CONVERT(DATETIME, '2015-06-02T11:18:25.000', 126)
but it is not working:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
The entire query is:
INSERT INTO BOLLE_TEST_POPPER (QIDDIADE,QNUMBOLLA,QSELEZIONALE,QDATA,QORA,QPRIMAPESATA,QSECONDAPESATA,QIMP1,QIMP2,QIDCAUSALE,QIDCLIENTE,QIDDESTINAZIONE,QIDVETTORE,QIDSUBVETTORE,QIDCAMION,QORATRASITO,QNUMBOLLAINGRESSO,QDATABOLLAINGRESSO,QCOMMITTENTIDELTRASPORTO,QANNOTAZIONI,QANNOTAZIONIINBOLLA,QIDARTICOLO,QQANTITA,QIDAUTISTA,QNUMTESSERA,QNUMGETTONE,VALORETAB1,VALORETAB2,VALORETAB3,VALORETAB4,VALORETAB5,VALORETAB6,VALORETAB7,VALORETAB8,VALORETAB9,VALORETAB10,VALORETESTO1,VALORETESTO2,VALORETESTO3,VALORETESTO4,VALORETESTO5,VALORETESTO6,VALORETESTO7,VALORETESTO8,VALORETESTO9,VALORETESTO10) VALUES ('4','5234','-',
convert(datetime,'2015-06-02'),convert(datetime,'2015-06-02T11:18:25.000',126),'30020','20230','null','null','4','1','391','50','50','50','500',convert(datetime,'2015-06-02T11:14:06+02:00',126),'-','false','-','-','19','9790.00','1','BK994P','-','-','null','null','null','null','null','null','null','null','null','-','-','-','-','-','-','-','-','-','-');
What is wrong?
Try this:
INSERT INTO TABLE (FIELD) VALUES CONVERT(DATETIME, '2015-06-02T11:18:25.000', 126)
126 relates to ISO8601, which is the format yyyy-mm-ddThh:mi:ss.mmm.
This is the same format as the string '2015-06-02T11:18:25.000'.
For more information, see here.
For dates with a datetimeoffset (for example '2015-06-02T11:14:06+02:00' - note the +02:00 at the end), you will have to do this:
CONVERT(DATETIME, CONVERT(DATETIMEOFFSET,'2015-06-02T11:14:06+02:00'), 127)
The fully fixed query should be:
INSERT INTO BOLLE_TEST_POPPER (QIDDIADE,QNUMBOLLA,QSELEZIONALE,QDATA,QORA,QPRIMAPESATA,QSECONDAPESATA,QIMP1,QIMP2,QIDCAUSALE,QIDCLIENTE,QIDDESTINAZIONE,QIDVETTORE,QIDSUBVETTORE,QIDCAMION,QORATRASITO,QNUMBOLLAINGRESSO,QDATABOLLAINGRESSO,QCOMMITTENTIDELTRASPORTO,QANNOTAZIONI,QANNOTAZIONIINBOLLA,QIDARTICOLO,QQANTITA,QIDAUTISTA,QNUMTESSERA,QNUMGETTONE,VALORETAB1,VALORETAB2,VALORETAB3,VALORETAB4,VALORETAB5,VALORETAB6,VALORETAB7,VALORETAB8,VALORETAB9,VALORETAB10,VALORETESTO1,VALORETESTO2,VALORETESTO3,VALORETESTO4,VALORETESTO5,VALORETESTO6,VALORETESTO7,VALORETESTO8,VALORETESTO9,VALORETESTO10) VALUES ('4','5234','-',
convert(datetime,'2015-06-02'),convert(datetime,'2015-06-02T11:18:25.000',126),'30020','20230','null','null','4','1','391','50','50','50','500',CONVERT(DATETIME, CONVERT(DATETIMEOFFSET,'2015-06-02T11:14:06+02:00'), 127),'-','false','-','-','19','9790.00','1','BK994P','-','-','null','null','null','null','null','null','null','null','null','-','-','-','-','-','-','-','-','-','-');
You need a format. In this case, 126:
INSERT INTO TABLE (FIELD)
VALUES (convert(datetime,'2015-06-02T11:18:25.000', 126))
The list is here.
For time zones, you need 127, so you need to fix your values clause:
('4','5234','-',
convert(datetime,'2015-06-02'),convert(datetime,'2015-06-02T11:18:25.000',127),'30020','20230','null','null','4','1','391','50','50','50','500',convert(datetime,'2015-06-02T11:14:06+02:00',127),'-','false','-','-','19','9790.00','1','BK994P','-','-','null','null','null','null','null','null','null','null','null','-','-','-','-','-','-','-','-','-','-');
just try this. it worked for me.
if(isset($_POST['buttonsave']))
{
$vfidperiodo = preg_replace('#[^A-Za-z0-9]#i','',$_POST['idperiodo']);
$vfperiodo = ms_escape_string($_POST['periodo']);
$vffechainicio = $_POST['fecha_inicio'];
$query_in="INSERT INTO iperiodos (idperiodo, periodo, fecha_inicio)
VALUES ('".$vfidperiodo."','".$vfperiodo."','".$vffechainicio."')";
$sql_in = sqlsrv_query($conn,$query_in);
if ($sql_in) // Se eejectuto la sentencia SQL?
{
echo "SQLSuccess"; // Mensaje Afirmativo.
} else {
die( print_r( sqlsrv_errors(), true)); // Causa del error.
}
exit();
}
here's the DB and the Form images.

insert - not recorded field in the table

INSERT INTO `jos1_content`
(`title`, `introtext`, `fulltext`, `state`, `sectionid`, `catid`, `attribs`)
VALUES
($title, $introtext, $fulltext, 1, 1, $catid, 5)
Query is made up of php script. Derivation of variables shows that they are not empty.
At the request of phpMyAdmin shows the error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '
You need to use single quotes to indicate strings to SQL - use:
INSERT INTO `jos1_content`
(`title`, `introtext`, `fulltext`, `state`, `sectionid`, `catid`, `attribs`)
VALUES
('". $title ."', '". $introtext ."', '". $fulltext ."', 1, 1, $catid, 5)
A safer means would be to use:
$query = sprintf("INSERT INTO `jos1_content`
(`title`, `introtext`, `fulltext`, `state`, `sectionid`, `catid`, `attribs`)
VALUES
('%s', '%s', '%s', 1, 1, %d, 5)",
mysql_real_escape_string($title),
mysql_real_escape_string($introtext),
mysql_real_escape_string($fulltext),
$catid);
Reference:
SPRINTF
MYSQL_REAL_ESCAPE_STRING
You need to surround your PHP variables that are being inserted into string-type fields with quotes:
INSERT INTO `jos1_content`
(`title`, `introtext`, `fulltext`, `state`, `sectionid`, `catid`, `attribs`)
VALUES
('$title', '$introtext', '$fulltext', 1, 1, $catid, 5)
Read about prepared statements in the manual of your programming language or use mysql_escape_string.