• Webinars
  • Docs
  • Download
  • Blogs
  • Contact Us
Try NCache Live
Show / Hide Table of Contents

SQL Datetime Function Syntax and Usage

Note

This feature is only available in NCache Enterprise Edition.

NCache provides you with an ease of searching the cache by providing parameters in various formats. You can use various functions in a single query for faster cache searching.

You can use DateTime in your query to search the cache with respect to a particular date or time. Make sure that you specify the date in the correct format. NCache supports various date formats, for more detail on these formats please refer to the DateTime Struct provided by Microsoft.

Pre-Requisites

  • .NET/.NET Core
  • Java
  • Node.js
  • Install the following NuGet packages:
    • Alachisoft.NCache.SDK.
  • Indexing for searchable objects and their attributes need to be configured first as explained in Configuring Query Indexes in Administrator's Guide.
  • Include the following namespaces in your application:
    • Alachisoft.NCache.Client
    • Alachisoft.NCache.Runtime.Caching
    • Alachisoft.NCache.Client.Services
    • Alachisoft.NCache.Runtime.Exceptions
  • The application must be connected to cache before performing the operation.
  • Cache must be running.
  • For API details, refer to: ICache, ICacheReader, ExecuteReader, SearchService, QueryCommand.
  • Make sure that the data being added is serializable.
  • To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
  • To handle any unseen exceptions, refer to the Troubleshooting section.
  • Add the following Maven dependencies in your pom.xml file:
<dependency>
    <groupId>com.alachisoft.ncache</groupId>
    <artifactId>ncache-client</artifactId>
    <version>5.2.0</version>
</dependency>
  • Indexing for searchable objects and their attributes need to be configured first as explained in Configuring Query Indexes in Administrator's Guide.
  • Import the following packages in your application:
    • import com.alachisoft.ncache.client.*;
    • import com.alachisoft.ncache.runtime.exceptions.*;
  • The application must be connected to cache before performing the operation.
  • Cache must be running.
  • For API details, refer to: Cache, CacheReader, executeReader, getSearchService(), QueryCommand.
  • Make sure that the data being added is serializable.
  • To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
  • To handle any unseen exceptions, refer to the Troubleshooting section.
  • Indexing for searchable objects and their attributes need to be configured first as explained in Configuring Query Indexes in Administrator's Guide.
  • Include the following modules in your application.
    • const ncache = require('ncache-client')
  • The application must be connected to cache before performing the operation.
  • Cache must be running.
  • For API details, refer to: Cache, CacheReader, executeReader, getSearchService(), QueryCommand.
  • To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
  • To handle any unseen exceptions, refer to the Troubleshooting section.

Syntax

The following example executes a query which searches for all the orders in the cache whose orderDate is lesser than the date specified using ExecuteReader.

  • .NET/.NET Core
  • Java
  • Node.js
try
{
    // Pre-condition: Cache is already connected
    // Create a query which will be executed on the data set
    // Use the Fully Qualified Name (FQN) of your own custom class
    string query = "SELECT * FROM FQN.Order WHERE OrderDate < ?";

    // Use QueryCommand for query execution
    var queryCommand = new QueryCommand(query);

    // Providing parameters to query 
    queryCommand.Parameters.Add("OrderDate", new DateTime (2017, 02, 01));

    // Executing QueryCommand through ICacheReader
    ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand);

    // Check if the result set is not empty
    if (reader.FieldCount > 0)
    {
        while (reader.Read())
        {
            // Get the value of the result set
            int result = reader.GetValue<int>("OrderID");

            // Perform operations
        }
    }
    else
    {
        // Null query result set retrieved
    }
}
catch (OperationFailedException ex)
{
    if (ex.ErrorCode == NCacheErrorCodes.INCORRECT_FORMAT)
    {
        // Make sure that the query format is correct
    }
    else
    {
        // Exception can occur due to:
        // Connection Failures
        // Operation Timeout
        // Operation performed during state transfer
    }
}
catch (Exception ex)
{
    // Any generic exception like ArgumentException, ArgumentNullException
}
try
{
    // Pre-condition: Cache is already connected
    // Create a query which will be executed on the data set
    // Use the Fully Qualified Name (FQN) of your own custom class
    String query = "SELECT * FROM FQN.Order WHERE OrderDate < ?";

    // Use QueryCommand for query execution
    QueryCommand queryCommand = new QueryCommand(query);

    // Providing parameters to query
    queryCommand.getParameters().put("OrderDate", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

    // Executing QueryCommand through CacheReader
    CacheReader reader = cache.getSearchService().executeReader(queryCommand);

    // Check if the result set is not empty
    if (reader.getFieldCount() > 0)
    {
        while (reader.read())
        {
            // Get the value of the result set
            int result = reader.getValue("OrderID", Integer.class);

            // Perform operations
        }
    }
    else
    {
        // Null query result set retrieved
    }

}
catch (OperationFailedException ex)
{
    if (ex.getErrorCode() == NCacheErrorCodes.INCORRECT_FORMAT) {
         // Make sure the query format is correct
    }
    else
    {
        // Exception can occur due to:
        // Connection Failures
        // Operation Timeout
        // Operation performed during state transfer
    }

}
catch (Exception ex)
{
    // Any generic exception like IllegalArgumentException or NullPointerException
}
// This is an async method
try
{   
    // Pre-condition: Cache is already connected
    // Create a query which will be executed on the data set
    // Use the Fully Qualified Name (FQN) of your own custom class
    var query = "SELECT * FROM FQN.Order WHERE OrderDate < ?";

    // Use QueryCommand for query execution
    var queryCommand = new ncache.QueryCommand(query);

    // Providing parameters to query
    var date = new Date("2021-02-10");
    let map = new Map();
    var parameters = map.set("orderDate", date);
    queryCommand.setParameters(parameters);

    // Executing QueryCommand through CacheReader
    var searchService = await this.cache.getSearchService();
    var reader = await searchService.executeReader(queryCommand);

    // Check if the result set is not empty
    if (reader.getFieldCount() > 0)
    {
        while (reader.read())
        {
            // Get the value of the result set
            var result = reader.getValue(1, Number());

            // Perform operations
        }
    } 
    else
    {
        // Null query result set retrieved
    }
}
catch (error)
{
    // Handle errors
}

Additional Resources

NCache provides sample application for SQL Searching on GitHub.

See Also

SQL Search for Keys Syntax and Usage
SQL IN Operator Syntax and Usage
SQL Like Operator Syntax and Usage
SQL GROUP BY Syntax and Usage
Query Operators
Search Cache with LINQ

Back to top Copyright © 2017 Alachisoft