# Preparation of a Data string for insert into MSQL database

**URL:** <https://discourse.nodered.org/t/preparation-of-a-data-string-for-insert-into-msql-database/18597>\
**Category:** General\
**Created:** [29 November 2019 08:59 UTC](https://discourse.nodered.org/t/preparation-of-a-data-string-for-insert-into-msql-database/18597 "2019-11-29T08:59:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francesco](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/francesco/32/12491_2.png) [@Francesco](https://discourse.nodered.org/u/Francesco)\
**Post date:** [29 November 2019 08:59 UTC](https://discourse.nodered.org/t/preparation-of-a-data-string-for-insert-into-msql-database/18597/1 "2019-11-29T08:59:47Z")

</div>

Hello everyone  
I ran into a small problem, currently solved, but I would like to understand.  
Preparing a string to introduce a date in the DB I need to have the fields month, hour, minutes and seconds always expressed with two characters.  
Unfortunately, however, it seems with ". Length" to fail to check the string length, why?

I enclose the code I'm using below.  
Surely it will be nonsense but I have to understand to avoid making stupid mistakes again.

The date in the query is now correct :  
`{"topic":"QueeryInsert","_msgid":"4860460f.55db48","query":"INSERT INTO Tb_DB (Data, Linea, NScheda, NRiga, QtaAv)\nVALUES ('2019-11-29T10:01:51.000Z', 7, 0, 0, 594);"}`

CODE

```auto
// Funzione per il trattamento della Data e conversione 
// in stringa data completa di ora e fuso per insert nel DataBase
function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear(),
        HH = d.getHours(),
        MM = d.getMinutes(),
        SS = d.getSeconds();
        

    MM = MM.toString();
    SS = SS.toString();
    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;
    // --------------------------------------------------------
    // On HH MM and SS length don't work 
    // --------------------------------------------------------

    //if (HH.length < 2) HH = '0' + HH;
    //if (MM.length < 2) MM = '0' + MM;
    //if (SS.lenght < 2) SS = '0' + SS;
    // --------------------------------------------------------
    // On HH MM and SS the comparison by value works !! 
    // --------------------------------------------------------
    if (SS < 10) SS = '0' + SS;
    if (MM < 10) MM = '0' + MM;
    if (HH < 10) HH = '0' + HH;
    
    /*
    if (MM == '0') MM = '0' + MM;
    if (MM == '1') MM = '0' + MM;
    if (MM == '2') MM = '0' + MM;
    if (MM == '3') MM = '0' + MM;
    if (MM == '4') MM = '0' + MM;
    if (MM == '5') MM = '0' + MM;
    if (MM == '6') MM = '0' + MM;
    if (MM == '7') MM = '0' + MM;
    if (MM == '8') MM = '0' + MM;
    if (MM == '9') MM = '0' + MM;
    
    if (HH == '0') HH = '0' + HH;
    if (HH == '1') HH = '0' + HH;
    if (HH == '2') HH = '0' + HH;
    if (HH == '3') HH = '0' + HH;
    if (HH == '4') HH = '0' + HH;
    if (HH == '5') HH = '0' + HH;
    if (HH == '6') HH = '0' + HH;
    if (HH == '7') HH = '0' + HH;
    if (HH == '8') HH = '0' + HH;
    if (HH == '9') HH = '0' + HH;
    
    if (SS == '0') SS = '0' + SS;
    if (SS == '1') SS = '0' + SS;
    if (SS == '2') SS = '0' + SS;
    if (SS == '3') SS = '0' + SS;
    if (SS == '4') SS = '0' + SS;
    if (SS == '5') SS = '0' + SS;
    if (SS == '6') SS = '0' + SS;
    if (SS == '7') SS = '0' + SS;
    if (SS == '8') SS = '0' + SS;
    if (SS == '9') SS = '0' + SS;
    */
    
    var D1 = '\'' + year + '-' + month + '-' + day + 'T' + HH + ':' + MM + ':' + SS + '.000Z' + '\'';  

    return D1;
}

var tempString =formatDate(Date().toString());

// Verifica se esiste la variabile globale
// Se non esiste la crea con valore pari a 0

var DateH=flow.get('DateH') || 0;

flow.set('DateH',tempString);

msg.payload=tempString;

return msg;

```

Thanks a lot to everyone

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [30 November 2019 18:11 UTC](https://discourse.nodered.org/t/preparation-of-a-data-string-for-insert-into-msql-database/18597/2 "2019-11-30T18:11:16Z")

</div>

Have a look at the JavaScript padStart function:

> **[String.prototype.padStart()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart)**
>
> The padStart() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length.

---

<div class="post-metadata">

**Author:** ![Francesco](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/francesco/32/12491_2.png) [@Francesco](https://discourse.nodered.org/u/Francesco)\
**Post date:** [30 November 2019 20:30 UTC](https://discourse.nodered.org/t/preparation-of-a-data-string-for-insert-into-msql-database/18597/3 "2019-11-30T20:30:19Z")

</div>

Thanks so much
