MSSQL Datetime issues

I'm parsing a CSV file and sending values into a table on a MSSQL Server (2008).
Everithings goes well but date and time, after the sql string are not in the italian format and MSSQL server refuses the insert.
This is my flow:


These are the debug messages:

As seen in the debug messages, the payload.giorno format is dd:mm:yyyy but after the SQL Insert, becomes mm:dd:yyyy.
This is the SQL String:
insert into Output_Produzione (Centro_di_Lavoro, data, n_ord_prod, n_operazione, articolo, VarINT1)
values (9999,'{{{payload.Giorno}}}' + ' ' + '{{{payload.Ora}}}', right('{{{payload.Lotto}}}',5), left('{{{payload.Lotto}}}',4),'AP11'+'{{{payload.Programma}}}', '{{{payload.Scheda}}}')

I can't change the source format of the CSV File.
The NODERED Server and SQL Server are on different HW but both in Italian and with the same regional settings (Italy).
Does it depend from Nodered settings?
Please, I'm a beginner, this is my first nodered flow, consider my background less than nothing :slight_smile:

As the CSV data is read, but before your template, add a function node to intercept and convert dates to a format suitable for SQL server.

Other notes....

  • Add debug (with "complete msg") set after each node (so you can see format of data before and after each node.
  • read this Working with messages it will be vital in understanding this issue
  • add a catch node and connect that to a debug node (again, complete msg)
  • if you're using node-red-contrib-mssql-plus attach a debug to the output (complete msg) and there is a property in msg that shows you the final SQL it tried to execute (helps you understand what's going on)

That should get you moving :slight_smile:

Additional notes:

