Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

how to get current year Expand / Collapse
Author
Message
Posted Tuesday, September 18, 2012 6:55 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, February 3, 2016 3:05 AM
Points: 214, Visits: 1,341
Table contains Orderdate Column which contains years starting from 2001 to 2010. i need to get currect year using any of the build in function or query can any body guide me

Post #1360758
Posted Tuesday, September 18, 2012 6:56 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 8:10 AM
Points: 13,970, Visits: 36,244
two built in functions should get you what you are after:
SELECT YEAR(getdate())



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Post #1360760
Posted Tuesday, September 18, 2012 6:57 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 1:45 PM
Points: 2,482, Visits: 8,197
Ummm. . .

YEAR(GETDATE());

http://msdn.microsoft.com/en-us/library/ms186313.aspx



Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
Post #1360762
Posted Tuesday, September 18, 2012 7:10 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, February 3, 2016 3:05 AM
Points: 214, Visits: 1,341

Sorry for not giving enough information

CREATE TABLE #TestBasedOnYear
(
OrderDate DATETIME,
TotalDue MONEY
)

INSERT INTO #TestBasedOnYear(OrderDate,TotalDue)
SELECT '2001-07-01 00:00:00.000','27231.5495'
UNION ALL
SELECT '2001-07-01 00:00:00.000','1716.1794'
UNION ALL
SELECT '2001-07-01 00:00:00.000','19005.2087'
UNION ALL
SELECT '2002-01-01 00:00:00.000','36096.7069'
UNION ALL
SELECT '2002-01-01 00:00:00.000','556.2026'
UNION ALL
SELECT '2003-01-01 00:00:00.000','47633.1875'
UNION ALL
SELECT '2003-01-01 00:00:00.000','46.0291'
UNION ALL
SELECT '2004-01-01 00:00:00.000','9128.8743'
UNION ALL
SELECT '2004-01-01 00:00:00.000','52154.9009'
UNION ALL
SELECT '2004-01-01 00:00:00.000','38.7783'
UNION ALL
SELECT '2004-01-01 00:00:00.000','4650.7326'

Out Put Should look like

TotalDueForTheCurrentYear OverAllTotalDue

65973.2861 198258.3498
Post #1360766
Posted Tuesday, September 18, 2012 7:18 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 8:10 AM
Points: 6,377, Visits: 14,132
Two options:

SELECT TotalDueForTheCurrentYear = SUM(CASE WHEN YEAR(OrderDate) = YEAR(GETDATE()) THEN TotalDue ELSE 0 END),
OverAllTotalDue = SUM(TotalDue)
FROM #TestBasedOnYear

SELECT TotalDueForTheCurrentYear = SUM(CASE WHEN OrderDate >= DATEADD( yyyy, DATEDIFF(yyyy,0,GETDATE()), 0) AND OrderDate < DATEADD( yyyy, DATEDIFF(yyyy,0,GETDATE()) + 1, 0) THEN TotalDue ELSE 0 END),
OverAllTotalDue = SUM(TotalDue)
FROM #TestBasedOnYear




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Post #1360770
Posted Tuesday, September 18, 2012 7:23 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, February 3, 2016 3:05 AM
Points: 214, Visits: 1,341
Thanks for giving me solution
Post #1360772
Posted Tuesday, September 18, 2012 7:29 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 8:50 AM
Points: 6,216, Visits: 12,512
Of course you do, because it's not 2004 any more.

Here's a third alternative:

WITH Totals(Yr, TotalDueForTheCurrentYear, OverAllTotalDue) AS (
SELECT
YEAR(OrderDate)
, SUM(TotalDue) OVER(PARTITION BY YEAR(OrderDate))
, SUM(TotalDue) OVER(PARTITION BY 1)
FROM
#TestBasedOnYear
)
SELECT DISTINCT
TotalDueForTheCurrentYear
, OverAllTotalDue
FROM
Totals
WHERE
Yr = 2004

John
Post #1360777
Posted Tuesday, September 18, 2012 7:30 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, February 3, 2016 3:05 AM
Points: 214, Visits: 1,341
Bang on:)
Post #1360778
Posted Tuesday, September 18, 2012 5:36 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Sunday, January 31, 2016 7:36 PM
Points: 354, Visits: 964
John Mitchell, I see that the solution works. It's nice. I understand CTE and 'PARTITION BY YEAR(OrderDate)' etc., but will you please explain why 'PARTITION BY 1'?
Post #1361062
Posted Tuesday, September 18, 2012 6:23 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Thursday, December 24, 2015 4:18 AM
Points: 3,977, Visits: 6,429
I can answer for John.

SUM OVER requires a PARTITION clause. In this case, since you're partitioning over the entire rowset, anything will do, like 1 or NULL.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1361069
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse