Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Adding new columns to a table using the datepart function SQL Expand / Collapse
Author
Message
Posted Thursday, February 07, 2013 2:28 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, May 07, 2013 3:30 AM
Points: 62, Visits: 155
Morning Everyone,

I have just started training on SQL and was wondering if you could help with a problem I have encountered.

I have populated a column with a date via the CREATE TABLE Function

USE Occupancy
CREATE TABLE Time
(Date date not null)

DECLARE @StartDate Date,
@EndDate Date

SET @StartDate = '01 Jan 2010'
SET @EndDate= '31 March 2015'
WHILE @StartDate <= @EndDate

BEGIN
INSERT INTO Time
VALUES (@StartDate)
SET @StartDate = Dateadd(dd,1,@StartDate)
END

But what I would like to do now is create 4 Extra columns called CalendarYear, CalendarMonth, FinancialYear and finally FinancialMonth, I know I should use the Datepart function but not matter how many time I have written it, it doesn't seem to work.

All help is much appreciated.

Thanks

Wayne
Post #1416903
Posted Thursday, February 07, 2013 3:44 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: 2 days ago @ 1:30 AM
Points: 803, Visits: 2,124
Can you post the code that you have tried.

As you are learning I would strongly recommend that stop using While loops/Cursors these are notoriously inefficient, in addition you will learn a lot of bad habits that will be difficult to break down the line.



_________________________________________________________________________
SSC Guide to Posting and Best Practices
Post #1416932
Posted Thursday, February 07, 2013 3:53 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, May 07, 2013 3:30 AM
Points: 62, Visits: 155
Hi Jason

I have been on this now for hours and getting so frustrated, you mentioned yesterday about loops on another post I had but my boss wants me to use it, all he wants me to do now is add the extra columns.

I used (SELECT Datepart(YEAR, '@StartDate') AS CalendarYear) but it didn't work and to be honest I don't know why.

Code so far:

USE Occupancy
CREATE TABLE Time
(Date date not null)

DECLARE @StartDate Date,
@EndDate Date

SET @StartDate = '01 Jan 2010'
SET @EndDate= '31 March 2015'

WHILE @StartDate <= @EndDate

BEGIN

INSERT INTO Time
VALUES (@StartDate)
SET @StartDate = Dateadd(dd,1,@StartDate)
(SELECT Datepart(YEAR, '@StartDate') AS CalendarYear)

END

Thanks for you help in advance.

Wayne
Post #1416935
Posted Thursday, February 07, 2013 4:01 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: 2 days ago @ 1:30 AM
Points: 803, Visits: 2,124
I wonder if hes teaching you the pain of using whiles.

Anyway back to the question at hand.

Firstly you've placed the @StartDate in single quotes you shouldnt

The statement should read

SELECT DATEPART(Year,@StartDate) AS CALENDARYEAR

Next are you meant to be adding this columns to the Table Time that you have as you will need to alter the table to have a add a CalendarYear.


_________________________________________________________________________
SSC Guide to Posting and Best Practices
Post #1416940
Posted Thursday, February 07, 2013 4:08 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, May 07, 2013 3:30 AM
Points: 62, Visits: 155
You're right about pain, he said I will give you a easy task first, I have been trying to figure this oout now for a day and a half. Anway back to the task at hand:

The final table should look something like this:

Date CalendarYear CalendarMonth FinancialYear FinancialMonth
01 Jan 2011 2011 January 2010 January
02 Jan 2011 2011 January 2010 January
03 Jan 2011 2011 January 2010 January
04 Jan 2011 2011 January 2010 January

Ta

Wayne
Post #1416945
Posted Thursday, February 07, 2013 5:42 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: 2 days ago @ 1:30 AM
Points: 803, Visits: 2,124
good luck, I hope this is being used as a 'wrong' way of doing something rather than the right way.

I would pick up a couple of books on SQL programming, the ones I would recommend are SQL 2008 Fundamentals and Inside SQL Server Programming TSQL both by Itzik Ben-gan.


_________________________________________________________________________
SSC Guide to Posting and Best Practices
Post #1417005
Posted Thursday, February 07, 2013 8:21 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 2:33 PM
Points: 8,620, Visits: 8,261

SELECT Datepart(YEAR, '@StartDate') AS CalendarYear)


The issue there is you have passed a string as the datetime because you wrapped your variable with tick marks.

Try this instead.
SELECT Datepart(YEAR, @StartDate) AS CalendarYear)



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1417106
Posted Thursday, February 07, 2013 8:30 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 2:33 PM
Points: 8,620, Visits: 8,261
wafw1971 (2/7/2013)
Morning Everyone,

I have just started training on SQL and was wondering if you could help with a problem I have encountered.

I have populated a column with a date via the CREATE TABLE Function

USE Occupancy
CREATE TABLE Time
(Date date not null)

DECLARE @StartDate Date,
@EndDate Date

SET @StartDate = '01 Jan 2010'
SET @EndDate= '31 March 2015'
WHILE @StartDate <= @EndDate

BEGIN
INSERT INTO Time
VALUES (@StartDate)
SET @StartDate = Dateadd(dd,1,@StartDate)
END

But what I would like to do now is create 4 Extra columns called CalendarYear, CalendarMonth, FinancialYear and finally FinancialMonth, I know I should use the Datepart function but not matter how many time I have written it, it doesn't seem to work.

All help is much appreciated.

Thanks

Wayne


A loop is horribly inefficient as a way to learn this stuff. I would recommend using computed columns instead of storing values. If you have the date portions stored in their own columns how are you going to handle updates? I can see that having dateparts could be useful in very large tables but do NOT store this as data.

Here is an example of using the MONTH as a computed column. Imagine your table and what you have to do if you update the date like in the update statement in the example. You would have to have a trigger on your table to update the other columns. Yuck!!!

if object_id('tempdb..#MyDate') is not null
drop table #MyDate

CREATE TABLE #MyDate
(
SomeID int identity,
SomeDate datetime,
SomeMonth as datepart(MONTH, SomeDate) persisted
)

insert #MyDate
select getdate()

select * from #MyDate

update #MyDate
set SomeDate = '2013-07-01'

select * from #MyDate



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1417115
Posted Thursday, February 07, 2013 8:31 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 2:33 PM
Points: 8,620, Visits: 8,261
And also since you say you are learning. You need to avoid using reserved words for object names. Tables named Time and columns named Date will cause you untold pain as you try to query those tables.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1417117
Posted Thursday, February 07, 2013 8:44 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 2:33 PM
Points: 8,620, Visits: 8,261
Just to demonstrate how incredibly inefficient cursors can be. Take a look at this insert. It uses the tally table which can be found here. http://www.sqlservercentral.com/articles/T-SQL/62867/

This code will insert the same 1,916 rows as your while loop. It will perform all of the inserts in about the blink of an eye, literally.

if object_id('tempdb..#MyTime') is not null
drop table #MyTime

create table #MyTime
(
MyDate date not null
)
insert #MyTime
select dateadd(DAY, N - 1, '2010-01-01')
from Tally
where N <= datediff(day, '2010-01-01', '2015-03-31') + 1



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1417126
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse