{"id":17215,"date":"2022-10-23T12:23:48","date_gmt":"2022-10-23T06:53:48","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-if-col_lengthemp_numemployee-is-not-null-equivalent-in-oracle-closed\/"},"modified":"2022-10-23T12:23:48","modified_gmt":"2022-10-23T06:53:48","slug":"solved-if-col_lengthemp_numemployee-is-not-null-equivalent-in-oracle-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-if-col_lengthemp_numemployee-is-not-null-equivalent-in-oracle-closed\/","title":{"rendered":"[Solved] IF COL_LENGTH(&#8216;EMP_NUM&#8217;,&#8217;EMPLOYEE&#8217;) IS NOT NULL &#8211; EQUIVALENT IN ORACLE [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-67077962\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"67077962\" data-parentid=\"67077322\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>In Oracle, you have to use dynamic SQL as you can&#8217;t execute DDL in PL\/SQL <em>as is<\/em>. Why PL\/SQL? Because <code>IF-THEN-ELSE<\/code> <em>is<\/em> PL\/SQL, not SQL.<\/p>\n<p>Presuming this is part of your PL\/SQL procedure, you&#8217;d then<\/p>\n<pre><code>if col_length('EMP_NUM', 'EMPLOYEE') is not null then\n  execute immediate 'alter table employee modify emp_num not null';\nend if;\n<\/code><\/pre>\n<hr>\n<p>That&#8217;s for the <code>ALTER<\/code> part. However, as Oracle doesn&#8217;t have COL_LENGTH function (nor anything like that, as far as I can tell), you have to do it yourself. Here&#8217;s  an example:<\/p>\n<p>A table whose column allows nulls:<\/p>\n<pre><code>SQL&gt; create table employee (emp_num number);\n\nTable created.\n<\/code><\/pre>\n<p>Check the <code>Null?<\/code> column &#8211; empty, so yes &#8211; it allows nulls.<\/p>\n<pre><code>SQL&gt; desc employee;\n Name                                      Null?    Type\n ----------------------------------------- -------- ----------------------------\n EMP_NUM                                            NUMBER\n<\/code><\/pre>\n<p>Query data dictionary to find column&#8217;s length:<\/p>\n<pre><code>SQL&gt; select data_length from user_tab_columns\n  2  where table_name=\"EMPLOYEE\"\n  3    and column_name=\"EMP_NUM\";\n\nDATA_LENGTH\n-----------\n         22\n<\/code><\/pre>\n<p>Finally, your future PL\/SQL procedure:<\/p>\n<pre><code>SQL&gt; declare\n  2    l_len number;\n  3  begin\n  4    select data_length\n  5      into l_len\n  6      from user_tab_columns\n  7      where table_name=\"EMPLOYEE\"\n  8        and column_name=\"EMP_NUM\";\n  9\n 10    if l_len is not null then\n 11       execute immediate 'alter table employee modify emp_num not null';\n 12    end if;\n 13  end;\n 14  \/\n\nPL\/SQL procedure successfully completed.\n\nSQL&gt; desc employee;\n Name                                      Null?    Type\n ----------------------------------------- -------- ----------------------------\n EMP_NUM                                   NOT NULL NUMBER\n\nSQL&gt;\n<\/code><\/pre>\n<p>As you can see, the <code>Null?<\/code> column now contains <code>NOT NULL<\/code> which means that <code>ALTER TABLE<\/code> was successfully executed.<\/p>\n<hr>\n<p>Additionally, you can even create your own function to query column&#8217;s data length and then use it in your procedure:<\/p>\n<pre><code>SQL&gt; create or replace function col_length(par_table in varchar2, par_column in varchar2)\n  2    return number\n  3  is\n  4    retval user_tab_columns.data_length%type;\n  5  begin\n  6    select data_length\n  7      into retval\n  8      from user_tab_columns\n  9      where table_name = par_table\n 10        and column_name = par_column;\n 11    return retval;\n 12  end;\n 13  \/\n\nFunction created.\n\nSQL&gt; select col_length('EMPLOYEE', 'EMP_NUM') from dual;\n\nCOL_LENGTH('EMPLOYEE','EMP_NUM')\n--------------------------------\n                              22\n\nSQL&gt;\n<\/code><\/pre>\n<p>Finally:<\/p>\n<pre><code>SQL&gt; drop table employee;\n\nTable dropped.\n\nSQL&gt; create table employee (emp_num number);\n\nTable created.\n\nSQL&gt; begin\n  2    if col_length('EMPLOYEE', 'EMP_NUM') is not null then\n  3       execute immediate 'alter table employee modify emp_num not null';\n  4    end if;\n  5  end;\n  6  \/\n\nPL\/SQL procedure successfully completed.\n\nSQL&gt; desc employee;\n Name                                      Null?    Type\n ----------------------------------------- -------- ----------------------------\n EMP_NUM                                   NOT NULL NUMBER\n\nSQL&gt;\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">0<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved IF COL_LENGTH(&#8216;EMP_NUM&#8217;,&#8217;EMPLOYEE&#8217;) IS NOT NULL &#8211; EQUIVALENT IN ORACLE [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] In Oracle, you have to use dynamic SQL as you can&#8217;t execute DDL in PL\/SQL as is. Why PL\/SQL? Because IF-THEN-ELSE is PL\/SQL, not SQL. Presuming this is part of your PL\/SQL procedure, you&#8217;d then if col_length(&#8216;EMP_NUM&#8217;, &#8216;EMPLOYEE&#8217;) is not null then execute immediate &#8216;alter table employee modify emp_num not null&#8217;; end if; That&#8217;s &#8230; <a title=\"[Solved] IF COL_LENGTH(&#8216;EMP_NUM&#8217;,&#8217;EMPLOYEE&#8217;) IS NOT NULL &#8211; EQUIVALENT IN ORACLE [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-if-col_lengthemp_numemployee-is-not-null-equivalent-in-oracle-closed\/\" aria-label=\"More on [Solved] IF COL_LENGTH(&#8216;EMP_NUM&#8217;,&#8217;EMPLOYEE&#8217;) IS NOT NULL &#8211; EQUIVALENT IN ORACLE [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[838,1244,500,1009],"class_list":["post-17215","post","type-post","status-publish","format-standard","hentry","category-solved","tag-oracle","tag-oracle11g","tag-sql-server","tag-tsql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] IF COL_LENGTH(&#039;EMP_NUM&#039;,&#039;EMPLOYEE&#039;) IS NOT NULL - EQUIVALENT IN ORACLE [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-if-col_lengthemp_numemployee-is-not-null-equivalent-in-oracle-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] IF COL_LENGTH(&#039;EMP_NUM&#039;,&#039;EMPLOYEE&#039;) IS NOT NULL - EQUIVALENT IN ORACLE [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] In Oracle, you have to use dynamic SQL as you can&#8217;t execute DDL in PL\/SQL as is. Why PL\/SQL? Because IF-THEN-ELSE is PL\/SQL, not SQL. Presuming this is part of your PL\/SQL procedure, you&#8217;d then if col_length(&#039;EMP_NUM&#039;, &#039;EMPLOYEE&#039;) is not null then execute immediate &#039;alter table employee modify emp_num not null&#039;; end if; That&#8217;s ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-if-col_lengthemp_numemployee-is-not-null-equivalent-in-oracle-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-23T06:53:48+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-if-col_lengthemp_numemployee-is-not-null-equivalent-in-oracle-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-if-col_lengthemp_numemployee-is-not-null-equivalent-in-oracle-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] IF COL_LENGTH(&#8216;EMP_NUM&#8217;,&#8217;EMPLOYEE&#8217;) IS NOT NULL &#8211; EQUIVALENT IN ORACLE [closed]\",\"datePublished\":\"2022-10-23T06:53:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-if-col_lengthemp_numemployee-is-not-null-equivalent-in-oracle-closed\/\"},\"wordCount\":173,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"oracle\",\"oracle11g\",\"sql-server\",\"tsql\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-if-col_lengthemp_numemployee-is-not-null-equivalent-in-oracle-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-if-col_lengthemp_numemployee-is-not-null-equivalent-in-oracle-closed\/\",\"name\":\"[Solved] IF COL_LENGTH('EMP_NUM','EMPLOYEE') IS NOT NULL - EQUIVALENT IN ORACLE [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-23T06:53:48+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-if-col_lengthemp_numemployee-is-not-null-equivalent-in-oracle-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-if-col_lengthemp_numemployee-is-not-null-equivalent-in-oracle-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-if-col_lengthemp_numemployee-is-not-null-equivalent-in-oracle-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] IF COL_LENGTH(&#8216;EMP_NUM&#8217;,&#8217;EMPLOYEE&#8217;) IS NOT NULL &#8211; EQUIVALENT IN ORACLE [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] IF COL_LENGTH('EMP_NUM','EMPLOYEE') IS NOT NULL - EQUIVALENT IN ORACLE [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-if-col_lengthemp_numemployee-is-not-null-equivalent-in-oracle-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] IF COL_LENGTH('EMP_NUM','EMPLOYEE') IS NOT NULL - EQUIVALENT IN ORACLE [closed] - JassWeb","og_description":"[ad_1] In Oracle, you have to use dynamic SQL as you can&#8217;t execute DDL in PL\/SQL as is. Why PL\/SQL? Because IF-THEN-ELSE is PL\/SQL, not SQL. Presuming this is part of your PL\/SQL procedure, you&#8217;d then if col_length('EMP_NUM', 'EMPLOYEE') is not null then execute immediate 'alter table employee modify emp_num not null'; end if; That&#8217;s ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-if-col_lengthemp_numemployee-is-not-null-equivalent-in-oracle-closed\/","og_site_name":"JassWeb","article_published_time":"2022-10-23T06:53:48+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-if-col_lengthemp_numemployee-is-not-null-equivalent-in-oracle-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-if-col_lengthemp_numemployee-is-not-null-equivalent-in-oracle-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] IF COL_LENGTH(&#8216;EMP_NUM&#8217;,&#8217;EMPLOYEE&#8217;) IS NOT NULL &#8211; EQUIVALENT IN ORACLE [closed]","datePublished":"2022-10-23T06:53:48+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-if-col_lengthemp_numemployee-is-not-null-equivalent-in-oracle-closed\/"},"wordCount":173,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["oracle","oracle11g","sql-server","tsql"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-if-col_lengthemp_numemployee-is-not-null-equivalent-in-oracle-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-if-col_lengthemp_numemployee-is-not-null-equivalent-in-oracle-closed\/","name":"[Solved] IF COL_LENGTH('EMP_NUM','EMPLOYEE') IS NOT NULL - EQUIVALENT IN ORACLE [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-23T06:53:48+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-if-col_lengthemp_numemployee-is-not-null-equivalent-in-oracle-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-if-col_lengthemp_numemployee-is-not-null-equivalent-in-oracle-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-if-col_lengthemp_numemployee-is-not-null-equivalent-in-oracle-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] IF COL_LENGTH(&#8216;EMP_NUM&#8217;,&#8217;EMPLOYEE&#8217;) IS NOT NULL &#8211; EQUIVALENT IN ORACLE [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/17215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=17215"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/17215\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=17215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=17215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=17215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}