HelloWorld1
HelloWorld1

Reputation: 14108

Convert varchar into date without day

Goal:
Gain datatype date with year and month

Problem:
Need to help to convert varchar value into datatype date

Unfortunatley, no day is included in the data from column. Year and month only.

I tried converting into date without using day but I failed.

Varchar(10) column data

data    
-------    
1997-05   
1984-12   
1988-11    
1984-10    
1984-02  
1984-01    
1984-04

Upvotes: 1

Views: 6817

Answers (2)

Aaron Bertrand
Aaron Bertrand

Reputation: 280431

Assuming returning the first day of the given month is ok, you can use:

SELECT CONVERT(DATE, data + '-01') FROM dbo.table;

As others have suggested, you can't have a date without a day.

Upvotes: 4

Bort
Bort

Reputation: 7638

If you don't care about the day, you can add an arbitrary day to the data:

CAST(data + '-01' as date)

Upvotes: 2

Related Questions