Difference between dateformat() and createODBCDate() in ColdFusion - coldfusion

What is the difference between dateformat() and createODBCDate() in ColdFusion? Are these two functions the same or not? When do I need to use DateFormat() and when do I need to use createODBCDate()?

dateFormat() accepts a date and a format 'mask' and returns a string of the date, in the format passed.
For example, consider the following code:
mydate = dateFormat( now(), 'yyyy-mm-dd' );
Assuming the date is July 15, 2014 (which it was when I wrote this) the value of the variable named 'mydate' would be '2014-07-15' (without the quotes). So, you need to pass a date to the function.
createODBCDate() creates an actual date from the values passed - it does not format the date, it merely creates a date 'object'
dateFormat() is typically used to display a date in a user friendly manner. Try running this writeDump( now() ) to see what the default display looks like.
createODBCDate() is typically used when you need to pass a date to a SQL query. However, if you use cfqueryparam with a cf_sql_type that accepts a date, ColdFusion will handle converting the value (assuming it is a valid date) to a date that the database accepts and you do not need to use createODBCdate()
In 10+ years of doing ColdFusion, I have never used createODBCDate()

Related

Date comparison in power bi dax

I had a problem with my code. It returns the below error:
DAX comparison operates do not support comparing values of type date with values of type text.
Basically, I want to count rows based on some conditions. And I know there is a need to convert the data type, but I am not sure how to do it.
Total Open Issues =
--------------------
--basic info
VAR SELECTEDDATE =
DATEVALUE(SELECTEDVALUE(Calender[FullDateAlternateKey].[Date]))
--------------------
--FIND the relvent data
VAR rlvttable =
calculatetable(
Tracker,
Tracker[Catagory]="ISSUE",
DATEVALUE(Tracker[ClosedDate])>SELECTEDDATE
||Tracker[ClosedDate]=""
)
--------------------
--Results
Return
countrows(rlvttable)
Anyone could advise me how to correct it? Thanks~
Check the data type of columns Tracker[ClosedDate] and Calender[FullDateAlternateKey] - one of them is Text, rather than Date.
To fix, you could:
choose a different field which is already a Date format
change the format of the offending column
use DATEVALUE in your measure, to convert the text date to a real date.
It also looks like you need to edit this statement, as these conditions conflict:
Tracker[ClosedDate]>SELECTEDDATE
&&Tracker[ClosedDate]=""
I am trying to compare the closedDate with "". I should use blank() instead.

informatica datetime datatype format

I want to convert the string 20160101000000 into datetime format using expression. I have used below date function
TO_DATE(PERIOD_END_DATE),'MM/DD/YYYY HH24:MI:SS')
But my table file is not loading. My session and workflow gets succeed. My target and source is also flatfile.
I want to change the string 20160101000000 into MM/DD/YYYY HH24:MI:SS for loading data into my target table.
You need to give exact format that looks so that to_date function can understand that format and converts it into date.
TO_DATE(PERIOD_END_DATE,'YYYYMMDDHH24MISS')
So here your date looks like YYYYMMDDHH24MISS (20160101000000).
There is often confusion with the TO_DATE function... it is in fact for converting a string into a date and the function itself is to describe the pattern of the incoming date. Now if you want to convert a date field to a specified date format you must use TO_CHAR

How parse datetime format from datebase?

The database (SQLite) has a field of type REAL with the values of the form (42153.659595).
How to translate this value in the form "dd.MM.yy HH:mm:ss" if 42153.659595 = 29.05.2015 15:49:49 ?
You can be explicit about what calendar system you require: http://www.sqlite.org/lang_datefunc.html
SELECT julianday('now') - julianday('1776-07-04');
In principle just don't "parse" (you mean: interpret raw representation). Use Sqlite API/builtin SQL functions to do it for you
In the interest of information:
The date and time functions use a subset of IS0-8601 date and time formats.
The datetime() function returns "YYYY-MM-DD HH:MM:SS". The julianday() function returns the Julian day - the number of days since noon in Greenwich on November 24, 4714 B.C. (Proleptic Gregorian calendar)
The date() function returns the date in this format: YYYY-MM-DD. The time() function returns the time as HH:MM:SS.
The correct value is achieved using the API/builtin SQL functions (42153.659595 - value from database):
SELECT datetime(julianday(42153.659595, 'localtime') + 2415018.29167) AS DT;
Output:
DT |
2015-05-29 15:49:49|
Constant 2415018.29167 was selected manually and query:
SELECT datetime (2415018.29167);
returns the current Greenwich Mean Time.
I work with a third-party application and documentation on the database is missing. Perhaps this strange decision, but it works. Thank you all for answers.

Coldfusion 10 DateFormat Issue

