sql - Conversion failed when converting the varchar value 'Q4' to data type int -


create table persons (     pre_dtr nvarchar(20),    tr_pre_dtr nvarchar(20) );    insert persons(pre_dtr, tr_pre_dtr)  values ('10.12.2014', '');  insert persons(pre_dtr, tr_pre_dtr)  values ('10.12.2014', '');   select    pre_dtr,    case       when datepart(month, pre_dtr) in (10, 11, 12)          convert(nvarchar(20), datepart(month, pre_dtr) + 'q4')     end tr_pre_dtr    persons 

when run query 'm getting error:

conversion failed when converting varchar value 'q4' data type int.

how can fix it?

thanks

the operator + overloaded. when argument numeric, means addition. when arguments strings, means string concatenation.

datepart() returns number, have convert character value. personally, prefer function month() on datepart(), so:

select pre_dtr,        (case when month(pre_dtr) in (10, 11, 12)               convert(nvarchar(20), month(pre_dtr)) + 'q4')         end) tr_pre_dtr persons; 

your query close. doing + before convert(). hence error, because month still number.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -