Can't convert column varchar to int

184 Views Asked by At

I have column of varchar data type with values like 5/3/2012. I want to convert into int as 2016-05-03. When I'm trying to do so, it is returning null as output

cast(convert(varchar(10), '5/3/2012', 112) as int) 
3

There are 3 best solutions below

1
On
  SELECT CONVERT(INT,convert(Datetime, '5/3/2012', 101))
--41030
2
On

2016-05-03 is a Date not a Integer value. So you need to convert it to Date

You just want to Convert it to Date with style 101

Select convert(Date, '5/3/2012', 101) --2012-05-03
1
On

But if you truly do need to convert it to an INT, try this -

DECLARE @d DATE = '5/3/2012'
DECLARE @y int = YEAR( @d )
    , @m int = MONTH( @d )
    , @dy int = DAY( @d )

SELECT CAST(concat( @y, format(@m, '00'), format(@dy,'00')) as int)