-1

Good day!

I have 2 questions how to update a date data type column field using varchar and numeric column field

1.)mydate varchar(8)--> varchar column field

SELECT mydate from mytable

Result: 20141120

my question is how can I update my date column field using my varchar column field using cast or convert

update table2
set date = (select mydate from mytable)

which I get an error!!! and I'm stuck.

2.)mydate numeric(8) --> numeric column field

SELECT mydate from mytable

Result:

20101015

20140910

etc.......

update table2
set date = (select mydate from mytable a, mytable2 b
where a.id=b.id)

my question is how can I update my date column field using my numeric column field using cast or convert

I used different CAST and CONVERT but still I'm getting error!

What is the correct syntax for this?

Thank your for your help!

itajolosa
  • 1
  • 1
  • 3

1 Answers1

1

To convert a string to a date field you will need to use the CONVERT function:

CONVERT(datetime, mydate, 101)

This is expecting a string field, so if your mydate field is really a numeric field then you will need to CAST that to a string, so the CONVERT command will then look like:

CONVERT(datetime, CAST(mydate as VarChar), 101)

The third parameter of the function is determined by the format of the date in the previous parameter, you can find the full list on MSDN at http://msdn.microsoft.com/en-us/library/ms187928.aspx

Mattgb
  • 408
  • 3
  • 9