删除年份的前两位数 - Postgresql

时间:2017-05-04 13:46:33

标签: sql postgresql

我有一张年份表,作为字段。表中的当前值采用以下格式:

year | term
------------
2006 | S1
2007 | S2
2009 | S1
2010 | S2

我使用以下代码没有成功:

select concat (replace(term, 'S', 's'), year) as test from semesters;

我得到以下结果:

Test
----
s12006
s22007
s12009
s12010

我想运行一个查询,我将以这种格式获得结果:

Test
-----
s106
s207
s109
s210

请给我一些关于如何在postgresql中实现此目的的指针。感谢

3 个答案:

答案 0 :(得分:0)

你只想要一年中的最后两个字符,所以:

select concat(replace(term, 'S', 's'), right(year, 2)) as test
from semesters;

或者,如果您想要花哨,请使用overlay()

select overlay(year placing lower(term) from 1 for 2)

答案 1 :(得分:0)

@Test
public void sellercheck () throws InterruptedException
{
      Thread.sleep(2000);
      driver.findElement(By.id("UserEvent")).click();  
      //String r=Read.getvalue().get(0);
      //select the seller
      driver.findElement(By.id("LegacyNumberCriterion")).sendKeys("123456");
      driver.findElement(By.id("SuperUse")).click();
      System.out.println("seller number entered");          
      try
      {
        if(driver.findElements(By.id("OrganizationBranchId")).size()!=0)
        {
            driver.findElement(By.id("button1")).click();
        }
        else
        {
            System.out.println("The button is not available for the seller");
        }
      }
      catch(NoSuchElementException e)
      {
        System.out.println("Element does not exist!");
      }
  }   

答案 2 :(得分:0)

这最终对我有用:

 select cast( concat (SUBSTR(cast (year as text), 3,4), 
 replace(term, 'S', 's')) as char(4)) as test from semesters;