sql server - Combine multiple CASE statements into one and SELECT INTO temp table -
i have query produces sepearate outputs. need combine select case statements 1 , have outputs inserted temp table
select case when #wcv_3.visit_1 null '' else convert(varchar(10), #wcv_3.visit_1,110) end #wcv_3 select case when #wcv_3.visit_2 null '' else convert(varchar(10), #wcv_3.visit_2,110) end #wcv_3
etc...
select case when #wcv_3.visit_5 null '' else convert(varchar(10), #wcv_3.visit_5, 110) end #wcv_3
i've tried
select * #wcv_4 (this new temp table) ( select case when #wcv_3.visit_1 null '' else convert(varchar(10), #wcv_3.visit_1,110) end #wcv_3 etc... end)
but 2 errors occur:
"incorrect syntax near keyword 'select'"
and
"incorrect syntax near ')'"
`
your query should :
select somecolumn #wcv_4 ( select case when #wcv_3.visit_1 null '' else convert(varchar(10), #wcv_3.visit_1,110) end somecolumn #wcv_3 union select case when #wcv_3.visit_2 null '' else convert(varchar(10), #wcv_3.visit_2,110) end somecolumn #wcv_3 union select case when #wcv_3.visit_4 null '' else convert(varchar(10), #wcv_3.visit_4, 110) end somecolumn #wcv_3 union select case when #wcv_3.visit_5 null '' else convert(varchar(10), #wcv_3.visit_5, 110) end somecolumn #wcv_3 ) temp
combines results of 2 or more queries single result set includes rows belong queries in union.
some more info can find here http://msdn.microsoft.com/en-us/library/ms180026.aspx
i have tested query (without #wcv_3
table) , works:
select somecolumn #wcv_4 ( select case when 'a' null '' else convert(varchar(10), 'a',110) end somecolumn union select case when 'a' null '' else convert(varchar(10), 'a',110) end somecolumn union select case when 'a' null '' else convert(varchar(10), 'a', 110) end somecolumn union select case when 'a' null '' else convert(varchar(10), 'a', 110) end somecolumn ) temp
Comments
Post a Comment