Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select all data from a specific year (2011)

Author  Topic 

PHenry
Starting Member

10 Posts

Posted - 2012-02-15 : 07:37:29
Hello,

I use the following Sql statement to pull all my data from the table:

SELECT jobDate, jobTime, Temp, Pressure
FROM T2

I am trying to pull out only the data that includes the date with 2011. The date format is dd/mm/yyyy.

I have tried the following but it fails to function:

SELECT jobDate, jobTime, Temp, Pressure
FROM T2
WHERE (YEAR(jobDate) = [2011])

Can anyone advise how to extract all JobDates that only include a specific year, e.g. 2011.

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-02-15 : 07:39:57
will this work?

SELECT jobDate, jobTime, Temp, Pressure
FROM T2
WHERE YEAR(jobDate) = 2011

If not: what is the DATATYPE of the column jobDate?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

PHenry
Starting Member

10 Posts

Posted - 2012-02-15 : 08:53:37
Hello,

That seemed to have worked:

WHERE (YEAR(jobDate) = 2011)

Do you know how i could pull out only the data with the date 28/01/2011?

Thanks

quote:
Originally posted by webfred

will this work?

SELECT jobDate, jobTime, Temp, Pressure
FROM T2
WHERE YEAR(jobDate) = 2011

If not: what is the DATATYPE of the column jobDate?


No, you're never too old to Yak'n'Roll if you're too young to die.

Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-02-15 : 08:59:12
It depends on
- what is the datatype of that column and
- is there a time part stored in the table to consider or not.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

PHenry
Starting Member

10 Posts

Posted - 2012-02-15 : 09:07:10
Because the data is imported from a txt file i have two columns in my database. One for Date which has a 'Date' data type. And another column called Time which has a 'time(7)' data type.
I have already tried: WHERE (jobDate = 28/01/2011) however it comes up with the SQL execution error: "Operand type clash: date is incompatible with int' which i dont understand since where am i using the integer?

Update - I just tried: WHERE(jobDate = '28/01/2011')and i also get an error which is "Conversion failed when converting date and/or time from character string'.

quote:
Originally posted by webfred

It depends on
- what is the datatype of that column and
- is there a time part stored in the table to consider or not.


No, you're never too old to Yak'n'Roll if you're too young to die.

Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-02-15 : 09:16:25
WHERE jobDate = '20110128'


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -