拆分字段并添加小数点以创建数值SQL

时间:2018-09-18 13:17:35

标签: sql sql-server data-analysis data-manipulation

我正在使用的SQL Server 2014中有一个如下所示的字段:

<div id="mainapp">
 <table>
  <thead>
    <th>Name</th><th>Status</th>
  </thead>
  <tbody>
    <tr v-for="user in users">
      <td>{{user.name}}</td>
      <td><combobox v-bind:default="user.status" v-bind:data="status_codes" v-on:item_change="status_change(item, user)"></combobox></td>
    </tr>
  </tbody>
  </table>
</div>

我想添加一个拆分字段并添加一个小数点并创建一个数字字段。这是我想看到的:

var combobox = Vue.component('combobox', {
  data: function () {
    return {
      selected_item:{title:'Select', value:-1},
      visible:false
    }
  },
  props:['data','default','symbol'],
  template: `
    <div class="combobox">
      <span class="symbol" v-if="!symbol">
        <i class="fa fa-chevron-down" aria-hidden="true"  ></i>
      </span>
      <span class="main" v-on:click="toggleVisible">{{selected_item.title}}</span>
      <ul class="combodata" v-if="visible">
        <li class="item" v-for="item in data" v-on:click="select(item)">{{item.title}}</li>
      </ul>
    </div>
  `,
  created:function(){
    if(this.data.length>0){
      if(this.default == null || this.default == undefined || this.default =='') this.default=0;
      this.selected_item = this.data[this.default];
    }
  },
  methods:{
    toggleVisible:function(){
      this.visible = !this.visible;
    },
    select:function(item){
      if(this.selected_item != item){
        this.selected_item= item;
        this.$emit('item_change', item);
      }
      this.visible = false;
    }
  }
});

var app=new Vue({
  el:"#mainapp",
  data:{
     status_codes:[{title:'Inactive', value:0},{title:'Active', value:1}],
     users:[{name:'Andrew', status:1},{name:'Jackson', status:0},{name:'Tom', status:1}]
  },
  methods:{
     status_change:function(item,user){  //This gets only the parameter from the event. How could I pass the additional parameters to this function?
        console.log(item,user);
        try{
          user.status = item.value;
        }catch(e){ console.log}
     }
  }
});

因此,我需要拆分字段,添加小数点,然后转换为数字值。我尝试了一些代码,但出现错误。请在下面查看我的代码:

**RawField**
20060202
20060323

我收到以下错误:

**RawField**
200602.02
200603.23

这是一个好方法吗?

3 个答案:

答案 0 :(得分:2)

您要将字符串转换为带有小数点后两位的数字吗?

select convert(decimal(10,2), RawField) / 100.0

我让您的RawField包含其他字母数字,然后您只发布了前8个字符?

这应该有效。只需采用前8个字符并进行转换。简单直接

select convert(decimal(10,2), left(RawField, 8)) / 100.0

答案 1 :(得分:1)

请尝试这个。

select top 1000 cast(SUBSTRING(cast(RawField as varchar(6)),1,6) + cast('.' as varchar(1)) + SUBSTRING(cast(RawField as varchar(2)),6,2) as numeric(8,2))
  from Table

您正在尝试将带有十进制数字的字符串转换为int。

答案 2 :(得分:0)

在投射时使用浮点数/数字/小数

select cast(SUBSTRING(RawField ,1,6) + cast('.' as varchar(1)) + SUBSTRING(RawField ,7,2) as numeric(16,2))