I am trying to replace a NULL date with a blank. I end up with invalid date string. Ive Tried COALESCE, ISNULL, IFNULL, CASE STATEMENTS and nothing seems to work. I am querying a LINKED PROGRESS ODBC connection and using
declare #Data varchar(max)
set #Data= N'
SELECT MyCode
FROM TABLE
'
exec (#Data ) AT PROGRESS;
Ive done this many times before, I can do ISNULL, COALESCE etc just fine on all my other fields, but not the case with this Date field. Any help is greatly appreciated
I understand you are accessing a Progress database.
The Progress unknown value ? is what SQL calls NULL.
Date fields can only contain valid dates or (if the field is not mandatory) the unknown value ?. Unlike other data types, unknown values are sometimes displayed as blanks instead of ?, for example when displayed as GUI widgets.
There are no actual blank dates in Progress.
Unfortunately I don't know enough SQL to tell exactly what you are trying to achieve.
I had to make my query an open query and in the outer select do ISNULL(datefield,'')
Related
I have an Oracle database linked to a SQL Server database.
Whenever I perform a specific query in Oracle I get expected data.
The fields I'm selecting are
SELECT
J.JOBNUMBER AS JOBNUMBER,
J.OPENDATE AS OPENDATE,
S.SERVICEREPORTNUMBER AS SERVICEREPORTNUMBER,
F.SSO AS SSO,
S.DATEOFVISIT AS DATEOFVISIT,
CASE WHEN J.SOURCE='MUST' THEN SRVCD.DACRENR ELSE W.DEBRIEFDATE END as "DEBRIEFDATE",
J.CLOSEDDATE AS CLOSEDDATE,
INITCAP(HR.COUNTRY_NAME) as "COUNTRY",
F.NAME AS NAME,
SY.MODALITY AS MODALITY,
HR.MANAGER_SSOID AS MANAGER_SSOID,
HR.MANAGER_NAME AS MANAGER_NAME
If I do the same query with openquery I get null values on the column which corresponds to
CASE WHEN J.SOURCE='MUST' THEN SRVCD.DACRENR ELSE W.DEBRIEFDATE END as "DEBRIEFDATE"
In this case the column holds dates and the ones that correspond to SRVCD.DACRENR are being shown and the W.DEBRIEFDATE dates are not.
I don't know why I'm seeing those values correctly in Oracle but not in the openquery result. I tried changing the values with TO_CHAR and TO_DATE and all scecnarios I see the values in Oracle but not in SQL Server.
Could you try a convert to varchar on your date so it match the same datatype?
Or post some sample data
Convert(varchar(10), your field,105)
Not sure how you declare your variables in OPENQUERY. There is one direct way where you don't use any variables to store the query for latter execution, and indirect way which is the opposite way of direct way. Most of the times, indirect way is the best way in order to get the desired results.
First you will need to declare some variables and execute it using EXEC() function. Let's see how it is done.
DECLARE #ORACLE_ENV nvarchar(20) = 'YOUR_ORACLE_DB_INSTANCE_HERE'
DECLARE #ORACLE_SQL nvarchar(max)
SET #ORACLE_SQL = 'SELECT * FROM OPENQUERY ('+#ORACLE_ENV+',''' +SELECT
J.JOBNUMBER AS JOBNUMBER,
J.OPENDATE AS OPENDATE,
S.SERVICEREPORTNUMBER AS SERVICEREPORTNUMBER,
F.SSO AS SSO,
S.DATEOFVISIT AS DATEOFVISIT,
CASE WHEN J.SOURCE=''MUST'' THEN CONVERT(VARCHAR(15), SRVCD.DACRENR, YOUR_PREFERRED_DATE_FORMAT) ELSE CONVERT(VARCHAR(15), W.DEBRIEFDATE, YOUR_PREFERRED_DATE_FORMAT) END as DEBRIEFDATE,
J.CLOSEDDATE AS CLOSEDDATE,
INITCAP(HR.COUNTRY_NAME) as COUNTRY,
F.NAME AS NAME,
SY.MODALITY AS MODALITY,
HR.MANAGER_SSOID AS MANAGER_SSOID,
HR.MANAGER_NAME AS MANAGER_NAME + ''')'
EXEC sp_executesql #ORACLE_SQL
Points to remember:
Everytime you filter something based on a value, it should always be inside double quotes ('').
SQL dates and ORACLE date formats are different. You have to convert it to varchar first.
Always be careful for the quotes that you are adding. Even one single quote can mess the entire OPENQUERY query.
The CONVERT() above has YOUR_PREFERRED_DATE_FORMAT which means you have to set the preferred date format according to your requirement. Here is the complete reference just in case if you get stuck: https://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/
Hope this helps.
I am trying to UNION ALL around 20 or so tables to consolidate into a single view. I keep getting an error that states:
'The numeric value XXXXX is not recognized'.
This error is contained in 1 column in each of the tables, but the data type for that column is VARCHAR(256) in each of the tables. No matter what I cast the column to I still get the same error.
The UNION ALL works perfectly if I comment that column out.
I've tried casting all columns to the same datatype, no luck.
I've tried commenting out the column in question, which works, but I need that column.
I've tried only UNION-ing a few of the tables, which sometimes works and sometimes doesn't, depending on the document type.
SELECT
CAST(QUICKBOOKS_MEXICO.BILL_LINE.DESCRIPTION AS VARCHAR(256)) AS DESCRIPTION
FROM QUICKBOOKS_MEXICO.BILL_LINE
UNION ALL
SELECT
CAST(QUICKBOOKS_EUROPE_BV.PURCHASE_LINE.DESCRIPTION AS VARCHAR(256)) AS DESCRIPTION
FROM QUICKBOOKS_EUROPE_BV.PURCHASE_LINE
The columns should seamlessly UNION.
Here is the error message:
Numeric value 'Exchange Gain Or Loss' is not recognized
It's worth mentioning that if I remove all the other fields BESIDES the column that is throwing the error from the query, it performs just fine. Truly baffling!
It seems to me that the error message that you get is not related to this column, but with another column where you might be casting the data as NUMERIC, but instead getting this value 'Exchange Gain Or Loss'.
One way to ignore this conversion error, is to use TRY_CAST instead of CAST, so when a value can not be converted to the intended data type, it will simply return NULL
I have looked and looked and can not find an answer anywhere so I am hoping you guys can help me. I am pulling data from multiple tables using a left join. Some info is not in the right table so it pulls across as NULL. The end user is requesting that all NULL values show as blank. I can easily do this for columns with string datatypes but I have not been able to figure out a way to do this for columns with numeric, int, money, or float datatypes.
See screenshot for simplistic example:
Using SQL Sever 2012
Probably not the answer you want to hear but you might need to convert the numeric columns in your result set to a string type (nvarchar, varchar...etc) and then put a CASE WHEN structure in your SELECT around each one of those columns and check if the value of the field is NULL then print '' the blank string.
Try this;
select
NumberNull=case when ISNUMERIC(YourField)=1 then cast(YourField AS varchar(100)) else '' end
I have a query that's written dynamically (OO PHP via Joomla) to insert some values into a MySQL database. The form that a user fills out has a field on it for dollar amount, and if they leave that blank I want the value going into the system to be NULL. I've written out the query to the error log as it's running; this is what the query looks like:
INSERT INTO arrc_Voucher
(VoucherNbr,securityCode,sequentialNumber, TypeFlag, CreateDT, ActivatedDT, BalanceInit, BalanceCurrent, clientName)
VALUES
('6032100199108006', '99108006','12','V','2010-10-29 12:50:01','NULL','NULL','NULL','')
When I look in the database table, though, although ActivatedDT is set correctly to NULL, BalanceInit and BalanceCurrent are both 0.00. The ActivatedDT field is a datetime, while the other two are decimal(18,2), and all three are set in the table structure as default value NULL.
If I run a query like this:
UPDATE arrc_Voucher
SET BalanceInit = null
WHERE BalanceInit like "0%"
...it does set the value to null, so why isn't the initial insert query doing so? Is it because null is in quotes? And if so, why is it setting correctly for ActivatedDT?
remove the quotes around NULL. What's actually happening is it's trying to insert the string 'NULL' as a number, and since it can't be converted to a number it uses the default value 0.
As for why ActivatedDT works, I'm guessing that's a date field. Failure to convert a string into a date would normally result in setting the value to 0 (which gets formatted as something like '1969-12-31'), but if you have NO_ZERO_DATE mode enabled, then it would be set to NULL instead.
If you'd like MySQL to throw an error in cases like this, when invalid values are passed, you can set STRICT_ALL_TABLES or STRICT_TRANS_TABLES (make sure you read the part about the difference between them) or one of the emulation modes, like TRADITIONAL.
You can try this with the command SET sql_mode='TRADITIONAL', or by adding sql-mode="TRADITIONAL" in my.cnf.
When you insert NULL into a MySQL database, you cannot insert it with quotes around it. It tries to insert the varchar 'NULL'. If your idea worked, you would never be able to insert the actual word NULL into the DB.
Remove the single quotes when you want to insert NULL.
You are not setting the fields to NULL but to strings ('NULL').
The question
Is it possible to ask SSIS to cast a value and return NULL in case the cast is not allowed instead of throwing an error ?
My environment
I'm using Visual Studio 2005 and Sql Server 2005 on Windows Server 2003.
The general context
Just in case you're curious, here is my use case. I have to store data coming from somewhere in a generic table (key/value structure with history) witch contains some sort of value that can be strings, numbers or dates. The structure is something like this :
table Values {
Id int,
Date datetime, -- for history
Key nvarchar(50) not null,
Value nvarchar(50),
DateValue datetime,
NumberValue numeric(19,9)
}
I want to put the raw value in the Value column and try to put the same value
in the DateValue column when i'm able to cast it to Datetime
in the NumberValue column when i'm able to cast it to a number
Those two typed columns would make all sort of aggregation and manipulation much easier and faster later.
That's it, now you know why i'm asking this strange question.
============
Thanks in advance for your help.
You could also try a Derived Column component and test the value of the potential date/number field or simply cast it and redirect any errors as being the NULL values for these two fields.
(1) If you just simply cast the field every time with a statement like this in the Derived Column component: (DT_DATE)[MYPOTENTIALDATE] - you can redirect the rows that fail this cast and manipulate the data from there.
OR
(2) You can do something like this in the Derived Column component: ISNULL([MYPOTENTIALDATE]) ? '2099-01-01' : (DT_DATE)[MYPOTENTIALDATE]. I generally send through '2099-01-01' when a date is NULL rather than messing with NULL (works better with Cubes, etc).
Of course (2) won't work if the [MYPOTENTIALDATE] field comes through as other things other than a DATETIME or NULL, i.e., sometimes it is a word like "hello".
Those are the options I would explore, good luck!
In dealing with this same sort of thing I found the error handling in SSIS was not specific enough. My approach has been to actually create an errors table, and query a source table where the data is stored as varchar, and log errors to the error table with something like the below. I have one of the below statements for each column, because it was important for me to know which column failed. Then after I log all errors, I do a INSERT where I select those records in SomeInfo that do not have an errors. In your case you could do more advanced things based on the ColumnName in the errors table to insert default values.
INSERT INTO SomeInfoErrors
([SomeInfoId]
,[ColumnName]
,[Message]
,FailedValue)
SELECT
SomeInfoId,
'PeriodStartDate',
'PeriodStartDate must be in the format MM/DD/YYYY',
PeriodStartDate
FROM
SomeInfo
WHERE
ISDATE(PeriodStartDate) = 0 AND [PeriodStartDate] IS NOT NULL;
Tru using a conditional split and have the records where the data is a date go along one path and the other go along a different path where they are updated to nullbefore being inserted.