[dba-SQLServer] SQL Server date functions

Mark A Matte markamatte at hotmail.com
Tue Mar 18 13:35:26 CDT 2008


Not sure if it is the best...but a number of the older reporting systems I recently inherited...use DATEPART for month, day, and year...on the fields with date and time....and then add criteria to each part.

Mark A. Matte

http://msdn2.microsoft.com/en-us/library/ms174420.aspx
******************
SELECT DATEPART(m, MyDateTime), DATEPART(d, MyDateTime), DATEPART(yy, MyDateTime)

******************

> Date: Tue, 18 Mar 2008 13:06:38 -0500
> From: Elizabeth.J.Doering at wellsfargo.com
> To: dba-sqlserver at databaseadvisors.com
> Subject: Re: [dba-SQLServer] SQL Server date functions
>
> John,
>
> Select myfield from mytable where mydatefield> getdate()-1
>
> Pulls everything in the last 24 hours.
>
> You could come up with more elegant variations with dateadd or datediff
> to get exactly the correct number of hours.
>
> HTH,
>
>
> Liz
>
>
> Liz Doering
> elizabeth.j.doering at wellsfargo.com
> 612.667.2447
>
>
> This message may contain confidential and/or privileged information. If
> you are not the addressee or authorized to receive this for the
> addressee, you must not use, copy, disclose, or take any action based on
> this message or any information herein. If you have received this
> message in error, please advise the sender immediately by reply e-mail
> and delete this message. Thank you for your cooperation.
>
>
> -----Original Message-----
> From: dba-sqlserver-bounces at databaseadvisors.com
> [mailto:dba-sqlserver-bounces at databaseadvisors.com] On Behalf Of jwcolby
> Sent: Tuesday, March 18, 2008 12:50 PM
> To: 'Discussion concerning MS SQL Server'
> Subject: [dba-SQLServer] SQL Server date functions
>
> I am pulling a TopN kind of thing and marking those records with
> GetDate() which places a date and time in a date field. Now I want to
> pull all the records with today's date. The first problem is that
> GetDate() pulls the date and time. The second problem is what to place
> in the filter. I am not finding a date() kind of thing like you have
> with Access.
>
> How do I pull all records with today's date in a date field (that also
> contains the time)?
>
> John W. Colby
> Colby Consulting
> www.ColbyConsulting.com
>
> _______________________________________________
> dba-SQLServer mailing list
> dba-SQLServer at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/dba-sqlserver
> http://www.databaseadvisors.com
>
>
>
> _______________________________________________
> dba-SQLServer mailing list
> dba-SQLServer at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/dba-sqlserver
> http://www.databaseadvisors.com
>

_________________________________________________________________
Connect and share in new ways with Windows Live.
http://www.windowslive.com/share.html?ocid=TXT_TAGHM_Wave2_sharelife_012008



More information about the dba-SQLServer mailing list