Sql server 如何在日期字段中插入2015年的所有日期?
我正在使用SQL Server,有一个带有“日期”字段的表\u Date。我想在此字段中插入所有2015年日期Sql server 如何在日期字段中插入2015年的所有日期?,sql-server,date,Sql Server,Date,我正在使用SQL Server,有一个带有“日期”字段的表\u Date。我想在此字段中插入所有2015年日期 它应该有365个不同的行,2015年每天一行。一种方法是使用递归CTE: with dates as ( select cast('2015-01-01' as date) as thedate union all select dateadd(day, 1, thedate) from dates where thedate
它应该有365个不同的行,2015年每天一行。一种方法是使用递归CTE:
with dates as (
select cast('2015-01-01' as date) as thedate
union all
select dateadd(day, 1, thedate)
from dates
where thedate < '2015-12-31'
)
select *
from dates
option (maxrecursion 0);
一种方法是使用递归CTE:
with dates as (
select cast('2015-01-01' as date) as thedate
union all
select dateadd(day, 1, thedate)
from dates
where thedate < '2015-12-31'
)
select *
from dates
option (maxrecursion 0);
一种方法是使用递归CTE:
with dates as (
select cast('2015-01-01' as date) as thedate
union all
select dateadd(day, 1, thedate)
from dates
where thedate < '2015-12-31'
)
select *
from dates
option (maxrecursion 0);
一种方法是使用递归CTE:
with dates as (
select cast('2015-01-01' as date) as thedate
union all
select dateadd(day, 1, thedate)
from dates
where thedate < '2015-12-31'
)
select *
from dates
option (maxrecursion 0);
类似的方法也可以奏效:
declare @count int = 0
while (@count < 365)
begin
--make this the insert
select DATEADD(DD, @count, getdate())
set @count = @count + 1
end
但不确定这将应用于什么上下文。。。这是非常基本的,但如果这是一次性事件,则无所谓。类似的方法也可以:
declare @count int = 0
while (@count < 365)
begin
--make this the insert
select DATEADD(DD, @count, getdate())
set @count = @count + 1
end
但不确定这将应用于什么上下文。。。这是非常基本的,但如果这是一次性事件,则无所谓。类似的方法也可以:
declare @count int = 0
while (@count < 365)
begin
--make this the insert
select DATEADD(DD, @count, getdate())
set @count = @count + 1
end
但不确定这将应用于什么上下文。。。这是非常基本的,但如果这是一次性事件,则无所谓。类似的方法也可以:
declare @count int = 0
while (@count < 365)
begin
--make this the insert
select DATEADD(DD, @count, getdate())
set @count = @count + 1
end
但不确定这将应用于什么上下文。。。这是非常基本的,但如果这是一次性事件,则无所谓。这里有一种方法:
CREATE TABLE #nums(num INT);
INSERT INTO #nums VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
WITH cteDays AS
(
SELECT 100*d100.num + 10*d10.num + d1.num AS YearDay
FROM #nums AS d1
CROSS JOIN #nums AS d10
CROSS JOIN #nums AS d100
WHERE d100.num <=3
)
SELECT CAST('2015-01-01' AS DATETIME) + YearDay AS YearDate
FROM cteDays
WHERE YEAR(CAST( CAST('2015-01-01' AS DATETIME) + YearDay AS DATETIME)) = 2015
这里有一个方法:
CREATE TABLE #nums(num INT);
INSERT INTO #nums VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
WITH cteDays AS
(
SELECT 100*d100.num + 10*d10.num + d1.num AS YearDay
FROM #nums AS d1
CROSS JOIN #nums AS d10
CROSS JOIN #nums AS d100
WHERE d100.num <=3
)
SELECT CAST('2015-01-01' AS DATETIME) + YearDay AS YearDate
FROM cteDays
WHERE YEAR(CAST( CAST('2015-01-01' AS DATETIME) + YearDay AS DATETIME)) = 2015
这里有一个方法:
CREATE TABLE #nums(num INT);
INSERT INTO #nums VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
WITH cteDays AS
(
SELECT 100*d100.num + 10*d10.num + d1.num AS YearDay
FROM #nums AS d1
CROSS JOIN #nums AS d10
CROSS JOIN #nums AS d100
WHERE d100.num <=3
)
SELECT CAST('2015-01-01' AS DATETIME) + YearDay AS YearDate
FROM cteDays
WHERE YEAR(CAST( CAST('2015-01-01' AS DATETIME) + YearDay AS DATETIME)) = 2015
这里有一个方法:
CREATE TABLE #nums(num INT);
INSERT INTO #nums VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
WITH cteDays AS
(
SELECT 100*d100.num + 10*d10.num + d1.num AS YearDay
FROM #nums AS d1
CROSS JOIN #nums AS d10
CROSS JOIN #nums AS d100
WHERE d100.num <=3
)
SELECT CAST('2015-01-01' AS DATETIME) + YearDay AS YearDate
FROM cteDays
WHERE YEAR(CAST( CAST('2015-01-01' AS DATETIME) + YearDay AS DATETIME)) = 2015
可能的重复可能的重复感谢戈登为我的问题提出了两种解决方案。我试图运行递归CTE,但它给出了以下错误。Msg 1035,15级,状态10,第2行“cast”附近语法不正确,应为“AS”。Msg 102,15级,状态1,第6行附近语法不正确。@Sam。我在演员阵容中遗漏了这一类型。感谢戈登为我的问题提出了两种解决方案。我试图运行递归CTE,但它给出了以下错误。Msg 1035,15级,状态10,第2行“cast”附近语法不正确,应为“AS”。Msg 102,15级,状态1,第6行附近语法不正确。@Sam。我在演员阵容中遗漏了这一类型。感谢戈登为我的问题提出了两种解决方案。我试图运行递归CTE,但它给出了以下错误。Msg 1035,15级,状态10,第2行“cast”附近语法不正确,应为“AS”。Msg 102,15级,状态1,第6行附近语法不正确。@Sam。我在演员阵容中遗漏了这一类型。感谢戈登为我的问题提出了两种解决方案。我试图运行递归CTE,但它给出了以下错误。Msg 1035,15级,状态10,第2行“cast”附近语法不正确,应为“AS”。Msg 102,15级,状态1,第6行附近语法不正确。@Sam。我把字体漏在演员阵容里了。