我试图在数据库中查找所有录入日期早于特定时间范围的记录(在这种情况下,所有录入日期都早于4天)。
我有:
# Get the database object
$sqlDB = Get-AzureRmSqlDatabase `
-ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-DatabaseName $databaseName
Write-Output "DB name: $($sqlDB.DatabaseName)" | timestamp
Write-Output "Current DB status: $($sqlDB.Status), edition: $($sqlDB.Edition), tier: $($sqlDB.CurrentServiceObjectiveName)" | timestamp
Write-Output "Changing tier!" | timestamp
$sqlDB | Set-AzureRmSqlDatabase -Edition $defaultEdition -RequestedServiceObjectiveName $defaultTier | out-null
Write-Output "Change initiated." | timestamp
$sqlDB = Get-AzureRmSqlDatabase -ResourceGroupName $resourceGroupName -ServerName $serverName -DatabaseName $databaseName
Write-Output "Current DB status: $($sqlDB.Status), edition: $($sqlDB.Edition), tier: $($sqlDB.CurrentServiceObjectiveName)" | timestamp
结果,我得到了很多与此匹配的录入日期,但是我也得到了仅两天前的日期,因此与我的代码不匹配。我在做什么错了?
如果有帮助,录取日期的格式为mm / dd / yyyy。
答案 0 :(得分:0)
admitdate
应该是date
。您似乎暗示这是一个字符串。您可以尝试:
where to_date(admitdate, 'MM/DD/YYYY') < trunc(sysdate) - 4;
然后您可以修复表中的数据,以便将其存储为日期。
答案 1 :(得分:0)
日期(包括sysdate)具有时间成分。即使您所有的admitdate
值都在午夜,这仍然是一个时间,并且如果您随后运行查询,则sysdate只会在午夜。
select sysdate, sysdate-4, trunc(sysdate), trunc(sysdate)-4 from dual;
SYSDATE SYSDATE-4 TRUNC(SYSDATE) TRUNC(SYSDATE)-4
------------------- ------------------- ------------------- -------------------
2018-06-21 16:44:53 2018-06-17 16:44:53 2018-06-21 00:00:00 2018-06-17 00:00:00
如果您在sysdate-4
上过滤记录,则该记录将包含所有admitdate
值,在此示例中,直到2018-06-17 16:44:53;因此大概是所有第17条记录,如果它们实际上都是午夜。
with membertable (memberid, admitdate) as (
select 1, date '2018-06-15' from dual
union all select 2, date '2018-06-16' from dual
union all select 3, date '2018-06-17' from dual
union all select 4, date '2018-06-18' from dual
union all select 5, date '2018-06-19' from dual
union all select 6, date '2018-06-20' from dual
union all select 7, date '2018-06-21' from dual
)
select memberid, admitdate
from membertable
where admitdate < (sysdate-4);
MEMBERID ADMITDATE
---------- -------------------
1 2018-06-15 00:00:00
2 2018-06-16 00:00:00
3 2018-06-17 00:00:00
如果您截断要与之比较的值,则其时间部分也将被视为午夜,因此您将仅匹配直到(但不包括)该时间点的记录,即2018-06-17 00: 00:00:
with membertable (memberid, admitdate) as (
select 1, date '2018-06-15' from dual
union all select 2, date '2018-06-16' from dual
union all select 3, date '2018-06-17' from dual
union all select 4, date '2018-06-18' from dual
union all select 5, date '2018-06-19' from dual
union all select 6, date '2018-06-20' from dual
union all select 7, date '2018-06-21' from dual
)
select memberid, admitdate
from membertable
where admitdate < trunc(sysdate)-4;
MEMBERID ADMITDATE
---------- -------------------
1 2018-06-15 00:00:00
2 2018-06-16 00:00:00