I'm trying to run a query that includes only the past 90 days worth of data, but the query is returning the error "Data value...has invalid format".
The query is:
select name, diff
from
(select name, (getdate()::date - to_date(split_part(name, ' ', 1),'MM/DD/YYYY')::date) as diff
from sample_table)
where diff<=90;
The strange part is that the operation getdate()::date - to_date(split_part(name, ' ', 1),'MM/DD/YYYY')::date works as expected, but when restricted to <=90, it returns this error.
Any help is appreciated!
Find the bad row using the following code:
select split_part(name,' ',1) original_value, name
from sample_table
where split_part(name,'1') !~ '^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$'
This uses regex to determine if there's a valid date in that part of the field-- if not, it returns the field so you may find it.
Related
I'm getting an error when I run a query due to a couple of lines of my source data having decimals when they should not. I need to pinpoint these specific rows so I can correct them in the source data but I do not know how to find them. Is there an easy way to pinpoint the rows that are causing the error?
It appears your data is improperly typed, you have a text or varchar data type for what should be numeric class type. If so that needs to be corrected, but yes you must fix the data first. You can to isolate and fix the incorrect rows by selecting those which fail a regexp that defines a valid value. Since you did not post sample data nor table descriptions only provide an example that I make up, not your values.
Example:
- For just digits only: column_name !~ '^[0-9]+$'
- For Positive or Negative Integer: column_name !~ '^(+|-)?[0-9]+$'
with test (id,num_val) as
( values (1,'abc'),(2,'18'),(3,'1.7'),(4,'-12') )
select id, num_val, 'Fails Just Digits'
from test
where num_val !~ '^[0-9]+$'
union
select id, num_val, 'Fails Pos/Neg Integer'
from test
where num_val !~ '^(\+|-)?[0-9]+$'
order by 1,3;
fails both
passes both (so not in output)
fails both
fails Just digits
I would like to understand question about conversion:
exam dump I'm working with has this question at least three times with 3 different solutions you approve or don't approve of note that RegistrationNumber is defined as varchar(5) :
You run the following query:
SELECT UserId FROM tblVehicleRegistration
WHERE RegistrationNumber = 20012
AND RegistrationDate > '2016-01-01'
The query output window displays the following error message: “Conversion failed when converting the varchar value ‘AB012’ to data type int.”
You need to resolve the error.
Solution: You modify the Transact-SQL statement as follows:
SELECT UserId FROM tblVehicleRegistration
WHERE RegistrationNumber = '20012'
AND RegistrationDate > '2016-01-01'
answer says this does not work
I would think the test is incorrect. Here is a simplified example:
declare #tblVehicleRegistration table (RegistrationNumber varchar(5))
insert into #tblVehicleRegistration(RegistrationNumber) VALUES('AB012')
SELECT * FROM #tblVehicleRegistration WHERE RegistrationNumber = 20012 --Fails as expected
SELECT * FROM #tblVehicleRegistration WHERE RegistrationNumber = '20012' --works as expected
SQL Server will do a convertion in order to compare 'AB012' and 20012. If you check this link Data type precedence you will see that type varchar, wich is low precedence, needs to be converted to int, wich is high precedence, in order to make a comparison.
I created a table and tried hands-on. It worked properly after casting or changing the integer value to a string with quotation marks.
I'm trying to pull data for certain dates out of a staging table where the offshore developers imported everything in the file, so I need to filter out the "non-data" rows and convert the remaining strings to datetime.
Which should be simple enough but... I keep getting this error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
I've taken the query and pulled it apart, made sure there are no invalid strings left and even tried a few different configurations of the query. Here's what I've got now:
SELECT *
FROM
(
select cdt = CAST(cmplt_date as DateTime), *
from stage_hist
WHERE cmplt_date NOT LIKE '(%'
AND ltrim(rtrim(cmplt_date)) NOT LIKE ''
AND cmplt_date NOT LIKE '--%'
) f
WHERE f.cdt BETWEEN '2017-09-01' AND '2017-10-01'
To make sure the conversion is working at least, I can run the inner query and the cast actually works for all rows. I get a valid data set for the rows and no errors, so the actual cast is working.
The BETWEEN statement must be throwing the error then, right? But I've casted both strings I use for that successfully, and even taken a value out of the table and did a test query using it which also works succesfully:
select 1
WHERE CAST(' 2017-09-26' as DateTime) BETWEEN '2017-09-01' AND '2017-10-01'
So if all the casts work individually, how come I'm getting an out-of-range error when running the real query?
I am guessing that this is due to the fact that in your cmplt_date field there are values which are not valid dates. Yes, I know you are filtering them using a WHERE clause, but know that Logical Processing Order of the SELECT statement is not always the actual order. What does this mean is that sometimes, the SQL Engine my start performing your CAST operation before finishing the filtering.
You are using SQL Server 2012, so you can just add TRY_CAST:
SELECT *
FROM
(
select cdt = TRY_CAST(cmplt_date as DateTime), *
from stage_hist
WHERE cmplt_date NOT LIKE '(%'
AND ltrim(rtrim(cmplt_date)) NOT LIKE ''
AND cmplt_date NOT LIKE '--%'
) f
WHERE f.cdt BETWEEN '2017-09-01' AND '2017-10-01'
--DB2 version 10 on AIX
I have a stored procedure, which I need to update. And want to check if there is data for a certain date. If data exists, go on, else run insert and then go on.
IF (SELECT COUNT(*)
FROM SCHEMA1.TABLE1_STEP1
WHERE XDATE = '9/27/2014' < 1)
THEN (INSERT INTO SCHEMA1.TABLE1_STEP1 (SELECT * FROM SCHEMA2.TABLE2 FETCH FIRST 2 ROWS ONLY))
END IF;
This errors-out.
DB2 Database Error: ERROR [42601] [IBM][DB2/AIX64] SQL0104N An unexpected token "(" was found following "/2014') < 1) THEN". Expected tokens may include: "". SQLSTATE=42601
Any thoughts on what's wrong?
I'm guessing you probably want the less than sign outside of the parenthesis...
However, as an aside, you can also do this kind of statement without an IF (although, I don't have an AIX DB2 available to check for sure. It worked on DB2 for z/OS and LUW, however):
INSERT INTO SCHEMA1.TABLE1_STEP1
SELECT *
FROM SCHEMA2.TABLE2
WHERE NOT EXISTS (
SELECT *
FROM SCHEMA1.TABLE1_STEP1
WHERE XDATE = '9/27/2014'
)
FETCH FIRST 2 ROWS ONLY
Also, you're not providing an ORDER BY on the SCHEMA2.TABLE2 select, so your results could come back in any order (whatever is "easiest" for the database engine)... order is not guaranteed unless you provide the ORDER BY statement.
I'm writing an SQL expression and I'd like to use the current month as the column name/header.
Code:
Select MONTH(GETDATE()) AS MONTH(GETDATE())
FROM SomeTable;
Error:
Error 102: Incorrect syntax near 'GETDATE'.
This is for a school project and I'm not sure if it's possible. If it is, I'd like to possibly convert that Month number to the actual month name. Thanks in advance.
Oh, and I'm using LinqPad to test the queries on a remote DB and SQL Express Server (Transact-SQL).
Cheers,
Lindsay
I think, You can not use function in column alias, if you try to then you get this error incorrect syntex "Expecting ID, QUOTED_ID, STRING, or TEXT_LEX" which means the alias text has to be hard coded.
I would suggest, you use your front end application to set current month as header, instead of relying on back end sql query.
The alias for your computed columns shouldn't contain any function - just text:
SELECT
MONTH(GETDATE()) AS 'Month'
FROM
dbo.SomeTable