OK, am a bit confused, I have an expression that turns the date into a UK format and with 3 years taken away, the expression is:
=cdate(format(DateAdd(DateInterval.Year, -3, now),"dd/MM/yyyy"))
This works happily when previewing the SSRS report in VS2015, however when I run it on the deployment server I get this message:
The DefaultValue expression for the report parameter ‘StartDate’ contains an error: Conversion from string "20/10/2013" to type 'Date' is not valid. (rsRuntimeErrorInExpression)
What is wrong with the expression to bring this error? and surely the same error should appear in the preview to?
Thanks
This will be due to regionalisation differences between your dev environment and your server. One of them will be dd/MM/yyyy and the other MM/dd/yyyy. Where possible pass date types or unambiguous string formats such as yyyy/MM/dd.
Related
My local SQL Server 2016 setup at work decided not to accept the YMD date format after going through a reinstall. For example, the following query, that was and still is accepted in my coworkers' setups:
SELECT "id"
FROM test.dbo.tabEmp
WHERE "DateAdmission" <= '2021-12-31' AND "DateAdmission">= '2021-12-30' `
When I try to run it I see this:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
however, if i rewrite the dates as 2021-31-12 and 2021-12-30, in the YYYY-DD-MM format, they are accepted.
I can't really convert or format it since the sql queries in our system are numerous and done so in a way that it would be nearly impossible to. Is there something that can be done? I tried changing windows' Date format but to no avail.
For the datetime and smalldatetime data types the format yyyy-MM-dd is not unambiguous (note that it is for the newer date and time data types). If you are not American, the date will very likely be interpreted as yyyy-dd-MM, and as there are not 31 months in the year you get an error.
For SQL Server, the formats that are unambiguous regardless of data type and language setting are yyyyMMdd and yyyy-MM-ddThh:mm:ss.nnnnnnn; ideally if you are using string literals use one of those formats as you can never get an error (unless you legitimately have an invalid date).
Otherwise you can explicitly CONVERT your value with a style code:
SELECT CONVERT(datetime, '2021-12-31', 126);
It seems that your new DB instance picked up a new language after the reinstallation.
The current language setting determines the language used on all system messages, as well as the date/time formats to use.
The date format setting affects the interpretation of character strings as they are converted
to date values for storage in the database. It does not affect the display of date data type values
that are stored in the database or the storage format.
You can run the following statement to return the language currently being used:
SELECT ##LANGUAGE;
This will tell us what the current language is and the date format (as well as a few other things):
DBCC USEROPTIONS;
Date format is modifiable via the following statements:
SET LANGUAGE us_english;
SET DATEFORMAT YMD;
Here is a good article on the subject: How to Change the Current Date Format in SQL Server (T-SQL)
It is also possible to modify SQL Server instance default language globally, once and for all: How to change default language for SQL Server?
Im trying to download some data from web services using Web Service Task component in SSIS. Everything works fine when I insert required Input value manually. The required input value have to be in dateTime type but it only works when I munually write datetime in YYYY-MM-DD or YYYY-MM-DD HH:MM format.
But that of course is not what I wanted to do.
So I created a variable with DateTime data type with simple expression:
(DT_DATE) GETDATE()
But that gives me format DD.MM.YYYY HH:MM instead of YYYY-MM-DD or YYYY-MM-DD HH:MM.
When I execute the package it goes into error message:
[Web Service Task] Error: An error occurred with the following error message: "Microsoft.SqlServer.Dts.Tasks.WebServiceTask.WebserviceTaskException: Could not execute the Web method. The error is: Method 'ProxyNamespace.Service.Method' not found..
at Microsoft.SqlServer.Dts.Tasks.WebServiceTask.WebMethodInvokerProxy.InvokeMethod(DTSWebMethodInfo methodInfo, String serviceName, Object connection)
at Microsoft.SqlServer.Dts.Tasks.WebServiceTask.WebServiceTaskUtil.Invoke(DTSWebMethodInfo methodInfo, String serviceName, Object connection, VariableDispenser taskVariableDispenser)
at Microsoft.SqlServer.Dts.Tasks.WebServiceTask.WebServiceTask.executeThread()".
I think its only because my variable is in incorrect format. Any suggestions how to change it to desired dateTime format while data type of variable remains with DateTime option ? Thanks !
EDIT :
So I managed to solve the problem with the DateTime format, I simply changed the date format on my server to YYYY-MM-DD in the Windows environment (Control Panel - Region - Formats) and it also changed in SSIS.
But the problem still persists, when I check the box in the input section that I use a Variable and select my variable in the correct format as a DateTime with the correct value YYYY-MM-DD, I still get an error message and nothing happens.
Do you have any other solutions to my problem?
Have an instance of SQL Server 2012 that appears to correctly interpret string literal dates whose formats are not listed in the docs (though note these docs are for SQL Server 2017).
Eg. I have a TSV with a column of dates of the format %d-%b-%y (see https://devhints.io/datetime#date-1) which looks like "25-FEB-93". However, this throws type errors when trying to copy the data into the SQL Server table (via mssql-tools bcp binary). Yet, when testing on another table in SQL Server, I can do something like...
select top 10 * from account where BIRTHDATE > '25-FEB-93'
without any errors. All this, even though the given format is not listed in the docs for acceptable date formats and it apparently also can't be used as a castable string literal when writing in new records. Can anyone explain what is going on here?
the given format is not listed in the docs for acceptable date formats
That means it's not supported, and does not have documented behavior. There's lots of strings that under certain regional settings will convert due to quirks in the parsing implementation.
It's a performance-critical code path, and so the string formats are not rigorously validated on conversion. You're expected to ensure that the strings are in a supported format.
So you may need to load the column as a varchar(n) and then convert it. eg
declare #v varchar(200) = '25-FEB-93'
select convert(datetime,replace(#v,'-',' '),6)
Per the docs format 6 is dd mon YY, but note that this conversion "works" without replacing the - with , but that's an example of the behavior you observed.
Warning: Classic ASP Ahead. :)
I'm working on a legacy Classic ASP application and I'm running into an oddity with an ADODB.Recordset object.
I have a SQL2012 database table containing a particular field. Let's call it AnnoyingField. Its datatype in SQL is 'date'.
The ASP opens an ADODB.Recordset with a SELECT on the table to collect the fields, then does some looping to do its work:
For each Field in rs.Fields
typeid = rs(Field.Name).Type
'do stuff based on type
For some reason, the Type for AnnoyingField is coming back as 202 (nvarchar) rather than one of the expected types for date (133 or even 7). This is causing some issues further in the code.
I tested with another field of 'datetime' type and the Recordset code returned the expected Type for a datetime field.. 135.
Anyone have an idea why the 'date' fields are returning as an nvarchar?
Changing the database fields from date to datetime in this case might not be possible, even though it might be the logical path to take to get expected data.
Date fields are newer than your version of ADODB. So it doesn't understand what it's getting.
You may be able to cheer it up by using
select convert(datetime, AnnoyingField) from CrazyFuturisticTable
You may also get the correct result if you upgrade your ADODB version to 2.8 and/or connect using the SQL Native Client. Obviously, I haven't tried this, because I live in 2014.
If you keep it a little simpler, cut ADO out of the picture and use isdate and "convert date" and vartype.
For each Field in rs.Fields
Field=rs(Field.Name)
if isdate(Field) then Field=cdate(Field) ' just in case
typeid = VarType(Field)
'do stuff based on type
Vartype
http://www.w3schools.com/vbscript/func_vartype.asp
ADO .type can report more "types"
http://www.w3schools.com/ado/prop_para_type.asp
But I think what vartype can offer should help people in 95% of cases.
Word of warning with cdate, depending on what the server locale is set too or your Session.LCID (in your code) is set will determine what format the date will be formatted to. Shouldn't be a problem for most people but obviously test to see if you get the expected result.
I am using Classic ASP / VBScript and have found that for SQL2012 using SQL Native driver ("Provider=SQLNCLI11"), and the ADO module provided by Windows (presumably Vista or newer [mine is Windows Servr2012 R2 - the version [MyDbConn.version] shows as 6.3) this now works again.
I had been looking for / expecting the Field.Type to be adDate [7], but am actually getting adDBDate [133]
I'm trying to format the current date according to the generic "2012-09-04 10:20:12 AM" format but somehow the the "AM" part is always missing.
I'm using the expression "..." & Format(CDate(Now), "yyyy-MM-dd hh:mm:ss tt") to format the date and append it to some text. That emits the warning reproduced below but prints out the date correctly (except for the AM/PM designator).
[rsRuntimeErrorInExpression] The Value expression for the textrun
‘EmissionDate.Paragraphs[0].TextRuns[0]’ contains an error: Input string
was not in a correct format.
What am I doing wrong here?
Note: I'm aware of this SO post and SQL Server query backed solutions but I'd like to use the built-in functions as much as possible.
I found this article and maybe it can solve your problem. I know you are already using Format(CDate) but since you are using (Now), witch is a datetime variable, maybe you should use FormatDateTime() instead of Format().
Try this as well:
Instead of: Format(CDate(Now), "yyyy-MM-dd hh:mm:ss tt")
Use: Format(CDate(Now), "yyyy-MM-dd hh:mm:ss am/pm")
Link:Working with Dates in Reporting Services
As per my previous comment, the report's Language property was referencing a culture for which no AM/PM designator was defined. Changing it to en-US presented the AM/PM designator as expected.