Tag: "just code"

DATETIME Spalten gruppieren und sortieren

Posted on Jul 13, 2004 von in SQL Server
SELECT
 DATEADD(day, 0 , DATEDIFF(day, 0, MsgDateReceived)) AS "Datum"
 , COUNT(*) AS Anzahl
FROM mails_header
GROUP BY
 DATEADD(day, 0 , DATEDIFF(day, 0, MsgDateReceived))
ORDER BY
 "Datum" DESC 

GETDATE() und Julianisches Datum

Posted on Jul 13, 2004 von in SQL Server
SELECT
 YEAR(GETDATE())*1000
 +
 DATEPART(y,GETDATE()) AS the_date

the_date    
----------- 
2004182

(1 row(s) affected)

Danke an Jonathan van Houtte!

Das Alter einer Person

Posted on Jul 13, 2004 von in SQL Server
DECLARE @d DATETIME
SET @d = '23.07.1968'
SELECT
     DATEDIFF(yyyy,CAST(@d AS DATETIME),GETDATE()) 
     - 
     (CASE WHEN DATEADD(yyyy,DATEDIFF(yyyy,CAST(@d AS DATETIME),GETDATE()),CAST(@d AS DATETIME)) > 
     GETDATE() THEN 1 ELSE 0 END)

            
----------- 
35

(1 row(s) affected)

Indexes mit Tabellen und Filegroups mappen

Posted on Jul 13, 2004 von in SQL Server
SELECT      
 CAST(OBJECT_NAME(sysind.id) AS CHAR(20)) AS TableName
 , CAST(sysind.name AS CHAR(30)) AS IdxName
 , CAST(sysfg.groupname AS CHAR(10)) AS GroupName
FROM 
 sysindexes  sysind    
INNER JOIN 
 sysfilegroups sysfg
ON 
 sysind.groupid = sysfg.groupid
 INNER JOIN 
  sysobjects sysobj
 ON 
  sysind.id = sysobj.id
WHERE 
 sysobj.xtype <> 'S' 
AND 
 sysind.name NOT LIKE '_WA%' 
ORDER BY
 TableName

Erster und letzter Tag eines Jahres

Posted on Jul 13, 2004 von in SQL Server
DECLARE @dt DATETIME
SET @dt = GETDATE()
SELECT
CAST(CAST(YEAR(@dt) AS CHAR(4))+'0101' AS DATETIME)
AS Jahresanfang
, CAST(CAST(YEAR(@dt) AS CHAR(4))+'-12-31T23:59:59.997' AS DATETIME)
AS Jahresende

Jahresanfang Jahresende
------------------------------------------------------ ------------------------
2004-01-01 00:00:00.000 2004-12-31 23:59:59.997

(1 row(s) affected)

Ganze Geschichte »

Ergebnis eines Dynamischen SQL Statements nach Ausführung weiterverwenden

Posted on Jul 13, 2004 von in SQL Server
DECLARE @stmt nvarchar(4000)
DECLARE @rowcount bigint
DECLARE @table nvarchar(255)
SET @table = 'authors'
SELECT @stmt = 'SELECT @count = COUNT(*) FROM ' + @table
EXEC sp_executesql @stmt, N' @count bigint output', @rowcount OUTPUT
IF @rowcount > 0
     BEGIN
         SELECT @rowcount AS Anzahl
     END
RETURN 

Anzahl               
-------------------- 
23

(1 row(s) affected)

Der Datumsbereich des SQL Servers

Posted on Jul 13, 2004 von in SQL Server
Select
Cast(-53690 As DateTime) As 'Erstes Datum'
, Cast(2958463.999999999 As DateTime) As 'Letztes Datum'

Erstes Datum Letztes Datum
------------------------- -------------------------
1753-01-01 00:00:00.000 9999-12-31 23:59:59.997

(1 row(s) affected)

Ganze Geschichte »

Anzahl von Tagen zwischen zwei Daten

Posted on Jul 12, 2004 von in SQL Server
USE pubs
GO
SELECT
DATEDIFF(DAY, pubdate, GETDATE()) AS Anzahl_Tage
FROM
titles
GO

Anzahl_Tage
-----------
4761
4764
4743
4751
4764
4755
1418
4743
3665
1418
4630
4758
4646
4761
4761
4630
4761
4761

(18 row(s) affected)