"Conversion failed when converting" Is there a way to det. what row/ entry? - sql

Is there a way to determine what row/ date entry SQL is having trouble converting ? There are 300k date entries and I'm unsure which one my code is having a problem with. This code has worked in the past.
Conversion failed when converting date and/or time from character
string

You could use TRY_CONVERT to search for invalid date literals:
SELECT *
FROM tab_name
WHERE TRY_CONVERT(DATE, col_name) IS NULL
AND col_name IS NOT NULL;
If necessary you could provide date time style.
db<>fiddle demo

You can check if the column was in a date format, using ISDATE():
select myColumnDate
from myTable
where ISDATE(myColumnDate) = 0

Related

Convert Number type to Date in postgres SQL

I have a numeric data in a column 20170930, need help in converting it into Date in PostgreSQL , tried multiple ways but non seems to work
You can convert to a string and then to a date:
select column::text::date
You can also express this using explicit cast() syntax:
select cast(cast(20170930 as text) as date)
Use one of the following :
SELECT cast(yourcol::varchar as date ) as dt1, yourcol::varchar::date as dt2
where dt1 and dt2 values of type date, and yourcol is a numeric value such as 20170930
Demo
The best thing is to change column datatype into Date type,
ALTER TABLE table_name
ADD column_name Date;
As shown above, PostgreSQL supports a full set of SQL date and time types, as shown in the table below. Dates are counted according to the Gregorian calendar. Here, all the types have a resolution of 1 microsecond / 14 digits except date type, whose resolution is day.
Please try below query
SELECT to_date(column::varchar,'YYYYMMDD')
For anybody who fell into my pitfall I tried this but my numeric was like a 'seconds past 01-01-1970 format' rather than YYYYMMDD
This worked
SELECT to_timestamp(yourcol) as numeric_column_now_date
from yourtable
see here
https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-ZONECONVERT

sql teradata filtering on date - database version Teradata 15.10.06.02 and provider version Teradata.Net 15.11.0.0

my table has a date column. its data type is date. I confirmed it by going to table name>>columns and it says MTH_END_DT [DATE, Not NULL]
I want to filter my data for a particular date. If I put a condition where MTH_END_DT = '6/1/2018' I get an error select failed [3535] A character string failed conversion to a numeric value.
I followed this page. I used where MTH_END_DT = date '6/1/2018' and i get an error syntax error invalid date literal
I tried where cast(timestamp_column as date) = date '2013-10-22'; something like this and it throws error too
How should i filter my data?
There's only one reliable way to write a date, using a date literal, date 'yyyy-mm-dd'
where MTH_END_DT = DATE '2018-06-01'
For a Timestamp it's
TIMESTAMP '2018-06-01 15:34:56'
and for Time
TIME '15:34:56'
In SQL Assistant it's recommended to switch to Standard SQL format YYYY-MM-DD in Tools-Options-Data Format-Display dates in this format
I did have the similar problem when I was filtering a particular date for my query with Teradata. First method I tried was putting 'DATE' term as the following:
WHERE saledate = DATE'04/08/01' but this did not solve the problem.
I then used an approach I stumbled upon when surfing, finally it worked.
WHERE extract(year from saledate)=2004 AND extract(MONTH from saledate)=8 AND extract(DAY from saledate)= 1 source
I think this really should not be this long, but it worked.
It seems to me it’s most likely you have input the date format incorrectly? Maybe it includes a time by default.
For example
where MTH_END_DT = ‘2013-10-22-00:00:00:00’

Convert from varchar into date in SQL Server

This looks easy solution but I can't seem to figure out as to why this is not working for me. I have a column that has data like this:
DateField
----------
12/16/2016
11/06/2016
All I want to do is to convert from varchar into a date column, but I am getting this error:
Conversion failed when converting date and/or time from character string.
Here is my simple query:
select convert (date, DateField) as convertedField
from myTable
Nothing wrong with the two examples you have given. There are some bad dates in your table which cannot be converted to date.
Use TRY_CONVERT function for bad dates it will return NULL
select TRY_Convert(date,DateField)
From myTable
You should always store dates in DATE/DATETIME datatype.
If you want to see the records which cannot be converted to date then
select DateField
From myTable
Where TRY_Convert(date,DateField) IS NULL
If working with a specific date format like mm/dd/yyyy You can specify it in Convert() function like the following
CONVERT(DATETIME,DATAFIELD,101)
If it still is not working, use TRY_CONVERT() to get which rows are throwing this exception:
SELECT *
FROM TBL
WHERE TRY_CONVERT(DATETIME, DATAFIELD, 101) IS NULL
This will return rows that cannot be converted
TRY_CONVERT() will return NULL if conversion failed
Read more about DateTime formats here:
SQL Server CONVERT() Function tutorial
Read TRY_CONVERT MSDN Article
You need to specify the format of date time while formatting. The date in your table is currently in U.S format so you should pass the third argument 101 in your convert function.
SELECT CONVERT(date,[DateField],101) FROM myTable;
Working Fiddle here http://rextester.com/NYKR49788
More info about date time style here: https://msdn.microsoft.com/en-us/library/ms187928.aspx

