'Nodejs Service returning datetime instead of just date when trying to cast datetime field to date of a SQL Server Query
My database is SQL Server. I have the following sql querySELECT 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?
Solution 1:[1]
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
Solution 2:[2]
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'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | user6565467 |
Solution 2 | James |