I am using the DateFormat function to convert dates to this format: yyyy-mm-dd. This is the original format of the date: dd-mm-yyyy. Below is a snippet of the code:
<cfset newdate = #DateFormat(Trim(mydate), "yyyy-mm-dd")# />
The problem is that I get different results for different dates. For example:
If my original date is: 15-05-2013 (dd-mm-yyyy)
The result is: 2013-05-15 (yyyy-mm-dd)
However, if I change the input and:
The original date is: 01-05-2013 (dd-mm-yyyy)
The result is: 2013-01-05 (yyyy-dd-mm)
Any help or guidance as to what is wrong would be highly appreciated.
I disagree with the other answer. The real cause of the problem is that DateFormat is not designed to handle non-US date strings.
The standard CF date functions always use U.S. date parsing rules. That means when you pass in an ambiguous date string, like 01-05-2013, it is parsed according to U.S. English date conventions. In this case, month first ie "mm-dd-yyyy". So the result will always be January 5th, not May 1st.
In some cases you get lucky. With the string 15-05-2013, there is obviously no 15th month, so CF/java must swap the month and day automatically, rather than throwing an error. That is why it seems to handle some dd-mm-yyyy date strings correctly, but not others.
If you want to parse non-US date strings, you should use the LS (Locale Sensitive) date functions instead. However, according to the docs dashes ie "-" are not a standard date separator in most non-US locales: only Dutch and Portuguese (Standard). So you would either need to change the separator OR use one of those two locales when parsing the date:
lsDateFormat( myDate, "yyyy-mm-dd", "pt_PT")
Side note:
As an aside, DateFormat does expect a date object. However, like most functions in CF it is flexible enough to accept a date string as well. That allows you to use it as a lazy shortcut to convert from date string => date object => then back to (formatted) date string again. Using date objects is preferable (and you really should validate date strings as well) but that is another conversation altogether ...
The problem is that DateFormat expects a date object, and returns a string.
You're passing it a string, not a date. What you want to do is firstly turn your string (of 01-05-2013 etc) into a date object.
To do this I'd recommend using either ParseDateTime or LSParseDateTime, and/or LSDateFormat.
e.g.
<cfset originalDateString = "01-05-2013">
<!--- turn that into a Date --->
<cfset dateObject = ParseDateTime(originalDateString)>
<cfset newdateString = DateFormat(dateObject, "yyyy-mm-dd")>
Alternatively, if you know your string is always in a dd-mm-yyyy format, you could parse the string yourself, e.g. treat it as a list delimited by hyphens.
<cfset dd = listFirst(originalDateString, "-")>
<cfset mm = listGetAt(originalDateString, 2, "-")>
<cfset yy = listLast(originalDateString, "-")>

DB2 The syntax of the string representation of a datetime value is incorrect

We have a staging table that's used to load raw data from our suppliers.
One column is used to capture a time-stamp but its data-type is varchar(265). Data's dirty: about 40% of the time, there is garbage data, otherwise time-stamp data like this
2011/11/15 20:58:48.041
I have to create a report that filters some dates/timestamps out that column but where I try to cast it, I get an error:
db2 => select cast(loadedon as timestamp) from automation
1
--------------------------
SQL0180N The syntax of the string representation of a datetime value is incorrect. SQLSTATE=22007
What do I need to do in order to parse/cast the timestamp string?
The string format for a DB2 timestamp is either:
'2002-10-20-12.00.00.000000'
or
'2002-10-20 12:00:00'
You have to get your date string in either of these formats.
Also DB2 runs on a 24 hour clock even though the output sometimes uses a 12 hour clock (AM / PM)
So '2002-10-20 14:49:50' For 2:49:50 PM
Or '2002-10-20 00:00:00' For midnight. Output would be 12:00:00 AM
It seems you have a lot of garbage data, so firt of all you should check if the data is a valid timestamp in the format you expect ('2011/11/15 20:58:48.041'). We could use a simple solution - just replace all digits with '0' and check the result format:
TRANSLATE(timestamp_column,'0','0123456789','0') = '0000/00/00 00:00:00.000'
If the format is the expected one, you should convert to DB2 timestamp. In DB2 for iSeries there is a build-in function since V6R1 TIMESTAMP_FORMAT. In your case it will look like that:
TIMESTAMP_FORMAT('2011/11/15 20:58:48.041','YYYY/MM/DD HH24:MI:SS.NNNNNN')
So the solution query combined should look something like that:
SELECT
CASE
WHEN TRANSLATE(timestamp_column,'0','0123456789','0') = '0000/00/00 00:00:00.000'
THEN TIMESTAMP_FORMAT(timestamp_column,'YYYY/MM/DD HH24:MI:SS.NNNNNN')
ELSE NULL
END
FROM
your_table_with_bad_data
EDIT
I just saw your comment that provider agreed to clean the data. You could use the solution provided to speed up the process and clean the data by yourself:
ALTER your_table_with_bad_data ADD COLUMN clean_timestamp TIMESTAMP DEFAULT NULL;
UPDATE your_table_with_bad_data
SET clean_timestamp =
CASE
WHEN TRANSLATE(timestamp_column,'0','0123456789','0') = '0000/00/00 00:00:00.000'
THEN TIMESTAMP_FORMAT(timestamp_column,'YYYY/MM/DD HH24:MI:SS.NNNNNN')
ELSE NULL
END;