{"id":25910,"date":"2022-07-04T10:24:36","date_gmt":"2022-07-04T00:24:36","guid":{"rendered":"https:\/\/database.guide\/?p=25910"},"modified":"2022-07-04T10:24:36","modified_gmt":"2022-07-04T00:24:36","slug":"redis-getrange-command-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/redis-getrange-command-explained\/","title":{"rendered":"Redis GETRANGE Command Explained"},"content":{"rendered":"\n<p>In Redis, the <code>GETRANGE<\/code> command allows us to get part of a string at a given key, starting and ending at the specified offsets.<\/p>\n\n\n\n<p>The <code>GETRANGE<\/code> command replaced the <code>SUBSTR<\/code> command, which basically does the same thing. The <code>SUBSTR<\/code> command is now considered deprecated (as of Redis 2.0.0).<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<p>The syntax for <code>GETRANGE<\/code> goes like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GETRANGE key start end<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Suppose we set a key to the following value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET band \"Avenged Sevenfold\"<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">OK<\/pre>\n\n\n\n<p>And we confirm that it holds that value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET band<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"Avenged Sevenfold\"<\/pre>\n\n\n\n<p>Let&#8217;s now use <code>GETRANGE<\/code> to get part of that value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GETRANGE band 8 12<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"Seven\"<\/pre>\n\n\n\n<p>Here, we returned a substring starting at position 8 and ending at position 12.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Negative Offsets<\/h2>\n\n\n\n<p>It&#8217;s possible to use negative offsets. When doing this, it starts from the end of the string:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GETRANGE band -8 12<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"even\"<\/pre>\n\n\n\n<p>Note that the second offset value (the end position) specifies the actual position of the end of the string (not the length of the string). To further illustrate this, here&#8217;s what happens when the second value is lower than the starting position:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GETRANGE band -8 7<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"\"<\/pre>\n\n\n\n<p>Bear in mind that the second offset value can also be a negative value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GETRANGE band -8 -5<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"even\"<\/pre>\n\n\n\n<p>In this case it counts back from the end of the string (just like the starting value did).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">When the Offset is Larger than the Original String<\/h2>\n\n\n\n<p>The function limits the resulting range to the actual length of the string.<\/p>\n\n\n\n<p>So here&#8217;s an example of what happens when the offset values represent a longer string than the original value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GETRANGE band 8 30<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"Sevenfold\"<\/pre>\n\n\n\n<p>And here&#8217;s what happens when the starting position is outside of the string:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GETRANGE band 20 30<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"\"<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Non Existent Keys<\/h2>\n\n\n\n<p>Specifying a non-existent key results in the empty string:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GETRANGE artist 10 12<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"\"<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Wrong Number of Arguments<\/h2>\n\n\n\n<p>Specifying no arguments or the wrong number of arguments results in an error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GETRANGE band 8<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(error) ERR wrong number of arguments for 'getrange' command<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In Redis, the GETRANGE command allows us to get part of a string at a given key, starting and ending at the specified offsets. The GETRANGE command replaced the SUBSTR command, which basically does the same thing. The SUBSTR command is now considered deprecated (as of Redis 2.0.0).<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[153],"tags":[154,84,20],"class_list":["post-25910","post","type-post","status-publish","format-standard","hentry","category-redis","tag-commands","tag-string","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/25910","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=25910"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/25910\/revisions"}],"predecessor-version":[{"id":25917,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/25910\/revisions\/25917"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=25910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=25910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=25910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}