SQL 当前日期 (AS400)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5199663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 09:35:02  来源:igfitidea点击:

SQL Current date (AS400)

sqldateibm-midrange

提问by integral007

I am trying to select the records from an AS400 database that have a current date (in format MMDDYY).

我正在尝试从具有当前日期(格式为 MMDDYY)的 AS400 数据库中选择记录。

This is what I am planning to do:

这是我打算做的事情:

SELECT * FROM tableName WHERE $DATE='030411'

I tried combinations of this but with no luck:

我尝试了这个组合,但没有运气:

SELECT * FROM tableName WHERE $DATE='SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 1), '/', '')'

Any suggestions?

有什么建议?

回答by dmc

Try this:

尝试这个:

SELECT * FROM tableName WHERE $DATE=
   substring(cast(current date as char(10)),6,2) || 
   substring(cast(current date as char(10)),9,2) || 
   substring(cast(current date as char(10)),3,2)

You can see what value this expression brings back like this:

你可以看到这个表达式带回来的值是这样的:

select substring(cast(current date as char(10)),6,2) || 
   substring(cast(current date as char(10)),9,2) || 
   substring(cast(current date as char(10)),3,2)    
from sysibm.sysdummy1                                   

You have to use sysibm.sysdummy1since SQL on an AS/400 (iSeries, System i, etc.) doesn't let you SELECTvalues out of thin air.

您必须使用,sysibm.sysdummy1因为 AS/400(iSeries、System i 等)上的 SQL 不会让您SELECT凭空获得值。

Also note that current datemight bring the date back in a different format, depending on the SQL date format. This code is expecting it to be in *ISO format (YYYY-MM-DD).

另请注意current date,根据 SQL 日期格式,可能会以不同的格式返回日期。此代码期望它采用 *ISO 格式 ( YYYY-MM-DD)。

Here are some SQL statements that I used to validate this routine.

以下是我用来验证此例程的一些 SQL 语句。

create table dmclib.test2 ( $DATE decimal(6,0) ) ;

insert into dmclib.test2 values   
  (010111), (010211), (031011) ;

SELECT * FROM dmclib.test2                         
where $DATE =                                      
   substring(cast(current date as char(10)),6,2) ||
   substring(cast(current date as char(10)),9,2) ||
   substring(cast(current date as char(10)),3,2) ;

Here's what I got back:

这是我得到的:

....+...                        
  $DATE                         
 31,011                         
********  End of data  ******** 

回答by CRPence

insert( replace( char( current_date, usa ) , '/', '' ) , 5, 2, '')

The above generates the character string representation of the *USA format [i.e. MM/DD/YYYY] for the Current Date using the CHARcast scalar function, then replaces the '/' character in the string MM/DD/YYYYwith the null string to become MMDDYYYYusing the REPLACEscalar function, and then inserts the null string starting as position five [i.e. the first digit of YYYYin MMDDYYYY] while removing two bytes of that string [i.e. the "insert" is actually replacing the fifth and sixth bytes, or per the arguments on the insert scalar from the fifth (5) for two (2) bytes, with the null string] using the INSERTscalar function, thus producing the desired result of the string MMDDYY[where the last two bytes YYare the 10**1 and 10**0 portions of the number denoted as YYYYin MM/DD/YYYY].

上述生成*美国格式[即的字符串表示MM/DD/YYYY用于使用该当前日期CHAR投标量函数,然后替换串中的“/”字符MM/DD/YYYY与空字符串成为MMDDYYYY使用REPLACE标量函数,然后插入从位置 5 开始的空字符串 [即YYYYin的第一个数字MMDDYYYY] 同时删除该字符串的两个字节 [即“插入”实际上是替换第五和第六个字节,或者根据来自第五个 (5) 的插入标量上的参数对于两 (2) 个字节,带有空字符串] 使用INSERT标量函数,从而产生所需的字符串结果MMDDYY[其中最后两个字节YY是数字的 10**1 和 10**0 部分,表示为YYYYMM/DD/YYYY]。

Regards, Chuck

问候,查克

回答by arlen

030411=

030411=

select substring(Replace(Replace(convert(varchar(10),GETDATE(), 101), '/', ''), '/', ''),1,4)+ substring(convert(varchar(4),year(GETDATE()),101),3,2)

选择子字符串(替换(替换(转换(varchar(10),GETDATE(),101),'/',''),'/',''),1,4)+子字符串(转换(varchar(4) ,年(GETDATE()),101),3,2)

回答by Jatin Sapariya

Try the below steps..

尝试以下步骤..

  1. STRSQL

  2. TYPE YOUR QUERY

  3. PRESS F4

  4. PUT YOUR CURSOR IN FRONT OF "WHERE"

  5. PRESS F4

  6. PUT 1 IN FRONT OF YOUR APPROPRIATE DATE COLUMN NAME

  7. HIT ENTER

  8. IF YOUR COLUMN IS NUMERIC GIVE VALUE WITHOUT QUOTES E.G 1234 AND NOT '1234'

  9. HIT ENTER

  10. YOU SHOULD GET YOUR RESULT

  1. STRSQL

  2. 输入您的查询

  3. 按F4

  4. 将光标放在“WHERE”前面

  5. 按F4

  6. 在您合适的日期列名称前加 1

  7. 按回车键

  8. 如果您的列是数字,则给出不带引号的值 EG 1234 而不是“1234”

  9. 按回车键

  10. 你应该得到你的结果