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: Thursday, May 09, 2013 7:04 AM
Points: 157, Visits: 833
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: Today @ 3:33 PM
Points: 11,648, Visits: 27,768
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: Thursday, May 16, 2013 9:16 AM
Points: 2,236, Visits: 6,486
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: Thursday, May 09, 2013 7:04 AM
Points: 157, Visits: 833

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


SSC Eights!

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

Group: General Forum Members
Last Login: Yesterday @ 4:59 PM
Points: 960, Visits: 1,924
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.
Please don't trust me, test the solutions I give you before using them.
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: Thursday, May 09, 2013 7:04 AM
Points: 157, Visits: 833
Thanks for giving me solution
Post #1360772
Posted Tuesday, September 18, 2012 7:29 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 2:18 PM
Points: 4,434, Visits: 7,219
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: Thursday, May 09, 2013 7:04 AM
Points: 157, Visits: 833
Bang on:)
Post #1360778
Posted Tuesday, September 18, 2012 5:36 PM
SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Monday, May 13, 2013 5:00 PM
Points: 172, Visits: 446
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


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 7:24 PM
Points: 2,346, Visits: 3,192
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.



No loops! No CURSORs! No RBAR! Hoo-uh!

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?

Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1361069
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse