Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Help: How to get the date 01/01/2000

Author  Topic 

Nav522
Starting Member

27 Posts

Posted - 2010-02-15 : 15:38:25
Hi Folks,
I have this procedure which has startdate and enddate as parameteres and my requirement is to get rid of this parameter and include the logic so that it looks for the date starting from 01/01/2000 until previous month ending i.e Jan 31. Thanks a lot

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2010-02-15 : 19:56:25
The generic date literal takes the form of: yyyy-mm-dd

Anything else is supject to local assumptions about month versus day precedence. To get your date you use '2000-01-01'.

=======================================
Few things are harder to put up with than the annoyance of a good example. (Mark Twain)
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-02-15 : 21:01:29
This maybe??
where @startdate > cast("20000101" as datetime) and @enddate <=
DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-02-15 : 22:59:12
[code]
where datecol >= '20000101'
and datecol < dateadd(month, datediff(month, 0, getdate()), 0)
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-16 : 00:46:23
quote:
Originally posted by Nav522

Hi Folks,
I have this procedure which has startdate and enddate as parameteres and my requirement is to get rid of this parameter and include the logic so that it looks for the date starting from 01/01/2000 until previous month ending i.e Jan 31. Thanks a lot


Do you mean change it to use a hardcoded values from parameter based logic. Can I ask reason for this change?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-02-16 : 02:18:39
"The generic date literal takes the form of: yyyy-mm-dd

Anything else is supject to local assumptions about month versus day precedence. To get your date you use '2000-01-01'.
"

@Bustaz Kool: you need to leave the hyphens out. The only date that will be implicitly converted, on all server settings for locale, is 6 or 8 digits (and 6 digits is open to Y2K interpretation, so 8 digits is preferred)

Thus: yyyymmdd or '20010101'

Simple example:

SET DATEFORMAT DMY

SELECT CONVERT(datetime, '19991231')
SELECT CONVERT(datetime, '1999-12-31')
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-02-16 : 02:33:44
OK ... there are others ... but not 8 digits plus punctuation

'31 Dec 1999'
{d '1999-12-31'}
'1999-12-31T01:02:03.456'

BOL says that "ISO 8601 Numeric '1998-02-23'" is not DATEFORMAT dependant, but that is simply not the case. Although BOL qualifies that in the text that the "T" and Time part must be used in ISO 8601

BOL says "Unseparated String Format
You can specify date data as an unseparated string. The date data can be specified by using four, six, or eight digits, an empty string, or a time value without a date value.

The SET DATEFORMAT session setting does not apply to all-numeric date entries, such as numeric entries without separators. The six-digit or eight-digit strings are always interpreted as ymd. The month and day must always be two digits.

This is the valid un-separated string format: [19]960415

A string of only four digits is interpreted as the year. The month and date are set to January 1. When you specify only four digits, you must include the century.
"

I think it is a crying shame that SQL Server is so liberal with its string dates, as there must be shedloads of code out that that works OK because of server locale - and if that ever needs to be moved (company merger, for example) its going to be a nightmare.

I wonder if Upgrade Advisor moans about that?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-16 : 02:37:49
quote:
Originally posted by Kristen

OK ... there are others ... but not 8 digits plus punctuation

'31 Dec 1999'
{d '1999-12-31'}
'1999-12-31T01:02:03.456'

BOL says that "ISO 8601 Numeric '1998-02-23'" is not DATEFORMAT dependant, but that is simply not the case. Although BOL qualifies that in the text that the "T" and Time part must be used in ISO 8601

BOL says "Unseparated String Format
You can specify date data as an unseparated string. The date data can be specified by using four, six, or eight digits, an empty string, or a time value without a date value.

The SET DATEFORMAT session setting does not apply to all-numeric date entries, such as numeric entries without separators. The six-digit or eight-digit strings are always interpreted as ymd. The month and day must always be two digits.

This is the valid un-separated string format: [19]960415

A string of only four digits is interpreted as the year. The month and date are set to January 1. When you specify only four digits, you must include the century.
"

I think it is a crying shame that SQL Server is so liberal with its string dates, as there must be shedloads of code out that that works OK because of server locale - and if that ever needs to be moved (company merger, for example) its going to be a nightmare.

I wonder if Upgrade Advisor moans about that?


You have made a good point here Kristen
I think this is something that needs some serious attention

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-02-16 : 02:41:52
Its another of my wishes ... that SQL had a STRICT mode, or LINT, so that it would warn about such usage in development.

I discovered that SET LANGUAGE also changes date interpretation ... so a user may be added to the database, set their default language, and Bang! all the string dates fall over in a heap
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-16 : 02:46:05
quote:
Originally posted by Kristen

Its another of my wishes ... that SQL had a STRICT mode, or LINT, so that it would warn about such usage in development.

I discovered that SET LANGUAGE also changes date interpretation ... so a user may be added to the database, set their default language, and Bang! all the string dates fall over in a heap


yeah..thats also another possibility.
so it seems best for us to to impose restriction to use the format always as ccyymmdd by means of coding standards whenever somebody needs to pass values like this

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -