user1449596
user1449596

Reputation: 309

how to convert a date column with varchar datatype to another date format

i have a date column with datatype 'nvarchar(50)' and it is displayed as '01/08/2012', '02/08/2012', etc. in the original table

But I want it to be displayed as '20120801', '20120802' in a query or stored procedure and parameter in stored procedure should also be able to put as '20120801', '20120802'

I tried the code below but it doesn't seem to work.

Create procedure testprocedure 
@startdate nvarchar(8), @enddate nvarchar(8)
As
Begin
select convert(varchar(8),date,112) AS DATE, field1, field2 from tbltable
where date between @startdate and @enddate
End
Go

execute testprocedure @startdate = '20120801', @enddate = '20120805'

Upvotes: 0

Views: 1210

Answers (1)

Habib Zare
Habib Zare

Reputation: 1204

the problem is : date is nvarchar you should convert it to Date then convert it to other format.

select convert(varchar(8),CAST(tdate as Date),112) AS DATE, field1, field2 from tbltable

tdate is tbltable.tdate.

it works.

Upvotes: 1

Related Questions