Click here to Skip to main content
15,886,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to insert only date in sql table using pl sql
---------------------------

I have a table in Sql,

Table Name -> CalendarDailySchedule
Column Name -> CalendarDates

I want to generate 365 days from today, This is working fine.
But Its inserting with date and time....just i need date as 18/02/2015 in column

AM using SqlServer 2005 database, This is my code. Please help.

SQL
DECLARE @CalDate datetime
DECLARE @Counter INT
SET @Counter =0
SET @CalDate = Getdate()
WHILE (@Counter < 365)
BEGIN
Insert into CalendarDailySchedule(CalendarDates) VALUES (@CalDate)
SET @Counter = @Counter + 1
SET @CalDate = @CalDate + 1

END
GO

Thanks...
Posted
Updated 18-Feb-15 17:52pm
v3

You need to use proper data type[^]. For sql server 2008 and higher it's a date data type.

Common Table Expression version:
SQL
;WITH MyDates AS
(
    SELECT CONVERT(DATE, '2015-01-01') AS MyDate
    UNION ALL
    SELECT DATEADD(dd, 1, MyDAte) AS MyDate
    FROM MyDates 
    WHERE MyDate<convert(date,>)
SELECT MyDate
FROM MyDates
OPTION (MAXRECURSION 0)
 
Share this answer
 
Datatype of CalendarDates column should be Date only, Check below

it works


SQL
Create table #CalendarDailySchedule(CalendarDates Date)

DECLARE @CalDate datetime
DECLARE @Counter INT
SET @Counter =0
SET @CalDate = Getdate()
WHILE (@Counter < 365)
BEGIN
Insert into #CalendarDailySchedule(CalendarDates) VALUES (@CalDate)
SET @Counter = @Counter + 1
SET @CalDate = @CalDate + 1
 
END
GO	

select * from #CalendarDailySchedule
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900