Nodejs Service returning datetime instead of just date when trying to cast datetime field to date of a SQL Server Query - node.js

My database is SQL Server. I have the following sql query
SELECT RQI,RQIOver,PCI,PCIOver,PQI,PQIOver,SR,SROver,IRI,IRIOver,RUT,RUTOver,cast(DateCollected as date) as DateCollected,cast(DateCollectedOver as date) as DateCollectedOver from dbo.PF_Condition where SegmentId=12665
The result I am getting for DateCollected is just the date, which I need, when run it in SQL Management Studio. However when I put the same SQL query in a nodejs service which I created the result is not the same.
I needed to extract YYYY-MM-DD from datetime field DateCollected and DateCollectedOver as mentioned in the query above.
Can someone help me with it?

I made it work with the slice operator of javascript and sliced the date part only and displayed in the frontend where I need to. The results returned are of type string so the slice operator worked.
If someone can help why the cast function is not working in the sql query when I put it in the nodejs service it will be really helpful

After casting as DATE, you can then use the LEFT function to truncate the result to 10 characters:
LEFT(cast(DateCollected as date), 10) as DateCollected
which would return a string with the format 'YYYY-MM-DD'

Related

What does Google Big Query expects in date column?

I'm sending a JSON with some data to Google Big Query, all string and numeric columns are accepting my data just fine, however, whenever I try to send a data to the Date column it returns a error saying:
'invalid',
location: 'creationdate',
debugInfo: '',
message: "Invalid date: '2020-10-05T00:00:00.000Z'"
I'm using Node.js to work with the data and sending it to Google Big Query through the insertAll route, following the format told by them on their docs: https://cloud.google.com/bigquery/docs/reference/rest/v2/tabledata/insertAll
If I send the data as Null it works fine, I've also tried to send it as a string and as an integer, but it returns the same error.
To confirm is the data type on the BigQuery schema Date or Datetime?
You may need to truncate to fit the date format or switch to using datetime.
For some reason the Date format in Big Query only accepts YYYY-MM-DD, without hours or timezone, I had to create another column in my table so I would be able to store the hours and timezone.

Handling SQL date as string with node-mssql

I'm using node-mssql to get rows from a table that includes a date column (YYYY-MM-DD). I want to pass the date to a client application as a string in that same format. node-mssql is creating date objects, which I'm having to convert and slice at significant cost to get the format that I started with. sql.map.register(String, sql.Date) doesn't seem to work here (or I'm using it wrong). Is there a way to change how node-mssql handles the SQL date data type?
I'm having the same problem. sql.map.register is only for converting JS types to SQL types and I don't think there is any way to avoid SQL Date fields being converted into JS Date objects with node-mssql alone. I just changed my SQL query to convert to a formatted varchar instead of a Date type, e.g.: convert(varchar, birthday, 105) as dob.

Sequelize: how to get datetime column value as string

I'm using Sequelize with MySQL on my Apollo GraphQL server.
Apollo part is not very important, but here's the deal.
Problem is, Sequelize is returning datetime values as Date object, when you make queries. And Apollo-Server turns the Date object to seconds(using valueOf() function of Date object) after it gets the data from the resolver. However, Apollo server only allow String datatype for Date object, so on my client's side I'm getting some thing like "385823948287" for date values.
I need to get date values as ISO String. Or any other form that moment.js accepts.
I have to make either Sequelize return DATETIME values as ISOString, or Apollo Server to make Date Objects as ISO String, not seconds in quotes.
If you know how to do just one of those jobs, please let me know.
I solved this using Graphql ISO Date. Now I'm getting proper date string.
Install the module, add scalar DateTime and change the data types of date values to DateTime in your schema. Import the module and add it as DateTime on your type resolver.

Generic Inquiry time format

In a Generic Inquiry, I'm trying to format the time part of a DateTime field in the results. I don't currently see any way to do this without parsing the date as a string, but I must be missing something. Using the Format() function, running the query tells me "The method or operation is not implemented". Using the Minute() function gets the minutes part, but using the Hour() function says "Unsupported formula operator Hour".
Add CRCase table in your tables and don't join with any tables under Relations tab, and in result grid, under Schema field use CRCase.CreatedDateTime, with this you will get the result in DateTime format.
let me know if this is my wrong assumption to your question.

How can I get the Current Date in a Cognos query expression?

I have a query expression in Cognos where I need to compare a past date to the current date. I don't see one in the functions list and I'm otherwise unsure how to put the query date inside a query object.
How can I use the current date in a query?
Depending on your Database software, the object will be either be current_date (SQL Server) or SYSDATE{} (Oracle). If you don't know which you have, just make an expression of just the function and press the Validate button; if you get an error, you used the wrong function for your database.
You can then use this object like any other Date query object, so you can add/compare it to dates in your query or display it somewhere on the page.
The best way is to use current_date. This method is data source agnostic and will be converted to the appropriate data source equivalent at run-time.
You can use something like this with your query:
SELECT
FIELD1
FROM TABLE
WHERE
FIELD2 = current_date
Asumming that FIELD2 has a date format

Resources