SQL Server 2017 Convert Varchar to Date - sql

I have a date stored as nvarchar which I cannot seem to convert to a date.
I have tried the following but get the error message
Conversion failed when converting date and/or time from character string
select cast([month-year] as date) from [Reporting].[AIM].[Hires_PBI]
select convert(date, [month-year], 103)from [Reporting].[AIM].[Hires_PBI]
Any ideas here?

Find the values that are causing the problem using try_convert():
select [month-year]
from [Reporting].[AIM].[Hires_PBI]
where try_convert(date, [month-year], 103) is null and
[month-year] is not null;
Once you see what values are causing the problem, you can adjust the conversion logic to convert all values. Or just use try_convert() to get NULL for the non-convertible values.

Related

Conversion from INT to varchar in sql

I have a table where there are values like 20170730 and also 0 values are there which is INT type
I am trying to convert it to value like 30/07/2017, for which i am using the below code,
Select convert(NVARCHAR(10),convert(date,convert(NCHAR(8),datecolumn)),103) from table
But for the zero values i am getting the below error
Conversion failed when converting date and/or time from character string.
If i delete all the zero this working fine but problem having with zero.
My requirement is to convert when there a date value and if 0 are there then it should be zero only like below,
Result
30/07/2017
0
Can u pls help
As already pointed out in the comments, you can try to use a CASE expression
SELECT CASE
WHEN nmuloc = 0 THEN
'0'
ELSE
convert(varchar(10),
convert(date,
convert(varchar(8),
nmuloc),
112),
103)
END
FROM elbat;
or try_convert() and coalesce().
SELECT coalesce(convert(varchar(10),
try_convert(date,
convert(varchar(8),
nmuloc),
112),
103),
'0')
FROM elbat;
db<>fiddle
The latter one will also correct other "malformed" data like 123 for example. The former will also fail in such cases. You may want that or not.
But, as also already pointed out in the comments, your real problem is that you use an inappropriate data type. Change the column's datatype to some date/time data type to really fix this.

Convert from varchar(50) to date

I have imported csv file to sql server manager. one of column is date but his data type is vachar(50).
I used the basic (cast, convert, try_parse) but still get message:
(Conversion failed when converting date and/or time from character
string.)
could you please help me :)
select cast(
replace(
case
when [factuur_datum] in ('null','')
then null
else [factuur_datum]
end, '-','') as date) as [factuur_datum]
from [dbo].[verkoop]
Please use below query to convert it into date
convert(date,'20-12-2018',105)
This should do what you intend:
select try_cast(replace(factuur_datum, '-', '') as date) as factuur_datum
from [dbo].[verkoop]
Non-dates will result in NULL.
I take it factuur_datum is the date column:
select
cast(factuur_datum as date) as [Alias]
from [dbo].[verkoop]
Not sure what the data looks like so can you include an example?
EDIT
convert(datetime,'factuur_datum',1)
possibly will error out if there are null values so you may need to case out the nulls then convert.

Conversion failed when converting date and/or time from character string when select date

I want date to be displayed as yyyy-mm-dd. The value in the table for date is dd/mm/yyyy. I have run the query
select CONVERT(DATE, FORMDATE, 103) from UPDATELOG
and i got error "Conversion failed when converting date and/or time from character string". Anyone can help me with this?
This certainly looks like SQL Server. So, use try_convert() instead:
select TRY_CONVERT(DATE, FORMDATE, 103)
from UPDATELOG ;
To get the values that fail, use:
select FORMDATE
from UPDATELOG
where TRY_CONVERT(DATE, FORMDATE, 103) is null

Convert VARCHAR to DATE in SQL SERVER

I have VARCHAR column (MyValue) in my table. It has date value in two different format.
MyValue
----------
25-10-2016
2016-10-13
I would like to show them in DATE format.
I wrote query like below:
SELECT CONVERT(date, MyValue, 105) FROM MyTable
SELECT CAST(MyValue as date) FROM MyTable
Both are giving me this error. Conversion failed when converting date and/or time from character string.
Is there anyway convert to DATE datatype format even the value stored in different formats like above?
Expecting your answers. Thanks in advance.
Does this help?
declare #varchardates table
(
vcdate varchar(20)
)
INSERT INTO #varchardates VALUES
('25-10-2016'),
('2016-10-13')
SELECT CONVERT(date,vcdate, case when SUBSTRING(vcdate, 3, 1) = '-'
THEN 105 ELSE 126 END) as mydate
FROM #varchardates
Depending on how many different formats you have in your data, you may need to extend the case statement!
See here for list of the different format numbers
You can use TRY_CONVERT and COALESCE. TRY_CONVERT returns NULL if the conversion fails, COALESCE returns the first NOT NULL value:
SELECT COALESCE(TRY_CONVERT(DATETIME, x, 105), TRY_CONVERT(DATETIME, x, 120))
FROM (VALUES('25-10-2016'), ('2016-10-13')) a(x)
I assumed the value 2016-10-13 is in format yyyy-MM-dd.
You mention in a comment you may have other formats as well. In that case it gets very tricky. If you get a value 01-12-2017 and you have no idea about the format, there is no way to tell whether this is a date in januari or in december.

Convert a date in nvarchar to date format in a table on the SQL Server

Here is what I have done:
select top 1000 convert(nvarchar(50), cast([date written] as datetime), 1)
from practicetable
where convert(nvarchar(50), cast([date written] as datetime), 1) = '09/07/16'
Although this query works on a small scale anytime I try to select more than 1000 records the query fails and I get this error:
Conversion failed when converting datetime from character string
Why can't I select more than 1,000 records?
Always risky to save dates as a string. Since you don't have Try_Convert() in 2008, I would recommend that you
select distinct [date written] from practicetable order by 1
to see which dates are not formatted correctly, and scrub as necessary
The query is failing because it is trying to convert a string to a date, but the string does not fit the date format. In other words, the 'Conversion failed when converting datetime from character string'. You should take a closer look at the underlying data to determine which record is causing the issue.