I'm having trouble with a line of code in MySQL. I am attempting to write the following line of code into a select statement that I already have and that works. I have a comma both before and after the line to handle the field prior to this and after this. The support_due_date field is a date field.
ISNULL(DATE_FORMAT(support_due_date, '%m/%d/%Y'), '01/01/1900') as support_due_date2
I'm getting a syntax error. The support_due_date field has some null values and some date values. I am wanting to format the field to have a M/D/YYYY format and if the field is Null change it to 01/01/1900. What am I doing wrong? Any help would be appreciated. I'm using version 5.2.47 if that helps.
ISNULL() by itself merely evaluates to true or false based on the argument passed; I believe your syntax error is due to attempting to pass 2 comma-separated arguments to that function. What you're actually looking for is, I think, something more like if(isnull(support_due_date), '01/01/1900', date_format(support_due_date, '%m/%d/%Y')) as support_due_date2 .
The documentation for control flow statements (including if()) is here.
That said, eggyal makes a good point about magic values in their comment to your question - imho there is a time and a place for both approaches, just something to consider.
Related
I'm trying to convert the date field from YYYY-MM-DD HH:MM:SS:MS to MM/DD/YYYY format in a view using Snowflake database with below condition:
TO_VARCHAR(DATE(SRC_DATE),'MM/DD/YYYY')
I'm able to convert the date to expected format with above condition, but when I try to load data from this view to different table using a sp its failing with below error:
Failed: Code: 939 - State: 22023 - Message: SQL compilation error: error line 1 at position 1,058
too many arguments for function [TO_VARCHAR(VALID_FROM_DATE, 'YYYYMMDDHH24MISS.FF9')] expected 1, got 2 - Stack Trace: Statement.execute, line 9 position 58
Need help in getting the right logic to fix this error. How can I do this?
You just need to do:
TO_VARCHAR(SRC_DATE,'MM/DD/YYYY')
I’m wondering why you are trying to change the format of a date in Informatica? A date is held as a number, the format is just how it is displayed - the underlying number doesn’t change
I'm not sure exactly what's going on yet, but I can walk through some things we do know.
First, the error message says the TO_VARCHAR() function only expected one argument. Knowing this we can look at the documentation for the method. Here we see there are several overloads:
TO_VARCHAR( <expr> )
TO_VARCHAR( <numeric_expr> [, '<format>' ] )
TO_VARCHAR( <date_or_time_expr> [, '<format>' ] )
TO_VARCHAR( <binary_expr> [, '<format>' ] )
Only one of these overloads (the first) fits the error message. Most of the overloads allow multiple arguments, but only if the first argument matches certain types: numeric, date/time, or binary. This does include the expected date result of the DATE() function
Therefore we can conclude somehow the result of the DATE(SRC_DATE) call is NOT a valid <date_or_time_expr> in every case, such that we at least sometimes end up with the first overload.
While the documentation for Date() does allow several ways for the function to return NULL, it also explicitly returns a Date type:
The data type of the returned value is DATE.
Thus I'd still expect it to always match the third overload above. The only other possible result from Date() is the conversion fails, in which case we'd see a different error message entirely.
The best explanation I could guess at is the return type for Date() doesn't matter if the result is NULL (that is: NULL is inherently untyped for this purpose), such that you're still ending up with the first overload, which does not allow the 2nd argument.
You could possibly fix this by adding a COALESCE() so NULL is converted to a valid consistent throw-away date expression; something like, say, 1900-01-01. If it's important to preserve NULL values you can then in turn also wrap the whole thing in a NULLIF() call.
Finally, all of this only make sense is SRC_DATE is not already a valid <date_or_time_expr>. That is, if it's something like a varchar column. This in itself would already be a mistake in schema design. On the other hand, if it is already a datetime column, there is no need at all to call Date(), and it can be used directly with TO_VARCHAR()... but with likely the same caveat about NULL values you're already seeing.
We can test this theory by trying the following:
TO_VARCHAR(DATE(IFNULL(SRC_DATE,'1900-01-01')),'MM/DD/YYYY')
I'm having trouble building a query in Access 2013. The database isn't mine and the only thing I really have control over is this query. There is a table, I'm pulling 7 fields from it and eventually adding an 8th field to the query to do some string manipulation.
However, I keep getting getting "Syntax error in date in query expression 'fieldname'." error whenever I click on the arrow to sort the fields. The odd thing is these errors pop up when sorting non-date fields. When sorting the date field I get "Syntax error (missing operator) in query expression 'Release Date'."
This happens after a fresh build. I have no WHERE conditions, just SELECT and FROM. Ideas?
Here's the sql query, though I'm mainly working in the query design view:
SELECT Transmissions.[Job#], Transmissions.[Part#], Transmissions.TransmissionSN, Transmissions.Status, Transmissions.[Release Date], Transmissions.[Build Book Printed], Transmissions.[ID Tags Required]
FROM Transmissions;
Well... it seems you are the lucky inheritor of a poorly designed database.
Using special characters in a field name is just asking for trouble. And you've found what that trouble is.
Access uses the # sign to designate a Date type for query comparisons. Such as:
dtSomeDate = #2/20/2017#
You surround the date with the # signs.
In your case, the query thinks [Job#] and [Part#] are trying to wrap dates. But of course, that's not the case and thus it fails.
You can try a couple of work arounds. (I leave it to you to experiment.)
1) You can try to rename the problem fields within your query. So that:
Transmissions.[Job#] becomes Transmissions.[Job#] as JobNum
and
Transmissions.[Part#] becomes Transmissions.[Part#] as PartNum
2) You can try to copy the [Transmissions] table to a new table that you create
that does not have the naming problems.
3) Export the [Transmissions] table to a CSV file and re-import it to a new
table (or possibly new database) without the naming problems.
Here is a link to a microsoft article that tells you why to avoid special characters in Access:
Big Bad Special Chars.
Hope that puts you on the right track. :)
Typically, this means that the field names are missing or misspelled.
Try running this to see:
SELECT * FROM Transmissions;
I'm using the "ASC" function in Access (Version 365 Proplus, 32 bit).
Have created a query that uses a table with a post code that needs validating. I'm looking at the first character in the postcode, converting it to ASCII character then planning on filtering out the ones I don't want.
The formula looks like this:-
Site_PostCode_String_Validation_P1: Asc(Left([site_postcode],1))
This works fine and converts as expected. However, when I try sorting or filtering using the Query Criteria on my Ascii list I get the following message:-
"Data Type Mismatch in Criteria Expression"
I have tried converting to a string, for example, using the below:-
Str(Asc(Left([site_postcode],1)))
But this has made no difference, get the same error message when applying criteria or sorting.
I have tried filtering using text and numbers but get the same error.
I have searched here and have Googled but can not see anything relating to the above.
Thanks for any suggestions.
You might simplify it a bit:
Site_PostCode_String_Validation_P1: Asc(Nz([site_postcode], Chr(0))
I tried creating a new table based on the results, thinking that this would then enable me to apply my filtering, during this process I got a more detailed error message stating error due to Null type conversion failure. So then realized that I need to make sure there were no Null entries before doing the ascii conversion. Hopefully this will be of help to someone else. Final formula now works as below:
IIf(IsNull([site_postcode])=-1,Null,Asc(Left([site_postcode],1)))
I was trying to enter date into MySQL and was trying to figure out the right syntax through trial and error. In one such command, MySQL accepted the value, however when I displayed the values it showed all zeroes.
can anyone help me understand why?
If you are not passing Date in default format then you need to intimate system that I am passing this string as date by mentioning format of date as describe below.
INSERT INTO test VALUES STR_TO_DATE('03-12-2016','%d-%m-%Y');
Hopefully this will help.
you must ensure that the value passed to mysql is in year-month-day sequence ("YYYY-MM-DD")
INSERT INTO test VALUES ('2016-03-02');
note:- please use another keyword instead inbuilt keyword.date is inbuilt mysql keyword.
Read here
Try this and also check your date format(data type) set in the database
INSERT INTO test VALUES ('2-03-2016')
Trial and error? This isn't some uncharted scientific territory where you need to strap on some goggles and use a bunsen burner. There's a whole chapter in the manual devoted to it.
Dates in MySQL should be in ISO-8601 format, that is YYYY-MM-DD or YYYY-MM-DD HH:MM:SS in 24-hour notation.
Please, before wasting tons of your time on pointless experimentation:
READ THE MANUAL
So I am currently working on a migration from an old Advantage database server to SQL 2005 using SSIS 2008. One of the columns in the old Advantage database is a MEMO type. By default this translates to a DT_TEXT column. Well in the new database I do not need this large of field, but can limit it to something such as VARCHAR(50). I successfully set up a derived column transformation to convert this with the following expression:
(DT_STR,50,1252)[ColumnName]
Now I want to go a step further and replace all NULL values with an empty string. This would seem easy enough using an ISNULL([ColumnName])?"":(DT_STR,50,1252)[ColumnName] expression, but the problem is that the OLE DB Destination contains the following error
Cannot convert between unicode and non-unicode strings...
So apparently the whole ISNULL expression converts the data type to Unicode string [DT-WSTR]. I have tried a variety of casts upon the whole expression or different parts, but I cannot get the data type to match what I need it.
First, is it possible to convert the DT_TEXT type directly to unicode? From what I can tell, the casts don't work that way. If not, is there a way to get an expression to work so that NULL values get converted to empty strings?
Thank you for all your help!
Give this a try in your derived column.
(DT_STR,50,1252) (ISNULL(ColumnName) ? "" : (DT_STR,50,1252) ColumnName)
It includes an additional type cast with the Conditional (?:) in parentheses to ensure the desired processing sequence. I think your original expression was implicitly casting to DT_WSTR because the "" defaults to DT_WSTR. With this new version, you force the cast to DT_STR after the expression is evaluated.
I figured something out that works. It may not be the best solution, but it will work for my situation.
From my OLE DB source I first did a Derived Column. This I used the ISNULL which ended up converting it to a DT_WSTR unicode type. although I could not get any casts to get it back to the type required, I then added a Data Conversion transformation in-between the Derived Column and the OLE DB Destination. This would take the input string and convert it back to a DT_STR. This all feels a little annoying converting so many times, but the column does not contain any funky information that I should have to worry about, so I suppose it will work.
Thanks for all those who pondered the solution, and if you find some awesome way to tackle it, I would be more than interested.