Author |
Topic |
rookie_sql
Constraint Violating Yak Guru
443 Posts |
Posted - 2006-05-15 : 07:45:16
|
Hi i've a date column dd/mm/yy, i want to display the month name (May) from the given date column in a month column.Example Date = 15/05/2006 Month = MayAm i on the right track ?update tbl_Voy_RCATransferset [Month] = (SELECT DATENAME(month, [Date])from tbl_Voy_RCATransfer) |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-15 : 07:52:11
|
[code]update tbl_Voy_RCATransferset [Month] = DATENAME(month, [Date])[/code] KH |
|
|
rookie_sql
Constraint Violating Yak Guru
443 Posts |
Posted - 2006-05-15 : 07:57:42
|
Am getting this error now .Syntax error converting datetime from character string. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-15 : 08:08:37
|
the column [Date] is a varchar ? KH |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-05-15 : 08:10:35
|
Is your Datecol DateTime datatype?Dont update. Just use selectselect DATENAME(month, [Datecol]) from yourTableMadhivananFailing to plan is Planning to fail |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-15 : 08:11:15
|
If the column [Date] is a string (char or varchar) then you have to convert to datetime first.update tbl_Voy_RCATransferset [Month] = DATENAME(month, convert(datetime, [Date], 103)) KH |
|
|
rookie_sql
Constraint Violating Yak Guru
443 Posts |
Posted - 2006-05-15 : 08:11:44
|
Nope i check it, its a datetime. i am using a function to convert it. here is the function it may have something got to do with it, as it set them to char(2) . CREATE FUNCTION dbo.udf_date_only(@date_in datetime)RETURNS datetimeAS BEGINRETURN CONVERT(datetime, CAST(DAY(@date_in) AS char(2)) + '/' + CAST(MONTH(@date_in) AS char(2)) + '/' + CAST(YEAR(@date_in) AS char(4)), 103)END |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-15 : 08:14:01
|
your function can be simplified toCREATE FUNCTION dbo.udf_date_only(@date_in datetime)RETURNS datetimeASBEGIN RETURN dateadd(day, datediff(day, 0, @date_in), 0)END KH |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-15 : 08:24:48
|
" Nope i check it, its a datetime"If the column is of datetime data type, then it should not gives you any error with DATENAME(month, [Date]) at all.What is the data type for [Month] ? KH |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-05-15 : 08:30:07
|
Easier to use this:CREATE FUNCTION dbo.udf_date_only(@date_in datetime)RETURNS datetimeASBEGIN RETURN CONVERT(VARCHAR, @date_in_datetime, 112)ENDBut I think that column Month does not hold enough space (characters) for example 'Septemberä which is 9 characters long... |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-05-15 : 08:31:31
|
update tbl_Voy_RCATransferset [Month] = DATENAME(month, [Date])Please make sure that [Month] column is at least 9 characters wide. |
|
|
rookie_sql
Constraint Violating Yak Guru
443 Posts |
Posted - 2006-05-15 : 09:06:01
|
quote: Originally posted by khtan " Nope i check it, its a datetime"If the column is of datetime data type, then it should not gives you any error with DATENAME(month, [Date]) at all.What is the data type for [Month] ? KH
The porblem was with the month i had it set to datetime, and i;'ve now changed it to varchar(10) |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-15 : 09:14:40
|
"The porblem was with the month i had it set to datetime, and i;'ve now changed it to varchar(10) "That sure solved the mystery. KH |
|
|
rookie_sql
Constraint Violating Yak Guru
443 Posts |
Posted - 2006-05-15 : 09:15:34
|
Thanks for all your help on this matter. |
|
|
|