Formatting dates with SQL Report Server - sql-server

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.

Related

How do I Format a String Date in SSRS?

Using Report Builder,
In a Matrix for a report I'm creating:
I have dates in my data the are are "FullMonthName-Year"
I am trying to get them to a report as "AbbreviatedMonthName-Year"
Example:
'January-2020' turns into 'Jan-2020'
'February-2021' turns into 'Feb-2021'
thanks in advance
Within the Text-Box Properties, I have been trying to mess with the Format function but can't seem to get it right.
ex:
=Format(Fields!labelDt.Value, "MM-YYYY") just gives me "MM-YYYY"
Depending your value type you can use formatting for dates and expression for strings
For dates you can set the format string to "MMM-yyyy"
For strings you can use the expression
=Left(Fields!labelDt.Value,3) & "-" & Right(Fields!labelDt.Value,4)

SQL Server not accepting YYYY-MM-DD

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?

Change SQL Server datetime/date format to dd/mm/yyyy

I'd like to change display format of all dates/datetimes among entire instance/database (whichever is possible).
I tried changing default language for the instance and for single users and it doesn't work. It always displays YYYY-MM-DD. Can this be changed without messing with the code to always include FORMAT function?
Use the following function and write the format you want
SELECT FORMAT(GetDate(), 'yyyy-MM-dd')

SSRS date conversion works on preview but not on deployment server

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.

How do I control the format in which MS-SQL Server returns datetime values?

Specifically, I wish to get the date format in a pure (ISO) format:
YYYY-MM-DD HH:mm:ss
I'm looking for a SET command or something that I can use.
I do not wish to rely on the culture setting of the server.
Note: I'm interested in the string format in which dates are returned, not entered.
To change the default format you need to add a new language (sp_addlanguage), set it's date format, then set the default language to it. More details can be found on this old technet article.
If you don't want to do that, then you can change it "per connection" using SET DATEFORMAT.
And if you don't want to do that, then you can use CONVERT to convert it to the relevent format string in each query.
Here's a handy article I found:
http://www.sqljunkies.ddj.com/Article/6676BEAE-1967-402D-9578-9A1C7FD826E5.scuk
What you're looking for is:
CONVERT(datetime,'05/08/2004',120)
This will return a date in the format you're after yyyy-mm-dd hh:mi:ss(24h) format (ODBC canonical, not ISO).
the formatting of the datetime depends on the client. You can use the convert function to display it as a string in the format of your choice.

Resources