Converting string to date in sql

I need to convert 2014-11-18T14:08:43+00:00 which is in varchar in my sql developer to date format in order to filter a few entries.
I tried
to_date(LAST_UPDATE_DATE,'YYYY-MM-DD')
but it gives an error
ORA-01830: date format picture ends before converting entire input
string.
Kindly help..
Just in case you didn't mean to put up sql server but instead you need to use oracle (seeing as you are using to_date and you are getting an ora exception)
I added a quick datetime conversion for date and timestamp (no milliseconds) for your date format:
SELECT to_Date(concat
(substr
(myvar,0,10),
concat(' ',
substr(myvar,12,8)
)
),'YYYY-MM-DD HH24:mi:ss') AS mydate
FROM mytable
Fiddle
declare #varDate as nvarchar(50) = '2014-11-18T14:08:43+00:00'
select CAST(substring(#varDate,0,CHARINDEX('T',#varDate)) as date)
Either you can use it like this
declare #Date as nvarchar(100) = '2014-11-18T14:08:43+00:00'
SELECT CONVERT(DATE,#Date) AS Date
OR go for this answer which is accepted in this question
ORA-01830: date format picture ends before converting entire input string / Select sum where date query
ORA-01830: date format picture ends before converting entire input string.
to_date(LAST_UPDATE_DATE,'YYYY-MM-DD')
2014-11-18T14:08:43+00:00 is TIMESTAMP and not DATE.
First of all, you should never ever store DATE/TIMSTAMP as string. It is a database design flaw.
Anyway, you could convert it to TIMESTAMP WITH TIMEZONE.
For example,
SQL> SELECT to_timestamp_tz('2014-11-18T14:08:43+00:00',
2 'YYYY-MM-DD"T"HH24:MI:SS.FFTZH:TZM')
3 AS tm_stamp
4 FROM dual;
TM_STAMP
----------------------------------------------------------------
18-NOV-14 02.08.43.000000000 PM +00:00
SQL>
You could try this;
Select CAST ('2014-11-18T14:08:43+00:00' as date)
The assumption is you are in SQL Server 2012

Convert NVARCHAR to DATETIME and select distinct Year

SELECT DISTINCT YEAR(convert(varchar(max),OrderCreatedDate)) from webshop
The above sql query is producing this error:
Conversion failed when converting date and/or time from character string.
The value in the database is NVARCHAR and in the following format: DD/MM/YYYY 00:00:00 AM (/PM)
I would like to select the unique values for the year only!
thanks for any help
To avoid this sort of issues, date should be saved in DateTime type field Not in a string field.
Year() function parameter is a DateTime type not a String. So you should make sure that the string you are passing is convertible to a DateTime type. In this case you could trim out the time part with Left() function and use 103 style as below.
Fiddle demo:
--Example
declare #val nvarchar(50) = '28/10/2013 11:25:45 AM (/PM)'
select year(convert(date,left(#val,10),103)) myYear
--Applying to your query
SELECT DISTINCT Year(Convert(Date,Left(OrderCreatedDate,10),103)) FROM Webshop
UPDATE:
If you are getting errors, it could be due to your date format. i.e. Format of the string you have saved may not be as you have described (DD/MM/YYYY 00:00:00 AM (/PM)) in the question.
Please check with ISDATE() function before converting to date and identify which records are causing the problem and correct them.
Try this query to get all of them with invalid strings. Following query will return 0 for values with invalid formatting.
SELECT DISTINCT CASE WHEN IsDate(eft(OrderCreatedDate,10))=1 THEN
Year(Convert(Date,Left(OrderCreatedDate,10),103))
ELSE 0 END as myDate,
OrderCreatedDate
FROM Webshop
Or you could get only the records which are causing the problem as;
SELECT OrderCreatedDate
FROM Webshop
WHERE IsDate(Left(OrderCreatedDate,10)) = 0
select distinct year(CONVERT(NVARCHAR(255),CONVERT(SMALLDATETIME, columnName,105)))
Try this
SELECT DISTINCT YEAR(Convert(datetime,OrderCreatedDate,103)) from webshop
DD/MM/YYYY 00:00:00 AM (/PM) is the British format. See Convert