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
 General SQL Server Forums
 New to SQL Server Programming
 "set dateformat dmy" in a function

Author  Topic 

iordanissav
Starting Member

4 Posts

Posted - 2008-01-31 : 06:53:12
I can't create a function which contains SET DATEFORMAT dmy (Greek dateformat). Does it only work in a SP?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-01-31 : 07:28:13
If you use proper datatype as DATETIME or SMALLDATETIME you will never have these issues.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

iordanissav
Starting Member

4 Posts

Posted - 2008-01-31 : 11:15:43
What do you mean? I certainly use datetime vars. I just want to make sure that the dates typed by the users in Greek format, are understood by SQL Server in the stored procedures. I think this has to do with the connection environment. How can I set this in the connection string (or in another place?)
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-01-31 : 11:20:37
A date is nothing more than a sequence number starting with 0 for January 1, 1900 (Microsoft SQL Server).
Today January 31, 2008 is sequence number 39476. The decimal point is number of milliseconds since midnight.

If you use proper datatypes to call the function, you will never have this problem.


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-01 : 10:19:13
1 Use proper DATETIME datatype
2 When expressing dates use universal format yyyymmdd HH:MM:SS

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

iordanissav
Starting Member

4 Posts

Posted - 2008-02-01 : 16:36:53
I understand all of these, but I can't expect users to type a date in a yyyy-mm-dd format. Not even in mm-dd-yyyy format. 11/5/2007 is different than 5/11/2007 but both are correct for SQL server. If I could use [SET DATEFORMAT dmy] then a date entered as 11/5/2007 would actually be May 11, 2007 for SQL Server.

And another thing. Can I use IsDate function in a UDF?
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-02-01 : 16:39:47
what we're saying is that you should use datetime parameters in your app.
don't generate sql like this:
string sql = "select * from myTable where myDateColumn = '" + txtMyDateTime.Text + "'"

date time formatting doesn't have any place in sql server.

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com <- new version out
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2008-02-01 : 17:15:13
I hope you are not using a call to SQL Server to validate user date input.

I can't imagine any modern language that would not have to ability to validate dates.





CODO ERGO SUM
Go to Top of Page

dashingsidds_868
Starting Member

1 Post

Posted - 2009-09-17 : 01:26:35
Hi,

The date format cannot be changed in the function as time dependent activities cannot be performed. It gives the following error while creating or altering the function.

"Invalid use of side-effecting or time-dependent operator in 'SET COMMAND' within a function."

But the date format can be changed in the SP in which you are calling the function and the same format (as mentioned in the SP) will be taken in the function.

Regards,

Dhaval Samaranayeke
Go to Top of Page

ashish908
Starting Member

18 Posts

Posted - 2009-11-27 : 09:58:21
In my case it did not propogate to the function
i had my "Set Dateformat ..." command in the stored procedure
but my function threw the error when it referred to a date that is in conflict with the regional settings

Regards,
Go to Top of Page

jlgervais
Starting Member

3 Posts

Posted - 2011-07-07 : 15:10:11
i understand your problem, personally, i receive all kinds of date format in many forms of input, (i still receive paper data !!!), so i basically worked around using a small test and update

SET DATEFORMAT DMY
UPDATE table SET DateVar= CONVERT(DATETIME,DateVar) WHERE ISDATE(DateVar) = 1

SET DATEFORMAT YMD
UPDATE RI_IMP_UCANNA SET DateVar = CONVERT(DATETIME,DateVar) WHERE ISDATE(DateVar) = 1

this way, i end up with proper datetime values, then i can work with it but you can't use a SET command within a function
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-07-08 : 03:54:01
Note that ISDATE is not fully reliable
http://beyondrelational.com/blogs/madhivanan/archive/2007/09/24/handle-isdate-with-care.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -