I've seen a few posts on here and dotted around on the internet but I'm still struggling to implement my findings to get this to work.
I am currently trying to pull through a date field and run parameters on this field however everything I do fails and it still comes through as text or I experience an error.
Here is my current code:
SELECT CEL_SLT.ACCOUNT,
CEL_SLA.NAME,
CEL_SLT.STOCK_CODE,
**CEL_SLT.DATE**,
CEL_SLT.STOCK_QTY,
CEL_SLT.AMOUNT,
CEL_SLT.ORDER_NO
FROM Datafile.dbo.CEL_SLA CEL_SLA, Datafile.dbo.CEL_SLT CEL_SLT
WHERE CEL_SLA.ACCOUNT = CEL_SLT.ACCOUNT
AND ((CEL_SLT.STOCK_CODE Like 'F%' And CEL_SLT.STOCK_CODE Not Like 'FNX%')
AND (CEL_SLT.ACCOUNT=?)
I need the field CEL_SLT.DATE to come through as a date field.
I hope this makes sense and someone can help.
Assuming that your data is clean (in other words, the text data actually looks like a date), you can do it with either the CAST or CONVERT command.
SELECT CAST(CEL_slt.DATE as date)
Related
I'm attempting to understand what exactly date-fns is doing when using parseISO() as it seems to be adding 2 hours to the time that I have saved in the database. The data is as follows:
ISO Date/Time saved in database: 2022-09-20T19:52:53.000Z
Using format(parseISO(2022-09-20T19:52:53.000Z), "HH:mm:ss") I get this: 21:52:53 hrs
Somewhere along the line 2 hours are being added and unsure as to the cause. A comment left by Monash Joshi in (stackoverflow issue) hints towards an explanation but not really a solution. Want I want to show is just the parsed date/time value as it is. Would it thus make sense to manipulate the string that I receive from the database myself?
Thanks in advance for your help! And let me know if you need any further information.
I'm New to salesforce reporting so i'm sure what i'm trying to get might be easy, but i'm not getting the results I want.
Basically I want to be able to extract the HOUR from a date/time field.
I have tried this:
VALUE(MID (TEXT (CREATED_DATE:UNIQUE), 12, 2))
But it gives me an '-' as an output rather then the actual hour.
Any idea as to what I might be doing wrong here?
Thank you!
(Opening the following on behalf of a Snowflake client...)
When I try to insert into the table it threw below error:
Numeric value 'abc_0011O00001y31VpQAI' is not recognized
Have check the table DDL and found only 3 columns defined as NUMBER and rest as VARCHAR.
I checked the SELECT query and didnot find any string value in those NUMBER Datatype columns. Also tried searching in all the Varchar columns for the value 'abc_0011O00001y31VpQAI' , I didn't find any
I know one thing Snowflake doesn't always shows correct error. Am I missing anything here? Is there any way to fix it?
Both COL4_MRR and COL5_QUANTITY are NUMBER
INSERT INTO TABLE
(COL1_DATE, COL2_CD, COL3_CUST_NAME, COL3_LOC_NAME,COL4_MRR,COL5_QUANTITY)
SELECT
'2019-10-03' AS COL1_DATE ,
'AE' AS COL2_CD
,CUSTOMER_NAME AS COL3_CUST_NAME
,LOCATION_NAME AS COL3_LOC_NAME
,MRR_BILLED as COL4_MRR
,QTY_BILLED as COL5_QUANTITY
FROM SCHEMA.V_TABLEA
union all
SELECT
'2019-10-03' AS COL1_DATE ,
'BE' AS COL2_CD
,CUSTOMER_NAME AS COL3_CUST_NAME
,LOCATION_NAME AS COL3_LOC_NAME
,NULL as COL4_MRR
,QTY_BILLED as COL5_QUANTITY
FROM SCHEMA.V_TABLEB
I created a table_D same as original TABLE and tried inserting into it , it worked fine . Then Inserted into Original TABLE from table_D , it worked again .
Deleted those rows from original TABLE and reran the job , it worked fine.
There was no issue with data as all was Number only, I even tried with TRY_TO_NUMBER too. It inserted the data without any changes to the code.
...............
Client is currently waiting on a next day run to re-test to determine if this is either a bug or an issue with their data. In the meantime, we are interested to see if anyone else has run into similar challenges and have a viable recommendation. THANK YOU.
The error typically means you are trying to insert non-numeric data (like 'abc_0011O00001y31VpQAI') into a numeric column. It seems like the customer did everything right in testing and TRY_TO_NUMBER() is a great way to verify numeric data.
Do the SELECT queries run fine separately? If so, then I would check whether there might be a potential mismatch in the datatype of the columns and make sure they are in the right order.
I would also check whether or not the header is being skipped in the file (that may be where the 'abc_0011O00001y31VpQAI' is coming from since the customer did not see it in the data).
SELECT queries work fine, I tried creating a new table with same DDL as original and tried loading into that new table, it worked fine. Not sure why it is not loading into the original table
hopefully the title describes what I'm trying to do.
I have a varchar field in a SQL Server 2008 table that contains text dates in the format dd-mm-yyyy (e.g., 31-12-2009). I am trying to use CONVERT to convert it to a DATE field. I was successful in converting a similar varchar field in the same table using the following:
SELECT DISTINCT(CONVERT(DATE, MYDATEFIELD1, 103)) AS [CONV_MYDATEFIELD1] FROM MYTABLE;
But when I apply the same to MYDATEFIELD2, which appears to have the same type of data values as MYDATEFIELD1, it fails with the following error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
I've tried sorting and using LIKE to try to find any characters that might prevent the conversion but I haven't been able to pinpoint anything.
Any help will be greatly appreciated. Thanks!
You may have some invalid dates (e.g. 30-02-2009), try to find them splitting the characters and validating the day and the months, assuring that the days correspond to the month and the month is in the range 01 - 12.
If you can't find which value is causing the conversion error then use a cursor to go through all the records individually and use TRY CATCH to find which record(s) cause the conversion error. You could use a PRINT statement in the CATCH block to identify the records that are erroring.
Find your bad dates with the following:
SET DATEFORMAT dmy;
select MYDATEFIELD1, isdate(MYDATEFIELD1)
from MYDATEFIELD1
I figured out the issue that was causing the CONVERT to fail but I'm not sure of the best way to select an answer (veritable stack noob) so, any help on that would be appreciated. Here are the major steps I took to find the issue:
I used MIN and MAX SUBSTRING to identify that the component parts of the
varchar field were correct (i.e., the 1st two digits min=01 max=31,
middle two min=01 max=12)
I used DISTINCT SUBSTRING to identify that all of the date separators were consistent (i.e., all dashes).
I used MAX(LEN) to determine that my varchar "date" field was 12 characters (vs. the 10 characters I was expecting).
I used CONVERT(VARBINARY, MYDATEFIELD2) to determine what was actually stored in the string.
The last step revealed that the field contained line feeds (00A). I opened the source text file in notepad++, clicked View -> Show Symbol -> Show All Characters and I could see the LF at the end of each line.
So now I'm modifying the DTSX package (fixed width text) to include an extra field for the linefeed that I can drop afterwards. Now that I know what the intended format of the date fields is, I'll try to import them as DT_DATE vs DT_STR. I'm not exactly sure how to specify the correct date style 105 at import (thanks #Panagiotis Kanavos) but I'll figure it out.
Whew! What a learning experience! :D
Thanks to everyone who helped - and if you can give advice on the best way to select the best answer it will be greatly appreciated.
Looking for help on this one. I'm working on a report in Report Builder that uses data from the Analysis Services cube and it is giving me a lot of problems when it come to any date/time data. I am trying to build a dynamic report that will allow the report to update depending on when it is viewed. I do this by setting parameters (#FromDateDate and #ToDateDate). Unfortunately MDX seems to hate dates which makes no sense to me.
My goal is to get data over a span of three months with the #ToDateDate being Today() and the #FromDateDate being 3 months in the past which I am able to achieve with this function (=DateAdd(DateInterval.Day,-90,Today()) ). However those don't go well with MDX.
My query looks like this:
SELECT
NON EMPTY
{[Measures].[Work Item Count]} ON COLUMNS
,NON EMPTY
{
[Date].[Year - Month - Date Hierarchy].[Date].ALLMEMBERS*
[Work Item].[System_State].[System_State].ALLMEMBERS*
[Work Item].[Microsoft_VSTS_Common_Severity].[Microsoft_VSTS_Common_Severity].ALLMEMBERS
}
DIMENSION PROPERTIES
MEMBER_CAPTION
,MEMBER_UNIQUE_NAME
ON ROWS
FROM
(
SELECT
StrToMember
(#FromDateDate
,CONSTRAINED
)
:
StrToMember
(#ToDateDate
,CONSTRAINED
) ON COLUMNS
FROM
(
SELECT
{[Work Item].[System_WorkItemType].&[Bug]} ON COLUMNS
FROM
(
SELECT
{
[Team Project].[Team Project Hierarchy].&[{6F43CBFD-2E98-4CA7-B428-0B732603517A}]
} ON COLUMNS
FROM [Work Item]
)
)
)
WHERE
(
[Team Project].[Team Project Hierarchy].&[{6F43CBFD-2E98-4CA7-B428-0B732603517A}]
,[Work Item].[System_WorkItemType].&[Bug]
)
CELL PROPERTIES
VALUE
,BACK_COLOR
,FORE_COLOR
,FORMATTED_VALUE
,FORMAT_STRING
,FONT_NAME
,FONT_SIZE
,FONT_FLAGS;
I was able to figure out how to essentially inject he appropriate format by adjusting my Parameter Values in the Dataset Properties to this -
="[Date].[Date].&["+format(Parameters!FromDateDate.Value,"yyyy-MM-ddThh:mm:ss")+"]"
My two parameters have default values of :
=DateAdd(DateInterval.Day,-90,Today()) and =Today()
When I run my report I get the following error:
The restrictions imposed by the CONSTRAINED flag in the STRTOMEMBER function were violated.
Now if I remove CONSTRAINED from the STRTOMEMBER function I get another error. I have also tried removing the entire STRTOMEMBER function and just using the parameters which I can get to work in Management Studio but not in Report builder. Please Help! I have attempted so many different ways and still no success. Let me know what additional information is needed.
Another thing -
If I remove the T in the date format of the Parameter Value in the Dataset Property to look like this:
="[Date].[Year - Month - Date Hierarchy].[Date].&["+format(Parameters!FromDateYearMonthDateHierarchy.Value,"yyyy-MM-dd hh:mm:ss")+"]"
I get the below error:
The '2017-06-08 12:00:00' string cannot be converted to the date type.
So here is the solution if anyone is interested. I've seen a ton of forum posts online with no one having a concrete answer so I can see this is a common problem - the workaround at this link is valid and does work:
Using Dynamic MDX in Reporting Services
My issue was the format of my "injected" date. When running the query generated by my #ToDateDate and #FromDateDate parameters...
(note: after changes I made my parameters are now
#FromDateYearMonthDateHierarchy and '#ToDateYearMonthDateHierarcy`
which you will notice in my Parameter Value function)
My Parameter Values appeared in the following format:
[Date].[Year - Month - Date Hierarchy].[Date].&[2010-12-31T00:00:00]
In order to fix the issue for me my "injection statement" had to look like this:
"[Date].[Year - Month - Date Hierarchy].[Date].&[" & Format(CDate(Parameters!FromDateYearMonthDateHierarchy.Value),"yyyy-MM-dd") & "T00:00:00]"
With the actual date formatting of "yyyy-MM-dd") & "T00:00:00]" so in the end the hours minutes and second was what was giving me the headache. The injection works perfectly but just need to pay attention to the Formatting of the date and time. I genuinely hope this helps someone!