Posts

Showing posts from October, 2014

SQL Server Functions

SQL Server Functions -- PARSE     SELECT PARSE ( 'Monday, 13 December 2010' AS date USING 'en-GB' ) AS OrderDate , PARSE ( '£345.98' AS money USING 'en-GB' ) AS OrderAmount -- TRY_PARSE     SELECT TRY_PARSE ( 'Monday, 32 December 2010' AS date USING 'en-US' ) AS OrderDate , TRY_PARSE ( '£345.98' AS money USING 'en-US' ) AS OrderAmount -- TRY_CONVERT     SELECT TRY_CONVERT ( float , '1.2' ), TRY_CONVERT ( int , 'One' )     -- DATEFROMPARTS     SELECT DATEFROMPARTS ( 2010 , 12 , 31 ) --DATETIMEFROMPARTS     SELECT DATETIMEFROMPARTS ( 2010 , 12 , 31 , 23 , 59 , 59 , 0 ) --SMALLDATETIMEFROMPARTS     SELECT SMALLDATETIMEFROMPARTS ( 2010 , 12 , 31 , 23 , 59 ) --DATETIME2FROMPARTS     SELECT DATETIME2FROMPARTS ( 2010 , 12 , 31 , 23 , 59 , 59 , 1 , 7 ) --TIMEFROMPARTS     SELE...

Windows Function - How to Calculate Running Total

Windows Function - How to Calculate Running Total   Query used:   -- Create a view that sums sales per city per year           CREATE VIEW CitySalesByYear             AS     SELECT c . City , YEAR ( h . OrderDate ) OrderYear , SUM ( d . OrderQuantity ) OrderQuantity FROM SalesOrderHeader h JOIN SalesOrderDetail d ON d . SalesOrderNumber = h . SalesOrderNumber JOIN Customers c ON h . CustomerKey = c . CustomerKey GROUP BY c . City , YEAR ( h . OrderDate )       GO           -- View the data in the view     SELECT * FROM CitySalesByYear ORDER BY City , OrderYear       GO           -- Use OVER to show a running total by year     SELECT City , OrderYear , OrderQuan...