{"id":359,"date":"2010-09-03T08:24:34","date_gmt":"2010-09-03T12:24:34","guid":{"rendered":"http:\/\/erics-test-lab.com\/?p=359"},"modified":"2010-09-03T08:24:34","modified_gmt":"2010-09-03T12:24:34","slug":"sql-adding-new-columns-to-query","status":"publish","type":"post","link":"https:\/\/www.esearing.com\/v2010\/2010\/09\/sql-adding-new-columns-to-query\/","title":{"rendered":"SQL adding new columns to query"},"content":{"rendered":"<p>Sometimes a table has all the fields you need and sometimes it doesn&#8217;t. I sometimes need to create flag fields or summary fields to hold additional information. So here are the methods I use.<\/p>\n<p>Add a 1 char flag field<br \/>\n<code>select *, ' ' as myflag from table...<\/code><br \/>\n<!--more--><br \/>\nAdd a column with more than one character or a data type<br \/>\n<code>select *, cast('' as varchar(10)) as mytextfield, cast('' as datetime) as mydatefield from table...<\/code><\/p>\n<p>Add several columns using alter<br \/>\n<code>Select * into #mytemptable from table...<br \/>\nAlter table #mytemptable<br \/>\nAdd ( col1 varchar(10) null,<br \/>\ncol2 datetime null,<br \/>\ncol3 int null,<br \/>\ncol4 char(1) null<br \/>\n)<\/code><\/p>\n<p>Summary field<br \/>\n<code>select units, price, unitsxprice as  extended price from table...<\/code><\/p>\n<p>Concatenated field<br \/>\n<code>select firstname+\" \"+lastname as name from table...<\/code><\/p>\n<p>Syntax may vary slightly depending on database.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes a table has all the fields you need and sometimes it doesn&#8217;t. I sometimes need to create flag fields or summary fields to hold additional information. So here are the methods I use. Add a 1 char flag field &hellip; <a href=\"https:\/\/www.esearing.com\/v2010\/2010\/09\/sql-adding-new-columns-to-query\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,24,14],"tags":[],"class_list":["post-359","post","type-post","status-publish","format-standard","hentry","category-code","category-snippets-quick-code-tips-for-single-elements-attributes-or-details","category-sql"],"_links":{"self":[{"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/posts\/359","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/comments?post=359"}],"version-history":[{"count":0,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/posts\/359\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/media?parent=359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/categories?post=359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/tags?post=359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}