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»»

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

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, August 18, 2014 9:32 AM
Points: 194, Visits: 1,105
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 @ 2:08 PM
Points: 12,903, Visits: 31,972
two built in functions should get you what you are after:
SELECT YEAR(getdate())



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1360760
Posted Tuesday, September 18, 2012 6:57 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 8:00 AM
Points: 2,433, Visits: 7,501
Ummm. . .

YEAR(GETDATE());

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



Not a DBA, just 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


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1360762
Posted Tuesday, September 18, 2012 7:10 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, August 18, 2014 9:32 AM
Points: 194, Visits: 1,105

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


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: Yesterday @ 7:03 PM
Points: 3,486, Visits: 7,531
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1360770
Posted Tuesday, September 18, 2012 7:23 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, August 18, 2014 9:32 AM
Points: 194, Visits: 1,105
Thanks for giving me solution
Post #1360772
Posted Tuesday, September 18, 2012 7:29 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:25 AM
Points: 5,306, Visits: 9,686
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-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, August 18, 2014 9:32 AM
Points: 194, Visits: 1,105
Bang on:)
Post #1360778
Posted Tuesday, September 18, 2012 5:36 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 9:13 PM
Points: 282, Visits: 751
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: Yesterday @ 4:14 AM
Points: 3,618, Visits: 5,254
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!
Post #1361069
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse