In SQL, setting default values in a string is a common practice

//I am attempting to incorporate default variables into my SQL string to avoid syntax errors when loading my code. However, for some reason, adding the if statement is not producing any results.

{`var serversLength = Request.Form("servers").count;
    var serversTemp = (serversLength) ? Request.Form("servers") : false;
    var servers = [];
    for (i = 1; i <= serversLength; i++) {
       servers.push(serversTemp(i));
    }
    var options = "";
    options +=  Request.Form("options");
    %> 

<%
    var dateLength = Request.Form("date").count;
    var dateTemp = (dateLength) ? Request.Form("date") : false;
    var date= [];
    for (i = 1; i <= dateLength; i++) {
       date.push(dateTemp(i));
    } %>
    <%
    var date2Length = Request.Form("date2").count;
    var date2Temp = (date2Length) ? Request.Form("date2") : false;
    var date2= []; 
    if (date2 === undefined){
    date2="9/29/2015";
    }
    for (i = 1; i <= date2Length; i++) {
       date2.push(date2Temp(i));
    } %>

  var SQL="SELECT LINKEDNAME, ERRNUM,CONVERT(CHAR(16),TSTAMP,21) AS TSTAMP, ERRDESC FROM MAIN.dbo.LINKEDSERVERFAILS " + ((serversLength) ? "WHERE LINKEDNAME IN ('" + servers.join("''") + "')" : "") + "AND TSTAMP BETWEEN" + ((dateLength) ? "('" + date.join("/ ") + "')" : "") + " AND " + ((date2Length) ? "('" + date2.join("/ ") + "')" : "") +""}

Answer №1

The variable date2 is actually an array, so assigning a string to it will replace the entire array contents.

Instead of directly assigning a value, use the date2.push("9/29/2015"); method to add the date to the array without overwriting existing data.

Keep in mind that once you declare var date2 = [];, date2 becomes an object and is no longer undefined. Therefore, the if statement checking for undefined will never evaluate as true.

To check if the array is empty, verify its length like this:

if (date2.length === 0){
    date2.push("9/29/2015");
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best approach for determining the most effective method for invoking a handler function in React?

As a newcomer to React, I am navigating through the various ways to define callback functions. // Approach 1 private handleInputChange = (event) => { this.setState({name: event.target.value}); } // Approach 2 private handleInputChange(event){ t ...

Adjust the dimensions of the thead, tbody, and td elements on the fly

I've implemented this property in a .css file on the table shown below and there are 9 records. .fixed tbody td, thead th { width: 5.2%; float: left; } In my scenario, when there are 4 columns, the width should be 23.2%; for 5 columns ...

Calculating the sum in MySQL using conditions with jOOQ

When using MySQL, it is possible to pass a condition as a parameter to the SUM() function. SELECT COUNT(*), SUM(value > 100) FROM TABLE1; This technique is illustrated in this answer. But how can I achieve the same result in JOOQ? ...

Reinvent the AJAX functionality

Is there a way to create a custom function that changes the default ajax functionality? I currently have this ajax function implemented: $.ajax({ type: "POST", url: "http://" + document.location.host + '/userajax', data: 'type= ...

Smoothly automate horizontal scrolling using JavaScript

My programming journey involved creating a code snippet that automatically scrolls paragraphs horizontally, giving it the appearance of "Breaking News" updates on popular websites. The Javascript script I implemented performs automatic scrolling, but once ...

Executing Datalist's Delete Command through Page Methods Implementation

Recently, I came across an issue with my DataList and Update Panel on my webpage. I noticed a significant delay in response time after incorporating the Update panels... intrigued, I delved deeper into this phenomenon and found some interesting insights in ...

Require field change in SharePoint 2010

I have implemented the following code on a SharePoint page - it locates the specified select based on its title and triggers an alert when the "Decision" value is selected. I am interested in removing the alert and instead substituting with code that iden ...

The Angular application is receiving a 404 error when trying to access the .NET Core

Having trouble calling a method in the controller via URL, as I keep encountering a 404 error. What could be the issue? API Endpoint: http://localhost:5000/Home/HomeTest /*.net core web-api*/ namespace MyApp.Controllers { Route("api/[controller]") ...

Tips for overlaying an image on a div regardless of its height

(!) Although this question may seem repetitive, I have not been able to find a suitable solution in any of the previous 10 topics. I apologize for the inconvenience and am actively seeking a resolution to this unique situation; Allow me to outline the iss ...

The date time chart in high charts is not displaying the X-Axis width correctly

Currently, I am utilizing the high charts library to display a date-time column chart. However, there seems to be an issue with the x-axis not displaying the exact starting value for the chart. In the example provided below, it is necessary to adjust the b ...

Pattern matching to specifically omit a specific numerical value

One of my models is a Department model. class Department < ActiveRecord::Base # Associations belongs_to :client belongs_to :facility scope :facility_departments, ->(facility_id) { whe ...

The search bar is preventing the props from being updated

Whenever I navigate between columns on my website, the <ArticleList> is supposed to update. It works flawlessly when moving from the home page to a column, or from an article to a column. However, the issue arises when going from one column to anothe ...

Developing a MySQL DB-driven Node.js dashboard without the need for a poller

After exploring various StackOverflow posts on the topic, I haven't been able to find a solution that fits my specific situation. We have multiple monitoring instances across our network, each monitoring different environments (such as Nagios, Icinga ...

Preventing browser flickering when using 'loadURL' to switch between URLs in Electron

Within my electron app, I am utilizing a singular window. As users navigate through the various "forms" within the application, I make use of the mainWindow.loadURL('file://...') function. Unfortunately, this approach leads to a flashing effect w ...

Unable to establish a connection to MySQL: Access denied for user 'root'@'localhost' with the specified password

I am attempting to create a simple login system in php+mysql, but every time I click the submit button, I encounter this error: Could not connect to MySQL: Access denied for user 'root'@'localhost' (using password: YES) This is the ...

What exactly does the Javascript object syntax signify? (specifically in a Vue script)

Can anyone explain the purpose of this statement and the difference between using const and var? const { SearchIcon } = myApp.icons; I am currently exploring Vue and still relatively new to Javascript. This code snippet appeared in a tutorial example. The ...

Dispatching information to a designated Google Analytics tracking code

Within our website, we have a unique dimension that is configured on Google Analytics and utilized when sending the page view event: gtag('config', 'UA-TrackingCode', { 'custom_map': { 'dimension1': &apo ...

Disabling modal popup buttons in Bootstrap 4 using Javascript

JSFiddle DEMO: https://jsfiddle.net/2yx16k8L/ I'm encountering an issue with my JS code. I want a click on a div to open the entire content within it. However, this action is causing the modal button in the dropdown to stop working. How can I resolve ...

Enhance your search experience with categorized autocomplete

Trying my hand at implementing a Jquery Autocomplete with categories and Ajax, Here's the javascript code I found on the Jquery UI website: <script> $.widget( "custom.catcomplete", $.ui.autocomplete, { _renderMenu: function( ul, items ...

Struggling with this mixed content problem

Encountering a problem while loading a js file: https://code.jquery.com/jquery-1.11.1.min.js, which is being loaded on my webpage in this manner: <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script> Upon loading my webpage, an ...