Where you have written values (9999,'{{{payload.Giorno}}}' + ' ' + '{{{payload.Ora}}}' etc, if that is in a template node or in the MSSQL node, then you are not writing correct mustache syntax (i.e. its not javascript so you dont need the + string concatenation

For mustache, you'd need something like...

insert into Output_Produzione (Centro_di_Lavoro, data, n_ord_prod, n_operazione, articolo, VarINT1)
values (9999,'{{{payload.Giorno}}} {{{payload.Ora}}}', right('{{{payload.Lotto}}}',5), left('{{{payload.Lotto}}}',4),'AP11{{{payload.Programma}}}', '{{{payload.Scheda}}}')

Again, if you are using node-red-contrib-mssql-plusattach a debug node (set "complete msg") to the output of the MSSQL node and there will be a property inmsg.query` that shows you the final SQL it compiled and attempted to send to the DB

1 Like

Many thanks.
Up to the MSG block output (The CSV parser), the date format is like the input dd:mm:yyyy
I'm not sure but the catch node seems doesn't add more debug messages.

I think the Insert string correct is with '{{{payload.Giorno}}}' + ' ' + '{{{payload.Ora}}}' because in the middle between date and time there is a space. Without it, as in your string, this is the error message I have:
image
Please, look at the timestamp. It is in the same format I need (dd:mm:yyy).
With days minor or equal to 12, the insert goes fine because if you exchange day and month, it's Always in the range.

You have pretty much done nothing I said :cry:

Please ensure debug nodes have the option "Complete msg" set...
image
... this way you WILL see extra info in the msg object


Also., please attach a debug node to all outputs of all nodes.


QUESTION: Where is your SQL string built? In the MSSQL node? or a template node?

QUESTION: Are you using node-red-contrib-mssql-plus node or the older node-red-contrib-mssql node? or?


You could export your flow (ctrl+E) and paste it in a reply (between backticks)...
```
paste exported flow here
```
... this will enable us to help you better

Sorry Steve :sweat_smile:
Added Debug nodes to all outputs. I did before to check the payload after file-in and after csv parser and data was correct. . What is not working is the debug message after SQL node. Seems that doesn't add any additional information, and I can't understand how to find msg.query properties.
I'm not sure if this could help but the query is working, if day# is minus than 12, the SQL node adds fields to the DB but with day and month exchanged.

ANSWER: Built in MSSQL Node

ANSWER: node-red-contrib-mssql-plus

Here the complete flow:

[{"id":"5324aeb8.7ec5d","type":"tab","label":"Log Quadra to SQLServer","disabled":false,"info":""},{"id":"e556d970.72ee18","type":"MSSQL-CN","z":"","name":"Scambio_NAV","server":"srv-dc","encyption":true,"database":"Scambio_NAV"},{"id":"3bd4a46.b94e45c","type":"inject","z":"5324aeb8.7ec5d","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":220,"wires":[["7e16e795.3ce558"]]},{"id":"f59b6b73.ae4af8","type":"csv","z":"5324aeb8.7ec5d","name":"msg","sep":";","hdrin":"","hdrout":false,"multi":"one","ret":"\\n","temp":"Giorno, Ora, Stato, Programma, Lotto, Scheda","skip":"0","strings":true,"x":470,"y":220,"wires":[["e3c9f0a.baf291","b9f04463.742488"]]},{"id":"7e16e795.3ce558","type":"file in","z":"5324aeb8.7ec5d","name":"Quadra DVC EVO","filename":"\\\\10.0.0.4\\log_quadra\\1102_job_test.txt","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":290,"y":220,"wires":[["f59b6b73.ae4af8"]]},{"id":"e3c9f0a.baf291","type":"debug","z":"5324aeb8.7ec5d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":670,"y":180,"wires":[]},{"id":"b9f04463.742488","type":"MSSQL","z":"5324aeb8.7ec5d","mssqlCN":"e556d970.72ee18","name":"SQL_Insert","query":"insert into Output_Produzione (Centro_di_Lavoro, data, n_ord_prod, n_operazione, articolo, VarINT1) \nvalues (9999,'{{{payload.Giorno}}}'+ ' '+'{{{payload.Ora}}}', right('{{{payload.Lotto}}}',5), left('{{{payload.Lotto}}}',4),'AP11'+'{{{payload.Programma}}}', '{{{payload.Scheda}}}')\n","outField":"payload","x":670,"y":300,"wires":[[]]},{"id":"2db4c2cc.4445ae","type":"catch","z":"5324aeb8.7ec5d","name":"","scope":["b9f04463.742488"],"uncaught":false,"x":390,"y":460,"wires":[["6685d49b.ea3b5c"]]},{"id":"6685d49b.ea3b5c","type":"debug","z":"5324aeb8.7ec5d","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":600,"y":460,"wires":[]},{"id":"d50a590b.fd1bf8","type":"MSSQL","z":"5324aeb8.7ec5d","mssqlCN":"e556d970.72ee18","name":"","query":"insert into Output_Produzione (Centro_di_Lavoro, data, n_ord_prod, n_operazione, articolo, VarINT1) \nvalues (9999,'{{{payload.Giorno}}}' + ' ' + '{{{payload.Ora}}}', right('{{{payload.Lotto}}}',5), left('{{{payload.Lotto}}}',4),'AP11'+'{{{payload.Programma}}}', '{{{payload.Scheda}}}')\n","outField":"payload","x":650,"y":580,"wires":[[]]}]

As the syntax of the MSSQL query input is MUSTACHE TEMPLATE you do not need to do SQL concatenation.

e.g. you have '{{{payload.Giorno}}}'+ ' '+'{{{payload.Ora}}}' but what youre trying to achieve is 'string1 _space_ string2'

You should do this... '{{{payload.Giorno}}} {{{payload.Ora}}}' - the space is left by the Mustache template

Try this flow...

[{"id":"c9b2af1a.4d95f","type":"inject","z":"5a0e07e.5bbf2f8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":220,"wires":[["e2432f25.ec175"]]},{"id":"b9169506.edf078","type":"csv","z":"5a0e07e.5bbf2f8","name":"msg","sep":";","hdrin":"","hdrout":false,"multi":"one","ret":"\\n","temp":"Giorno, Ora, Stato, Programma, Lotto, Scheda","skip":"0","x":470,"y":220,"wires":[["3276b0d3.13d81","308d3a7.773fac6"]]},{"id":"e2432f25.ec175","type":"file in","z":"5a0e07e.5bbf2f8","name":"Quadra DVC EVO","filename":"\\\\10.0.0.4\\log_quadra\\1102_job_test.txt","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":290,"y":220,"wires":[["b9169506.edf078"]]},{"id":"3276b0d3.13d81","type":"debug","z":"5a0e07e.5bbf2f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":650,"y":220,"wires":[]},{"id":"308d3a7.773fac6","type":"MSSQL","z":"5a0e07e.5bbf2f8","mssqlCN":"f3f7a482.3f2868","name":"SQL_Insert","query":"insert into Output_Produzione (Centro_di_Lavoro, data, n_ord_prod, n_operazione, articolo, VarINT1) \nvalues (9999,'{{{payload.Giorno}}} {{{payload.Ora}}}', right('{{{payload.Lotto}}}',5), left('{{{payload.Lotto}}}',4),'AP11{{{payload.Programma}}}', '{{{payload.Scheda}}}')\n","outField":"payload","returnType":"0","throwErrors":"0","x":670,"y":280,"wires":[["ae883555.e575c8"]]},{"id":"4f2d06c.14646f8","type":"catch","z":"5a0e07e.5bbf2f8","name":"","scope":["308d3a7.773fac6"],"uncaught":false,"x":650,"y":360,"wires":[["c1b5598c.dcfe18"]]},{"id":"c1b5598c.dcfe18","type":"debug","z":"5a0e07e.5bbf2f8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":830,"y":360,"wires":[]},{"id":"ae883555.e575c8","type":"debug","z":"5a0e07e.5bbf2f8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":830,"y":280,"wires":[]},{"id":"f3f7a482.3f2868","type":"MSSQL-CN","z":"","name":"Scambio_NAV","server":"srv-dc","encyption":true,"database":"Scambio_NAV"}]

when you get a failure, expand the error msg and yoy WILL see the query generated by the mustache template - take that and try it in SQL Studio

In the debug node click the dropdown next to Output and select Show Complete Message.

Same result. In the CSV file I have 3 records, 2 of them over day 12 and I have two times this error:
image
And only one record added to the table (the one lower than 12).
Thanks for your explanation I learnt a little bit more about mustache template.
I'm quite sure that the problem is in the datetime format between node-red-contrib-mssql-plus and MSSQL of my server. If I split in 3 parts the payload.giorno and stitch together with a different sequence could work but it's a waste of performance and I need to import many thousands of rows. At the end it's not so elegant.
Other Ideas?

I'm certain your timestamp is incorrect too - thats why I asked you to "Add debug (with "complete msg") set after each node (so you can see format of data before and after each node" and why I asked you to check the msg.query

example...

once you find this, you can Copy/Paste the generated query and test it yourself in SQL Server Manager

Once you understand why your QUERY is incorrect, you can add a FUNCTION node before the MSSQL node to do conversion on the date time (you could even LEFT and RIGHT trim in NODE-RED instead of in the SQL QUERY

1 Like

Here is a clue to your problem. By running node in a simple cmd line - you can see what dates JS likes...

image

my guess is you need a transform step before the MSSQL node (that converts the date & time into an ISO standard format)

  • OR -

you need to use the SQL CONVERT functions in your INSERT query to coerce the string into a date type.

Steve… sorry again... but I don't have (or see) the msg.query :sob:
It could be very helpful…I'm thinking that my environment is not complete and I'm not so skilled to understand what happens.
In the meanwhile I have tried the way of left, right and mid (in the SQL Node) but mid is not recognized, as substring. :exploding_head:
The other solution, for me, is out of skill. I don't know what you mean when you write running node in cmd line and convert in sql. Tried but without results.

What versions do you have?

open a terminal / command prompt and enter
node -v
npm -v

In node-red, open the pallete manager and look in the "installed" list - what version of MSSQL do you have installed?
image

in node-red, under menu, what version do you have?

Also, try this demo flow - see if you can expand the debug output - compare it to my screen shot in previous post.

[{"id":"87e2527.bb1f0b","type":"inject","z":"a96cce5.282c93","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":60,"wires":[["44a9df5d.d3ce3"]]},{"id":"e25bb35b.d6c6c","type":"csv","z":"a96cce5.282c93","name":"msg","sep":";","hdrin":"","hdrout":false,"multi":"one","ret":"\\n","temp":"Giorno, Ora, Stato, Programma, Lotto, Scheda","skip":"0","strings":true,"x":410,"y":120,"wires":[["ac337215.d6678","39634da4.b9c5c2","9cd5b277.d0c9f"]]},{"id":"44a9df5d.d3ce3","type":"file in","z":"a96cce5.282c93","name":"Quadra DVC EVO","filename":"\\\\10.0.0.4\\log_quadra\\1102_job_test.txt","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":290,"y":60,"wires":[["e25bb35b.d6c6c"]]},{"id":"ac337215.d6678","type":"debug","z":"a96cce5.282c93","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":750,"y":120,"wires":[]},{"id":"39634da4.b9c5c2","type":"MSSQL","z":"a96cce5.282c93","mssqlCN":"47f92084.1a0af","name":"SQL_Insert","query":"insert into Output_Produzione (Centro_di_Lavoro, data, n_ord_prod, n_operazione, articolo, VarINT1) \nvalues (9999,'{{{payload.Giorno}}} {{{payload.Ora}}}', right('{{{payload.Lotto}}}',5), left('{{{payload.Lotto}}}',4),'AP11{{{payload.Programma}}}', '{{{payload.Scheda}}}')\n","outField":"payload","returnType":"0","throwErrors":"0","x":610,"y":180,"wires":[["c0253a0d.0aba38"]]},{"id":"3e144157.4a543e","type":"catch","z":"a96cce5.282c93","name":"","scope":["39634da4.b9c5c2"],"uncaught":false,"x":570,"y":260,"wires":[["f85477fe.4e9cf8"]]},{"id":"f85477fe.4e9cf8","type":"debug","z":"a96cce5.282c93","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":750,"y":260,"wires":[]},{"id":"c0253a0d.0aba38","type":"debug","z":"a96cce5.282c93","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":750,"y":180,"wires":[]},{"id":"13de8553.8c230b","type":"inject","z":"a96cce5.282c93","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":120,"wires":[["ea008fb6.193d"]]},{"id":"ea008fb6.193d","type":"template","z":"a96cce5.282c93","name":"fake data","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"22-02-2020;12:00;col3_row1;col4_row1;col5_row1\n22-02-2020;12:01;col3_row2;col4_row2;col5_row2\n22-02-2020;12:02;col3_row3;col4_row3;col5_row3","output":"str","x":260,"y":120,"wires":[["e25bb35b.d6c6c","8feb88b5.17b418"]]},{"id":"8feb88b5.17b418","type":"debug","z":"a96cce5.282c93","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":250,"y":180,"wires":[]},{"id":"9cd5b277.d0c9f","type":"debug","z":"a96cce5.282c93","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":410,"y":180,"wires":[]},{"id":"47f92084.1a0af","type":"MSSQL-CN","z":"","name":"Scambio_NAV","server":"srv-dc","encyption":true,"database":"Scambio_NAV"}]
1 Like

node -v = v13.7.0
npm -v = 6.13.6
In nodered menu = v1.0.3

The demo flow doesn't has the Query in the debug output. Always the same message about out-of-range value.
This is the installed list:

Many thanks for this huge support, and patience, and full immersion training!

And there is your problem. Remove the old 0.0.7 - its conflicting the newer fork version.

FYI, way back, many of the different MSSQL node-red nodes use the same base code (forks) and thus they conflict each other.

PS, you may have to temporarily delete all SQL nodes (and the config nodes) to uninstall the old 0.0.7 version.

After you remove it, restart node-red. You may need to check your MSSQL settings but they should be ok.

1 Like

Hi Steve, you are a jump ahead! Uninstalled and reinstalled two times (??) the first time was not possible to connect to SQL Server. I can't explain but it's so.
Now the environment is like yours :-), with more features.

In my opinion, the query must be:
insert into Output_Produzione (Centro_di_Lavoro, data, n_ord_prod, n_operazione, articolo, VarINT1)
values (9999, substring('{{{payload.Giorno}}}',4,2)+left('{{{payload.Giorno}}}',2)+right('{{{payload.Giorno}}}',4)+' '+'{{{payload.Ora}}}', right('{{{payload.Lotto}}}',5), left('{{{payload.Lotto}}}',4),'AP11{{{payload.Programma}}}', '{{{payload.Scheda}}}')

Doesn't work!
During my tests, I did a mistake and with this query it works:

insert into Output_Produzione (Centro_di_Lavoro, data, n_ord_prod, n_operazione, articolo, VarINT1)
values (9999, right('{{{payload.Giorno}}}',2)+substring('{{{payload.Giorno}}}',4,2)+left('{{{payload.Giorno}}}',2)+' '+'{{{payload.Ora}}}', right('{{{payload.Lotto}}}',5), left('{{{payload.Lotto}}}',4),'AP11{{{payload.Programma}}}', '{{{payload.Scheda}}}')

It's completely wrong, unless "YYMMDD hh:mm:ss" is a supported daytime format, I've checked a CSV file of 932 records and all of them are correct in the DB. :dizzy_face:

Even if it works, It's not the solution.

(IMO) That is not an elegant way to achieve this. Far too messy and overly complicated. Even WITH the msg.query output of the updated SQL node, you still have an INSERT with all sorts of overly complex crap in!

You'd be far better off (IMO) putting a function node between the CSV and the SQL nodes to transform the data into a perfectly formed object containing the exact values you need to build your query.

EXAMPLE

Put a function node after CSV mode but before MSSQL

function formatDT(datepart, timepart){
  let formatted = '';
  //format the datepart & timepart into t a SQL compatible format
  //... code here
  //...
  return formated;
}

//transform payload object into a nice new object...
let row= {...msg.payload};
msg.payload = {
  Centro_di_Lavoro: 9999,
  dt: formatDT( row.Giorno, row.Ora), //Call a function to make the correct DATETIME string
  n_ord_prod : row.Lotto.substr(-5),  //last 5 characters same as RIGHT(xxx,5)
  n_operazione: row.Lotto.substr(0, 4),  //first 4 characters same as LEFT(xxx,4)
  //... and so on
}
//Now we have an object with the EXACT values to write to DB
//return the msg with nicely formatted data, ready to insert straight into the query {{mustach}}
return msg;

Then your MSSQL query template becomes a simple & elegant mustache statement...

IINSERT INTO Output_Produzione (
  Centro_di_Lavoro, 
  data, 
  n_ord_prod,  
  n_operazione, 
  articolo, 
  VarINT1
) VALUES (
  payload.Centro_di_Lavoro,
  '{{payload.dt}}',
  '{{payload.n_ord_prod}}',
  '{{payload.n_operazione}}',
  '{{payload.articolo}}',
  '{{payload.VarINT1}}'
}

Give it a go - its not difficult.

If you get stuck, send me ...

  • some sample data (perhaps attach a file with 10 sample rows in).
  • a table create script (something like CREATE TABLE Output_Produzione ( column1 datatype, column2 datatype, ...)

... and I will return you a fully working flow demonstrating this technique

Hi Steve, I appreciate your effort to help me! I really appreciate but I'm not so skilled with javascript, Never wrote a JS line of code. When I started, three days ago with nodered my target was to use the GUI and SQL for a simple procedure… never thought about all of this problems with datetime format.
To be honest I'm going ahead without understanding the real issue. Why I need to change the datetime format? Which is the datetime format used by MSSQL Node?
These are sample rows:

02/12/2019;15:21:44;START;132300A;0030/23456;1
02/12/2019;15:22:47;END;132300A;0030/23456;2
02/12/2019;16:18:38;START;132400A;0030/23457;10
02/12/2019;16:19:41;END;112400A;0030/23457;11
02/12/2019;16:28:44;START;112400A;0030/23458;11
02/12/2019;16:30:46;END;112400A;0030/23458;12
02/12/2019;18:32:40;START;112800A;0030/23459;99
02/12/2019;18:34:44;END;112800A;0030/23459;101
03/12/2019;10:25:30;START;112800A;0030/23459;102
03/12/2019;10:27:34;END;112800A;0030/23459;103
03/12/2019;11:15:40;START;113900A;0030/23459;104
03/12/2019;11:17:54;END;113900A;0030/23459;105
03/12/2019;12:35:30;START;115600A;0030/25443;42
03/12/2019;12:37:44;END;115600A;0030/25443;43
04/12/2019;09:15:11;START;149800A;0030/25442;0
04/12/2019;09:17:26;END;149800A;0030/25442;1
04/12/2019;09:18:18;START;149800A;0030/25442;1
04/12/2019;09:19:33;END;149800A;0030/25442;2
04/12/2019;09:25:01;START;149800A;0030/25442;2
04/12/2019;09:26:16;END;149800A;0030/25442;3
05/12/2019;11:34:21;START;137500A;0030/25667;2
05/12/2019;11:35:18;END;137500A;0030/25667;3
05/12/2019;11:38:45;START;137500A;0030/25667;2
05/12/2019;11:39:42;END;137500A;0030/25667;3
11/12/2019;13:43:05;START;180302bt;0100/37707;0
11/12/2019;13:44:31;END;180302bt;0100/37707;1
11/12/2019;13:46:07;START;180302bt;0100/37707;1
11/12/2019;13:47:31;END;180302bt;0100/37707;2
11/12/2019;13:47:56;START;180302bt;0100/37707;2

You didnt provide the CREATE TABLE sql instruction as requested. So I cant provide an accurate demo.

Also, i'll need to know what goes where.

e.g. for data row 05/12/2019;11:39:42;END;137500A;0030/25667;3
05/12/2019;11:39:42; --> to column xxx
END --> column yyyy
137500A --> xyz
0030/25667 --> right 5 charcaters to column abc
etc etc

OK, as you haven't replied, I cant test the DATABASE input and I've had to make several assumptions (i.e. there are likely some problems but you should be able to correct them)...

First, a screen shot demonstrating my earlier post...

This is the transform function - it is heavily commented - you should try to understand it

/*** transform payload object into a nice new object ***/


//A function to convert the DATE and TIME into a format the database likes
function formatDT(datepart, timepart){
    //split the dd/mm/yyy string by / into a 3 part array
    let dateparts = datepart.split('/');
    //now put the date parts (in reverse order) 
    //back into a string and add the time part 
    let newDateStr = `${dateparts[2]}/${dateparts[1]}/${dateparts[0]} ${timepart}`
    return newDateStr;
}

//get 
//store original in a msg property (for checking in debug output)
msg.origPayload = msg.payload;

//make a copy of the row data
let row = {...msg.payload};

//example data 11/12/2019;13:47:31;END;180302bt;0100/37707;2
//Giorno-       - 11/12/2019
//Ora           - 13:47:31
//Stato         - END
//Programma     - 180302bt
//Lotto         - 0100/37707
//Scheda        - 1

var lottoParts = row.Lotto.split("/");
msg.payload = {
  a: 9999, //not sure what this is about?
  DateTime: formatDT(row.Giorno, row.Ora), //Call a function to make the correct DATETIME string
  LottoRight5 : lottoParts[1],  //characters right of the '/' 
  LottoLeft4: lottoParts[0],  //characters left of the '/' 
  Stato: row.Stato, //add Stato to the payload
  Programma: row.Programma, //add Programma to the payload
  Scheda: row.Scheda //add Scheda to the payload
}

//Now we have an object with the clean values to write into DB
//return the msg with nicely formatted data, ready to insert straight into the query {{mustach}}
return msg;

Now the clean values in payload can be mapped straight into the SQL query...

INSERT INTO Output_Produzione (
  Centro_di_Lavoro, 
  data, 
  n_ord_prod,  
  n_operazione, 
  articolo, 
  VarINT1
) VALUES (
  payload.a, --not certain what this is supposed to be
  '{{payload.DateTime}}',
  '{{payload.LottoRight5}}',
  '{{payload.LottoLeft4}}',
  '{{payload.Programma}}',
  '{{payload.Scheda}}'
}

This is the flow...

[{"id":"87e2527.bb1f0b","type":"inject","z":"a96cce5.282c93","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":60,"wires":[["44a9df5d.d3ce3"]]},{"id":"e25bb35b.d6c6c","type":"csv","z":"a96cce5.282c93","name":"msg","sep":";","hdrin":"","hdrout":false,"multi":"one","ret":"\\n","temp":"Giorno, Ora, Stato, Programma, Lotto, Scheda","skip":"0","strings":true,"x":410,"y":120,"wires":[["9cd5b277.d0c9f","e14f2462.7363a8"]]},{"id":"44a9df5d.d3ce3","type":"file in","z":"a96cce5.282c93","name":"Quadra DVC EVO","filename":"\\\\10.0.0.4\\log_quadra\\1102_job_test.txt","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":290,"y":60,"wires":[["e25bb35b.d6c6c"]]},{"id":"39634da4.b9c5c2","type":"MSSQL","z":"a96cce5.282c93","mssqlCN":"47f92084.1a0af","name":"SQL_Insert","query":"INSERT INTO Output_Produzione (\n  Centro_di_Lavoro, \n  data, \n  n_ord_prod,  \n  n_operazione, \n  articolo, \n  VarINT1\n) VALUES (\n  payload.a,\n  '{{payload.DateTime}}',\n  '{{payload.LottoRight5}}',\n  '{{payload.LottoLeft4}}',\n  '{{payload.Programma}}',\n  '{{payload.Scheda}}'\n}","outField":"payload","returnType":"0","throwErrors":"0","x":610,"y":180,"wires":[["c0253a0d.0aba38"]]},{"id":"3e144157.4a543e","type":"catch","z":"a96cce5.282c93","name":"","scope":["39634da4.b9c5c2"],"uncaught":false,"x":570,"y":260,"wires":[["f85477fe.4e9cf8"]]},{"id":"f85477fe.4e9cf8","type":"debug","z":"a96cce5.282c93","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":750,"y":260,"wires":[]},{"id":"c0253a0d.0aba38","type":"debug","z":"a96cce5.282c93","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":750,"y":180,"wires":[]},{"id":"13de8553.8c230b","type":"inject","z":"a96cce5.282c93","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":120,"wires":[["ea008fb6.193d"]]},{"id":"ea008fb6.193d","type":"template","z":"a96cce5.282c93","name":"fake data","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"02/12/2019;15:21:44;START;132300A;0030/23456;1\n02/12/2019;15:22:47;END;132300A;0030/23456;2\n02/12/2019;16:18:38;START;132400A;0030/23457;10\n02/12/2019;16:19:41;END;112400A;0030/23457;11\n02/12/2019;16:28:44;START;112400A;0030/23458;11\n02/12/2019;16:30:46;END;112400A;0030/23458;12\n02/12/2019;18:32:40;START;112800A;0030/23459;99\n02/12/2019;18:34:44;END;112800A;0030/23459;101\n03/12/2019;10:25:30;START;112800A;0030/23459;102\n03/12/2019;10:27:34;END;112800A;0030/23459;103\n03/12/2019;11:15:40;START;113900A;0030/23459;104\n03/12/2019;11:17:54;END;113900A;0030/23459;105\n03/12/2019;12:35:30;START;115600A;0030/25443;42\n03/12/2019;12:37:44;END;115600A;0030/25443;43\n04/12/2019;09:15:11;START;149800A;0030/25442;0\n04/12/2019;09:17:26;END;149800A;0030/25442;1\n04/12/2019;09:18:18;START;149800A;0030/25442;1\n04/12/2019;09:19:33;END;149800A;0030/25442;2\n04/12/2019;09:25:01;START;149800A;0030/25442;2\n04/12/2019;09:26:16;END;149800A;0030/25442;3\n05/12/2019;11:34:21;START;137500A;0030/25667;2\n05/12/2019;11:35:18;END;137500A;0030/25667;3\n05/12/2019;11:38:45;START;137500A;0030/25667;2\n05/12/2019;11:39:42;END;137500A;0030/25667;3\n11/12/2019;13:43:05;START;180302bt;0100/37707;0\n11/12/2019;13:44:31;END;180302bt;0100/37707;1\n11/12/2019;13:46:07;START;180302bt;0100/37707;1\n11/12/2019;13:47:31;END;180302bt;0100/37707;2\n11/12/2019;13:47:56;START;180302bt;0100/37707;2","output":"str","x":260,"y":120,"wires":[["e25bb35b.d6c6c","8feb88b5.17b418"]]},{"id":"8feb88b5.17b418","type":"debug","z":"a96cce5.282c93","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":250,"y":180,"wires":[]},{"id":"9cd5b277.d0c9f","type":"debug","z":"a96cce5.282c93","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":410,"y":180,"wires":[]},{"id":"e14f2462.7363a8","type":"function","z":"a96cce5.282c93","name":"transform data","func":"/*** transform payload object into a nice new object ***/\n\n\n//A function to convert the DATE and TIME into a format the database likes\nfunction formatDT(datepart, timepart){\n    //split the dd/mm/yyy string by / into a 3 part array\n    let dateparts = datepart.split('/');\n    //now put the date parts (in reverse order) \n    //back into a string and add the time part \n    let newDateStr = `${dateparts[2]}/${dateparts[1]}/${dateparts[0]} ${timepart}`\n    return newDateStr;\n}\n\n//get \n//store original in a msg property (for checking in debug output)\nmsg.origPayload = msg.payload;\n\n//make a copy of the row data\nlet row = {...msg.payload};\n\n//example data 11/12/2019;13:47:31;END;180302bt;0100/37707;2\n//Giorno-       - 11/12/2019\n//Ora           - 13:47:31\n//Stato         - END\n//Programma     - 180302bt\n//Lotto         - 0100/37707\n//Scheda        - 1\n\nvar lottoParts = row.Lotto.split(\"/\");\nmsg.payload = {\n  a: 9999, //not sure what this is about?\n  DateTime: formatDT(row.Giorno, row.Ora), //Call a function to make the correct DATETIME string\n  LottoRight5 : lottoParts[1],  //characters right of the '/' \n  LottoLeft4: lottoParts[0],  //characters left of the '/' \n  Stato: row.Stato, //add Stato to the payload\n  Programma: row.Programma, //add Programma to the payload\n  Scheda: row.Scheda //add Scheda to the payload\n}\n\n//Now we have an object with the clean values to write into DB\n//return the msg with nicely formatted data, ready to insert straight into the query {{mustach}}\nreturn msg;","outputs":1,"noerr":0,"x":580,"y":120,"wires":[["fcc4bfd8.b885c","39634da4.b9c5c2"]]},{"id":"fcc4bfd8.b885c","type":"debug","z":"a96cce5.282c93","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":750,"y":120,"wires":[]},{"id":"47f92084.1a0af","type":"MSSQL-CN","z":"","name":"Scambio_NAV","server":"srv-dc","encyption":true,"database":"Scambio_NAV"}]