在SQL中将小时转换为分钟

时间:2016-11-23 00:26:46

标签: sql teradata

我正在尝试编写一个将小时转换为分钟的正则表达式。 目前我有一个值,它给我点前的小时数和点后的分钟数,例如6.10是6小时10分钟。

我可以使用正则表达式查找点之前的值并乘以60(得到分钟)然后在点之后添加值(已经是几分钟)?

因此,根据之前使用的示例,它会: (6 * 60)+ 10

谢谢!

3 个答案:

答案 0 :(得分:2)

如果您的值“6.10”存储为字符串,请先将其转换为数字:

Change(&pod)

然后,使用ModulusFloor

总分钟=小数部分* 100 +整数部分* 60。

因此,如果您的值SET @Input = CAST(@InputString AS FLOAT) 包含在名为@Input的变量中,那么您可以获得所需的值

6.10

如果它在表中,那么更合适的东西是合适的:

SET @Output = (@Input % 1) * 100 + FLOOR(@Input, 0)  * 60

以上仅在分钟填充为零时才有效,例如6小时5分钟= 6.05。如果你代表没有填充的分钟(6小时5分钟= 6.5),那么使用under建议的方法。

答案 1 :(得分:2)

有一个STRTOK函数来标记字符串,然后就是:

select '12.34' as x, 
    cast(strtok(x,'.',1) as int) * 60 + cast(strtok(x,'.',2) as int)

答案 2 :(得分:1)

正则表达式对于这种简单的字符串操作可能是一种过度杀伤。您只需要INDEX函数来获取点和SUBSTR函数的位置以获得小时和分钟。

点位置:

{
  "_args": [
    [
      "minimatch@^3.0.2",
      "C:\\Stash-repo\\bs\\node_modules\\glob"
    ]
  ],
  "_from": "minimatch@>=3.0.2 <4.0.0",
  "_id": "minimatch@3.0.3",
  "_inCache": true,
  "_location": "/minimatch",
  "_nodeVersion": "4.4.4",
  "_npmOperationalInternal": {
    "host": "packages-12-west.internal.npmjs.com",
    "tmp": "tmp/minimatch-3.0.3.tgz_1470678322731_0.1892083385027945"
  },
  "_npmUser": {
    "email": "i@izs.me",
    "name": "isaacs"
  },
  "_npmVersion": "3.10.6",
  "_phantomChildren": {},
  "_requested": {
    "name": "minimatch",
    "raw": "minimatch@^3.0.2",
    "rawSpec": "^3.0.2",
    "scope": null,
    "spec": ">=3.0.2 <4.0.0",
    "type": "range"
  },
  "_requiredBy": [
    "/glob",
    "/gulp-match",
    "/readdirp",
    "/rework-import/glob"
  ],
  "_resolved": "http://npm.pacden.com/minimatch/-/minimatch-3.0.3.tgz",
  "_shasum": "2a4e4090b96b2db06a9d7df01055a62a77c9b774",
  "_shrinkwrap": null,
  "_spec": "minimatch@^3.0.2",
  "_where": "C:\\Stash-repo\\bs\\node_modules\\glob",
  "author": {
    "email": "i@izs.me",
    "name": "Isaac Z. Schlueter",
    "url": "http://blog.izs.me"
  },
  "bugs": {
    "url": "https://github.com/isaacs/minimatch/issues"
  },
  "dependencies": {
    "brace-expansion": "^1.0.0"
  },
  "description": "a glob matcher in javascript",
  "devDependencies": {
    "standard": "^3.7.2",
    "tap": "^5.6.0"
  },
  "directories": {},
  "dist": {
    "shasum": "2a4e4090b96b2db06a9d7df01055a62a77c9b774",
    "tarball": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.3.tgz"
  },
  "engines": {
    "node": "*"
  },
  "files": [
    "minimatch.js"
  ],
  "gitHead": "eed89491bd4a4e6bc463aac0dfb5c29ef0d1dc13",
  "homepage": "https://github.com/isaacs/minimatch#readme",
  "installable": true,
  "license": "ISC",
  "main": "minimatch.js",
  "maintainers": [
    {
      "name": "isaacs",
      "email": "i@izs.me"
    }
  ],
  "name": "minimatch",
  "optionalDependencies": {},
  "repository": {
    "type": "git",
    "url": "git://github.com/isaacs/minimatch.git"
  },
  "scripts": {
    "posttest": "standard minimatch.js test/*.js",
    "test": "tap test/*.js"
  },
  "version": "3.0.3"
}

小时(点前):

 select INDEX([yourstring], '.') 

分钟(点后):

 select SUBSTR([yourstring], 1, INDEX([yourstring], '.')  - 1)