I want to convert Netezza
TO_TIMESTAMP(TO_CHAR(POL.PERIOD_FROM_DATE, 'FM99999999999999999'), 'YYYYMMDDHH24MISSMS') AS EFFECTIVE_DATE
to a SQL Server function to include this in IBM Cognos Framework Manager
Required output: 2023-02-24 00:00:00.0
Can anyone please help me with this?
4条答案
按热度按时间jecbmhm31#
It looks like
PERIOD_FROM_DATE
is a numeric(17,0) column, with each digit matching a position in a datetime value, down to the 1000th of a second.This was a poor schema choice, but I understand you may have no say here. Additionally, the Oracle code handles this in about the worst way possible. Thanks to cultural/internationalization issues, converting between strings and numeric or date values is not the simple process we often assume; rather it's about the slowest and most error-prone approach you can take.
Assuming the schema is set in stone (but really: change it if you can), to solve this for SQL Server in a better way we'll break it into two parts:
DateTime
value from the numeric inputNote: you're generally much better off simply returning a raw DateTime, and letting the client code or tooling handle the format. This lets you skip part two completely, and it's what the Oracle code was doing. That you saw a specific string format was an accident of the tooling, as the result returned from Oracle was binary and not a human-readable value at all.
Again, I'll assume you can't control this requirement, but to the degree you have the ability you should push to fix this (because the current design really is broken).
For part one, we want to use the
DATETIMEFROMPARTS()
method :It seems like you might only want the first digit from the milliseconds. If so, you can adjust that portion accordingly as I don't have enough information to know whether to round or truncate the remainder.
This may look like a lot of code and separate access to the column, but I promise you it should be faster and more reliable than the string alternative, and not by a small margin. It does still assume the data is stored consistently.
Once you have this
DateTime
value, you can get a formatted string using eitherCONVERT()
orFORMAT()
, with the former preferred when possible. Unfortunately, none of the built in formats use a single digit with the fractional seconds, so you will need to do this:See it work here:
https://dbfiddle.uk/W4wv24YG
pftdvrlh2#
You should use the time to convert your number column zto a proper datetime and only use that in future.
every convertion tale a lot of time, so date shpould always be stored as such datatypes
fiddle
sauutmhj3#
dnph8jn44#
To avoid using vendor specific functions (like, for SQL Server) and make this more portable across any data source you may be connecting Cognos to, this probably should have written like this in the first place:
You can also do it similarly using numbers:
Here is an example Cognos report spec using the GO Sales (query) sample data source demonstrating this in data items expressions in a report. You would use the same expressions in Framework Manager.