Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Is there a better way of selecting information for a dat
Message
De
25/04/2007 08:01:11
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
 
À
25/04/2007 07:55:48
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 2005
Divers
Thread ID:
01219598
Message ID:
01219698
Vues:
11
>>>Consider the following select statement:
>>>
>>>SELECT	[Id]
>>>FROM	[MyTable]
>>>WHERE	YEAR(DateField) = YEAR(@DateField) AND
>>>	MONTH(DateField) = MONTH(@DateField) AND
>>>	DAY(DateField) = DAY(@DateField)
>>>
>>>
>>>I want to select the records that have the same date as the parameter, but I do not care about the time part of the field or parameter.
>>>Is there another (better) way of doing this?
>>>
>>>Thanks,
>>>Einar
>>
>>
>>DECLARE @myDate datetime
>>set @myDate = convert(varchar(8),@DateField,112)
>>
>>select [id] from [myTable] WHERE DateField between @myDate AND @myDate + 1
>>
Cetin
>
>Cetin,
>
>this solution has a tiny bug : the next day midnight precise is selected also, and Murphy is never far away ...
>
>As a solution use Dateadd(second,86399, @myDate) as enddate. 86399 = 86400 seconds in a day minus 1. But even here, the last millisecond of the day isn't selected :-(

Pascal,
I have already noticed and updated:) Check first reply.

PS: Dateadd() solutions are somewhat ugly because SQL server cannot resolve less than 3 milliseconds. ie: To SQL server 2000,1,1 23:59:59.999 is 2000,1,2 0:0:0
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform