Convert from varchar(50) to date - sql

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.

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.

SQL Server 2017 Convert Varchar to Date

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.

SQL: How To Select Earliest Date

I have a date column & I am simply trying to know the earliest date. I use the command:
select Min(Install_date) From PocketGemsSchema.pocketgemstable2;
This returns 1-Dec-17
But the minimum date from my sample data is actually 1-Nov-17.
Can anyone help please?
Try this:
If your Install_date contain datatype varchar than
SELECT MIN(CAST(Install_date AS DATE))
FROM PocketGemsSchema.pocketgemstable2
SELECT FORMAT(MIN(CAST(Install_dateAS DATE)), 'dd-MMM-yy ')
FROM PocketGemsSchema.pocketgemstable2
If your Install_date contain datatype date or datetime than your query will work
I think its the data type issue , you can try two approach
convert the field to datatime and your query should work
cast it on the run time like below
Mysql
SELECT Min(Str_to_date(Install_date, '%m/%d/%Y'))
FROM pocketgemsschema.pocketgemstable2;
SQL server
SELECT Min(Cast(Install_date as datetime))
FROM pocketgemsschema.pocketgemstable2;
I would change the column to a date or a datetime type and sort out any bugs that arise.

How to convert varchar to datetime in SQL Server

I would like to convert varchar data to datetime format. So, can anyone let me know the way. this is the sample data available in column.
I tried following things
SELECT
CASE WHEN [EntryTime] = ''
THEN NULL
ELSE CONVERT(datetime2, [EntryTime])
END [EntryTime]
FROM
table
I get this result:
But, I don't need additional milliseconds.
So, can anyone let me know how should I convert it to a datetime format?
For all other tries, I am getting error as
Conversion failed when converting date and/or time from character string
So, can anyone please help me? Thanks.
specify the sub-second width as datetime2(7)
select
try_cast(EntryTime as datetime2(7)) by_try_cast
, try_convert(datetime2(7), EntryTime ) by_try_covert
I also suggest you use try_cast() or try_convert() if they are available, if not just remove "try_", The advantage of these is that if a string is an invalid date they return NULL and don't crash the query.
If you really don't want datetime2:
select cast(try_cast(EntryTime as datetime2(7)) as datetime)
see it: http://rextester.com/YWBB31287
LEFT and Concat (SQL2012+)/ or use standard concatenation
then Cast or Convert (for formatting)
declare #mytable as table (
entrytime varchar(100)
)
insert into #mytable
values ('2017-08-31 08:23:23'),
('2017-11-10 08:28:37.75000000'),
('2017-10-30 08:23:37.32000000')
select cast(left(concat(entrytime,'.000'),23) as datetime) from #mytable
Result
2017-08-31 08:23:23.000
2017-11-10 08:28:37.750
2017-10-30 08:23:37.320

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.