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.
| Author |
Topic |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2011-11-08 : 12:15:34
|
I have a front end developer in Domino telling me they can't discern the type of date the are getting...so I proposed a solution.Is there something better than this? Without changing the default settings?DECLARE @d1 date, @d2 date, @d3 dateDECLARE @v1 varchar(10), @v2 varchar(10), @v3 varchar(10)SET @v1 = '31/10/2011'SET @v2 = '10/31/2011'SET @v3 = '2011/10/31'SELECT @v1, @v2, @v3, ISDATE(@v1), ISDATE(@v2), ISDATE(@v3)IF ISDATE(@v1) = 0 SELECT @d1 = CONVERT(date,SUBSTRING(@v1,4,2)+'/'+SUBSTRING(@v1,1,2)+'/'+SUBSTRING(@v1,7,4)) ELSE SELECT @d1 = CONVERT(date,@v1)IF ISDATE(@v2) = 0 SET @d2 = CONVERT(date,SUBSTRING(@v2,4,2)+'/'+SUBSTRING(@v2,1,2)+'/'+SUBSTRING(@v2,7,4)) ELSE SET @d2 = CONVERT(date,@v2) IF ISDATE(@v3) = 0 SET @d3 = CONVERT(date,SUBSTRING(@v3,4,2)+'/'+SUBSTRING(@v3,1,2)+'/'+SUBSTRING(@v3,7,4)) ELSE SET @d3 = CONVERT(date,@v3) SELECT @d1, @d2, @d3 Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Kristen
Test
22859 Posts |
Posted - 2011-11-08 : 12:33:53
|
| "Server: Msg 241, Level 16, State 1, Line 23Conversion failed when converting date and/or time from character string."(during @v3 processing)Probably due to server-settings - which thus may not effect you, unless you move to a different server.With a 31st-of-month SQL may well do the right thing, will your test also work for 1st-February dates?Can the front end pass "31Oct2011" style format perhaps? |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-11-08 : 12:39:47
|
| You can't tell what the date format is from the date itself. You can guess if it has a day greater than 12 but that's all as you have found.I had a pc for file imports (fairly important for end of month billing) - whenever it was rebooted (every few months) the date format got reset to US. Was fine until the 13th when we discovered that all the data it had imported had the wrong date and had to be reprocessed. Funny it never happened after the 12th so that it would error immediately.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2011-11-08 : 12:57:34
|
| yeah, I was told that they 3rd party company (but of course, I have never found 1 YET that was worth a damn) can't convert the date to us...even though we originally GIVE it to them in that format.They then format it to match the person's country format and store it (and I'm sure it's probably varchar..scrubs)So now, I have to look up the person and then convert it based on the persons country code..So that means, I either rewrite all of the sprocs that have date parameters to be varchar..OR create a special sproc called usp_TheBozos_are inTown.sql and just do it for this one applications call (there are many different business with many different technologies all call my sprocs)So...I'm going with the Bozo SprocBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2011-11-08 : 13:12:12
|
| "So...I'm going with the Bozo Sproc"I know you know this, but in case you've overlooked:Presumably, based on the person's-country-format, you could just "fire" a SET DATEFORMAT dmyand then everything downstream will be rosy ...Or perhaps even a SET LANGUAGE for the session created in something that is man-in-the-middle? Get them to connect with ODBC perhaps and set it in the DSN? |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2011-11-08 : 13:37:51
|
quote: Originally posted by X002548 I have a front end developer in Domino telling me they can't discern the type of date the are getting...so I proposed a solution.Is there something better than this? Without changing the default settings?<snip>
I'm assuming you/they cannot use an ISO (or even ANSI) date format? |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2011-11-08 : 14:11:35
|
I'm sure they "could" ... but if did they wouldn't be "bozos" Was "Flipping the Bozo bit" in one of Steve McConnell's books? |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2011-11-08 : 16:22:19
|
quote: Originally posted by Kristen "So...I'm going with the Bozo Sproc"I know you know this, but in case you've overlooked:Presumably, based on the person's-country-format, you could just "fire" a SET DATEFORMAT dmyand then everything downstream will be rosy ...Or perhaps even a SET LANGUAGE for the session created in something that is man-in-the-middle? Get them to connect with ODBC perhaps and set it in the DSN?
I don't like playing with the settings...besides I could get multiple people in all with different formatsI would still need to know the format before, and it would still have to come in as varchar because they can't tell me what the format is...I have to look the person up first to find out what to use..I could then join to the person table, find the country code and use a case statement to format the "date"And I just found out the reason..they have a "Major" release and they can't release anything until after January..HOW do they stay in business?I was doing weekly release not too long agopainfulBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2011-11-08 : 17:49:58
|
"I don't like playing with the settings...besides I could get multiple people in all with different formats"SET DATEFORMAT would just be for the current session though ... so I was thinking you could doCREATE usp_TheBozos_are_inTown @MyDate varchar(24)AS @UserCountry = SomeDeterminationOfPersonsCountry(xxx) IF @UserCountry = 'GB' SET DATEFORMAT dmy IF @UserCountry = 'USA' SET DATEFORMAT mdy EXEC Original_NonBozo_SProc @MyDate If Original_NonBozo_SProc requires a parameter as DATETIME, it will make an implicit conversion if you provide it with a VARCHAR instead, so I reckon you just need to be in control of the conversion algorithm.If you can do it in transport layer then even better as no code changes needed at the database end. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2011-11-09 : 12:35:46
|
Something like SELECT XXX.AKA_ID, P.LAST_NAME, XXX.Expiration_DT AS varchar_Expiration_DT, D.DATE_FORMAT, D.PRIM_COUNTRY_CD , CASE WHEN DATE_FORMAT IS NULL OR DATE_FORMAT IN ('', ' ') THEN CONVERT(date,DATEADD(dd,30,GetDate())) WHEN DATE_FORMAT = 'dd/mm/yyyy' AND ISDATE(SUBSTRING(Expiration_DT,4,2)+'/' +SUBSTRING(Expiration_DT,1,2)+'/' +SUBSTRING(Expiration_DT,7,4))=1 THEN CONVERT(date, (SUBSTRING(Expiration_DT,4,2)+'/' +SUBSTRING(Expiration_DT,1,2)+'/' +SUBSTRING(Expiration_DT,7,4))) WHEN ISDATE(Expiration_DT) = 1 THEN CONVERT(date,Expiration_DT) ELSE CONVERT(date,DATEADD(dd,30,GetDate())) END AS Expiration_DT FROM (SELECT 'X002548' AS AKA_ID, '05/06/2011' AS Expiration_DT UNION ALL -- Good USA Date SELECT 'X124192' AS AKA_ID, '06/05/2011' AS Expiration_DT UNION ALL -- Good GBR Date SELECT 'X046984' AS AKA_ID, '2011/05/06' AS Expiration_DT UNION ALL -- Good JPN Date SELECT 'X169267' AS AKA_ID, '13/42/9999' AS Expiration_DT UNION ALL -- Bad Date SELECT 'X123456' AS AKA_ID, '05/06/2011' AS Expiration_DT) AS XXX -- No Country Code LEFT JOIN PERSON P ON XXX.AKA_ID = P.AKA_ID LEFT JOIN DATE_FORMATS D ON P.PRIM_COUNTRY_CD = D.PRIM_COUNTRY_CDResults inAKA_ID LAST_NAME varchar_Expiration_DT DATE_FORMAT PRIM_COUNTRY_CD Expiration_DT------- -------------------- --------------------- ----------- --------------- -------------X002548 KAISER 05/06/2011 mm/dd/yyyy USA 2011-05-06X124192 NELSON 06/05/2011 dd/mm/yyyy GBR 2011-05-06X046984 HUETZ 2011/05/06 yyyy/mm/dd JPN 2011-05-06X169267 MCLAUGHLIN 13/42/9999 mm/dd/yyyy USA 2011-12-09X123456 SCHOCH 05/06/2011 NULL NULL 2011-12-09(5 row(s) affected) Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2011-11-09 : 14:21:09
|
Change [DATE_FORMAT] to be "dmy" style, and you can then do:DECLARE @strDateFormat varchar(10)SELECT @strDateFormat = 'dmy' -- i.e. set programaticallySET DATEFORMAT @strDateFormatSELECT [UK Style]=ISDATE('31/12/2000'), [USA Style]=ISDATE('12/31/2000') |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2011-11-10 : 17:57:08
|
| would a calendar table help out? with CountryCode as one of the fields?If you don't have the passion to help people, you have no passion |
 |
|
|
|
|
|
|
|