{"id":1458,"date":"2022-05-05T16:43:36","date_gmt":"2022-05-05T19:43:36","guid":{"rendered":"https:\/\/gladiston.net.br\/?page_id=1458"},"modified":"2022-09-14T10:51:44","modified_gmt":"2022-09-14T13:51:44","slug":"colocando-aspas-simples-numa-string-que-talvez-ja-tenha-aspas-tambem","status":"publish","type":"page","link":"https:\/\/gladiston.net.br\/en\/banco-de-dados\/colocando-aspas-simples-numa-string-que-talvez-ja-tenha-aspas-tambem\/","title":{"rendered":"Colocando aspas simples numa string que talvez j\u00e1 tenha aspas tamb\u00e9m"},"content":{"rendered":"\n<p>A linguagem pascal tem uma fun\u00e7\u00e3o muito util para constru\u00e7\u00e3o de queries chamada QuotedStr, esta fun\u00e7\u00e3o coloca uma string entre aspas, por exemplo digamos que uma variavel onde nome=Sant&#8217;Ana, e agora voc\u00ea precisa usar ela dentro de uma querie<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;sql&quot;,&quot;mime&quot;:&quot;text\/x-sql&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;SQL&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;sql&quot;}\">SQL.Text:='select * from clientes where nome='+QuotedStr(nome);<\/pre><\/div>\n\n\n\n<p>O QuotedStr notar\u00e1 as aspas simples em &#8216;nome&#8217; e a duplicar\u00e1 para que o c\u00f3digo SQL funcione ficando assim Sant&#8221;Ana. Dentro da linguagem procedural(psql daqui em diante) tamb\u00e9m precisamos disso em situa\u00e7\u00f5es variadas, por exemplo, quando precisamos usar <strong>execute statement<\/strong> com comandos SQL puros e voc\u00ea n\u00e3o sabe se alguns deles ter\u00e3o aspas simples em seu interior. Por essa raz\u00e3o criei a fun\u00e7\u00e3o STR_QUOTED:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;sql&quot;,&quot;mime&quot;:&quot;text\/x-sql&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;SQL&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;sql&quot;}\">create or alter function STR_QUOTED (\n    AVALUE varchar(4096),\n    AUSE_IN_SQL boolean = false)\nreturns varchar(4096)\nas\ndeclare variable Q varchar(1);\nBEGIN\n  \/* essa procedure retorna um texto (4096 bytes) entre aspas simplesmente e opcionalmente\n   com aspas simples dentro do texto transformando-se em aspas simples duplas.\n   p='ola mundo'\n   ret=STR_QUOTED(:p, false);   \/\/ resultado: 'ola mundo' com aspas simples abrindo e fechando o texto\n   se o segundo parametro for 'true' ele tamb\u00e9m trocar\u00e1 aspas simples que houverem dentro\n   do texto para aspas simples duplas.\n   Util para uso com banco de dados e instru\u00e7\u00f5es execute statement\n   by gladiston.santana[em]gmail.com\n  *\/\n  q='''';\n  if (:AUSE_IN_SQL) then\n  begin\n    if (position (:q in :AVALUE)&amp;gt;0) then\n    begin\n      --troca aspas simples por duplas\n      AVALUE = replace (:AVALUE, :q, :q||:q);\n    end\n  end\n  -- Retorna uma string entre aspas\n  RETURN :q||:AVALUE||:q;\nEND<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Como usar<\/h2>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;sql&quot;,&quot;mime&quot;:&quot;text\/x-sql&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;SQL&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;sql&quot;}\">SQL='update clientes set status='||\n  STR_QUOTED('A')||\n  ' where uf='||STR_QUOTED('SP');\nexecute statement :SQL;<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Conclus\u00e3o<\/h2>\n\n\n\n<p>Agora voc\u00ea tem um QuotedStr em psql.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A linguagem pascal tem uma fun\u00e7\u00e3o muito util para constru\u00e7\u00e3o de queries chamada QuotedStr, esta fun\u00e7\u00e3o coloca uma string entre aspas, por exemplo digamos que uma variavel onde nome=Sant&#8217;Ana, e agora voc\u00ea precisa usar ela dentro de uma querie O QuotedStr notar\u00e1 as aspas simples em &#8216;nome&#8217; e a duplicar\u00e1 para que o c\u00f3digo SQL [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"parent":1109,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"templates\/template-full-width.php","meta":{"footnotes":""},"class_list":["post-1458","page","type-page","status-publish","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/gladiston.net.br\/en\/wp-json\/wp\/v2\/pages\/1458","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gladiston.net.br\/en\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/gladiston.net.br\/en\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/gladiston.net.br\/en\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/gladiston.net.br\/en\/wp-json\/wp\/v2\/comments?post=1458"}],"version-history":[{"count":6,"href":"https:\/\/gladiston.net.br\/en\/wp-json\/wp\/v2\/pages\/1458\/revisions"}],"predecessor-version":[{"id":2020,"href":"https:\/\/gladiston.net.br\/en\/wp-json\/wp\/v2\/pages\/1458\/revisions\/2020"}],"up":[{"embeddable":true,"href":"https:\/\/gladiston.net.br\/en\/wp-json\/wp\/v2\/pages\/1109"}],"wp:attachment":[{"href":"https:\/\/gladiston.net.br\/en\/wp-json\/wp\/v2\/media?parent=1458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}