Type mismatch error occurs while casting string values to date type in MDX query with some blank rows - ssas

I have a string-type dimension in my SSAS cube having the date values along with some blank values and please find the screenshot for your reference.
I am trying to convert these values into Date using the CDate function in the MDX query. I have used the following MDX query which will return null when the value is either empty or null and returns the date value for valid values.
WITH
MEMBER [Measures].[Company].[OrderDate] AS 'CASE WHEN ISEMPTY([Company].[OrderDate].CurrentMember.MemberValue) OR [Company].[OrderDate].CurrentMember.MemberValue = null THEN null Else CDate([Company].[OrderDate].CurrentMember.MemberValue) END' SELECT{
[Measures].[Company].[OrderDate]} ON COLUMNS ,Subset(NONEMPTY((
(Order((([Company].[OrderDate].[OrderDate].ALLMEMBERS)), CDate([Company].[OrderDate].CurrentMember.Member_Caption),ASC))),{[Measures].DefaultMember}),0,100) ON ROWS FROM [Model] CELL PROPERTIES VALUE, FORMATTED_VALUE, FORMAT_STRING
However, When am trying to convert these string values into Date I am getting the Type mismatch error as shown in the following screenshot:
Can anyone please suggest how to convert the string dimension values to date type when blank values are present in the dimension?

Related

Converting a numeric value into Date value in DB2

I have a DB2 table where NUM column is defined as INTEGER in DB2 and the query result is shown below,
NUM columns have numeric values which needs to be converted to date format. This numeric values are nothing but duration from 01.01.1850. Example : 01.01.1850 + 57677 days = 01.12.2007.
So Is it possible to convert or cast the numeric value into date fields in DB2 , so that the select query from the table can result as shown below after converting a numeric field into date field,
You may use the scalar ADD_DAYS function:
SELECT EMP_ID, ADD_DAYS('1850-01-01', NUM) AS NUM
FROM yourTable;
Not all Db2 products & versions have the ADD_DAYS function.
The following expression works for all of them.
You may optionally add DAY or DAYS at the end.
DATE ('1850-01-01') + 57677

How to retrieve the column with mandatory decimal point in Sql 2008?

I am retrieving a numeric value from one of the column in Sql. However I have to retrieve it by three decimal point (eg below).
Though if the value in the table is in the decimal point, then its giving the result as expected. But in case the value is not in decimal format and only an integer it must display .000
Below is the example.
Value in table= 2
Retrieval Value = 2
Required Value = 2.000
As per your requirement convert your incoming value in Decimal(18,3). like given below,
select convert(decimal(18,3),2)
To really show a value in a particular format, you can convert the value to a string. One method uses the str() function:
str(x, 18, 3)
Note that you can also cast() the decimal:
select cast(cast(x as decimal(18, 3)) as varchar(19))

Difference between two dates returns date instead of number [DAX]

In PowerPivot DAX formulas, I need the number of days between two dates.
My formula is
ProjectDays:=[ProjectFinish]-[ProjectStart]
But instead of a number, it returns a date.
Any ideas?
ProjectDays:= VALUE( [ProjectFinish] - [ProjectStart] )
VALUE() is the poorly named 'convert to numeric type' function.

SQL Server ISDATE() Error

I have a table and need to verify that a certain column contains only dates. I'm trying to count the number of records that are not follow a date format. If I check a field that I did not define as type "date" then the query works. However, when I check a field that I defined as a date it does not.
Query:
SELECT
count(case when ISDATE(Date_Field) = 0 then 1 end) as 'Date_Error'
FROM [table]
Column definition:
Date_Field(date, null)
Sample data: '2010-06-27'
Error Message:
Argument data type date is invalid for argument 1 of isdate function.
Any insight as to why this query is not working for fields I defined as dates?
Thanks!
If you defined the column with the Date type, it IS a Date. Period. This check is completely unnecessary.
What you may want to do is look for NULL values in the column:
SELECT SUM(case when Date_Field IS NULL THEN 1 ELSE 0 end) as 'Date_Error' FROM [table]
I also sense an additional misunderstanding about how Date fields, including DateTime and DateTime2, work in Sql Server. The values in these fields are not stored as a string in any format at all. They are stored in a binary/numeric format, and only shown as a string as a convenience in your query tool. And that's a good thing. If you want the date in a particular format, use the CONVERT() function in your query, or even better, let your client application handle the formatting.
ISDATE() only evaluates against a STRING-like parameter (varchar, nvarachar, char,...)
To be sure, ISDATE()'s parameter should come wrapped in a cast() function.
i.e.
Select isdate(cast(parameter as nvarchar))
should return either 1 or 0, even if it's a MULL value.
Hope this helps.
IsDate takes a character string or exression that yeilds a character string as it's argument
The problem is this method ISDATE() only admits arguments of type datetime and smalldatetime within the "time" types, so it won´t work if you are using date type.
Also if you use date as type for that field, you won´t have to check the information there because it won´t admit other type of field.
You shoul only check for null values in your column, that´s all.

Convert string to DateTime in SSIS

The data from source has string data type and sometime has invalid dates, empty column ('') I want to check whether it is valid date and convert string to date time. How can i do this in SSIS?
My string value is already formatted as mm/dd/yyyy.
Here is an expression that you can use in a Derived Column Transformation that will convert the date to a DT_DBTIMESTAMP data type. Or, it will set the value to null (with DT_DBTIMESTAMP data type) if it is null or blank. The DT_DBTIMESTAMP data type matches the SQL Server DATETIME data type.
ISNULL(MyDate) || TRIM(MyDate) == "" ? NULL(DT_DBDATE) : (DT_DBDATE)MyDate
If the input value is not a valid date you can map the error output path to a another Derived Column Transformation that assigns a null to the column.
What datetime value should an empty string be transformed into? NULL? You could use a derived columns transformation and / or script transformation.