Display date in MM-dd-yyyy format SSRS report - sql-server

I have a SQL query: select ModifiedDate from Person.Person and this returns the date as 2/24/1998 12:00:00 AM
I'm trying to display this in MM/dd/yyyy format in SSRS report. I have used the expression =Format(Fields!ModifiedDate.Value,"MM/dd/yyyy")
But, still it's coming as 2/24/1998 12:00:00 AM
I want to display this as 2/24/1998.
How can I do this?

I would recommend using the format codes:
Right click - properties on the cell, select format, click the ellipsis "...", and you can see the date formats from there. This will be converted into a date code when you OK the dialog. This is useful as it sets the date in the fomat the user wants to see it in.
To convert the data within SSRS and not the data source you could try using something like:
=Format(Cdate(Fields!ModifiedDate.Value),"dd/MM/yyyy")
Another sample for you without Cdate:
=Format(Fields!ModifiedDate.Value,"dd/MM/yyyy")

There is a special function for formatting Dates in SSRS:
=FormatDateTime(Fields!ModifiedDate.Value, DateFormat.ShortDate)
It will return your date the way that you want it.

As per your question,
To convert the date in SSRS you should try like this,
=FORMAT(Fields!ModifiedDate.Value,"MM-dd-yyyy")
Output would be,
12-06-2010 -- 12(Month)-06(Date)-2010(Year)

Set the language of the entire Report to the language of the country the report is going to be run in.
For example for US it's en_US and for Australia it's en_AU.
Simple !

If you want to get date in format of 'MM/DD/YYYY' use the following query andand you have to convert in varchar datatype.
select CONVERT(varchar(20),GETDATE(),101)

I had this problem and i solve that by this code :
FormatDateTime('MM/dd/yyyy',ADOqueryname.fieldbyname('ModifiedDate').AsDateTime);

I did two things to make it work:
1st, In Data set property Query - select CONVERT(varchar(20),ModifiedDate,101) from Person.Person
2nd, In expression - =Format(Fields!ModifiedDate.Value,"dd/MM/yyyy")
This worked for me.Thanks for the hints.

You can use:
- FormatDateTime(Fields!ModifiedDate.Value, 2)
or:
- FormatDateTime(Fields!ModifiedDate.Value, DateFormat.ShortDate)
You will get the same result.

Related

Max aggregate function with autonumber and date field in access

I have ran into a problem recently. One of the tables I have in MS Access database contains multiple fields but the main focus is on an autonumber field and date field. When I try to apply Select query with Max(autonumber) function and where clause containing date field, the wrong value or rather empty column is shown. as soon as i remove the date field from the where clause, the value is returned fine. My query is attached. Any help would be appreciated.
SELECT MAX(serial) AS Expr1
FROM coaDetails
WHERE (((coaDetails.[title])='CLAIMS')AND ((coaDetails.[dates])=#04/08/2018#));
The above query, serial is autonumber while dates is date/time with format as dd/mm/yyyy
There is no error in the query but it just gives wrong value. as soon as date condition is removed it gives results.
Use the mm/dd/yyyy or yyyy/mm/dd format for the string expression for the date value:
WHERE (((coaDetails.[title])='CLAIMS')AND ((coaDetails.[dates])=#2018/08/04#));
In the SQL code, the dates have to be formatted the american way: #mm/dd/yyyy#. In the query design grid, which is a user-friendly, graphical view of the SQL code in the back, the format depends on your regional settings.
Therefore, just use the month first:
SELECT MAX(serial) AS Expr1
FROM coaDetails
WHERE (((coaDetails.[title])='CLAIMS')AND ((coaDetails.[dates])=#08/04/2018#));

Excel pivot not recognising converted date from SQL as a date field

I've had a similar problem with dates before.
I'm querying a proprietary CMS database that stores dates as a string 'YYYYMMDD'.
I'm using...
convert(varchar(10),right(VarCompletionDate,2)+'/'+substring(VarCompletionDate,5,2)+'/'+left(varCompletionDate,4), 103)
to convert to 'DD/MM/YYYY' format. On the SQL side this appears to work but in Excel, the date is treated like a string rather than a date and I'm not getting the date filters that you would get with a proper date field...
what am I doing wrong here? is the convertconverting to a varchar rather than a date? ...if I do Convert(date,... I get conversion errors.
In excel, you can select a data cell and confirm that the data does not have an apostrophe sign as suffix.
This forces excel to treat the data as text. If you do find it, please do a find and replace all in your selected data range.
If there is no apostrophe, select your data and go to Home>Number>format dropdown and then select long date or short date as the format type.
This will then bring up the date filters.
Managed to find the answer!
Convert(date,....)
Was the correct treatment to output a date format from SQL.
The Pivot table wouldn't recognise the field as a date after a refresh unless I recreated the pivot table - there must be some kind of data type persistence going on in Excel.

formatting date on insert to sql?

I have a VB6 application where I insert a set of dates into a SQL-SERVER. Each time I insert a value, it gets inserted as 1978-12-12 00:00:00.000. Is it possible to specify in the INSERT statement, how you want the date to be formatted? VB6 does not seem to recognize CONVERT. I did previously CONVERT date when I loaded it into a MSHFlexGrid like this:
Convert(varchar,tblClient.DOB, 101)
But I did this in a select statement. Will SQL let me insert a value in a format MM/DD/YYYY as I need it later in that format.
The reason why I need the formatting is because I connected all my tables in SQL-SERVER2008 to Access for report generating purposes. So I need it formatted correctly in SQL-SERVER2008 as it dynamically connects to Access.
Ideally, the data type of the column in the database is set to Date or DateTime. Basically, if you want to store a date, then use a date date type.
That being said, in VB6 you usually have to (at least temporarily) store the date as a string so there is almost always a string to date conversion that happens somewhere.
Will SQL let me insert a value in a format MM/DD/YYYY
Yes. But you should not do this. Instead, you should insert the date with the format "YYYYMMDD". Notice that there are no delimiters. The problem with mm/dd/yyyy is that it could accidentally be interpreted as the wrong date. For example, 1/2/2015 would be interpreted as Feb 1, 2015 if you lived in England, or Jan 2, 2015 if you live in the US. However, SQL Server will always interpret 20150102 and Jan 2, 2015.
Once you have the data stored the way you want in the database (as an actual date data type), you should actually return it as a date to your front end (either Access or VB6). In the front end, you should use the format command to display the date. The format command will use the regional settings of the computer to display dates the way the user wants to see it.
Ex:
txtDateOfBirth.Text = Format(rs.Fields.Item("DOB").value, "Short Date")
Doing things this way... you should never have problems with dates.
The best way is not to store formatted dates in your database server.
One way you can get what you want is by using a view where you format your data and use that as input for your report:
CREATE VIEW myreport
SELECT replace(convert(NVARCHAR, mydate, 106), ' ', '/') from mytable
But I would recommend formatting dates on the application level.
You can use VB6s format function prep the date before inserting it into SQL. Here's an example (tested in VBA).
Format(Now(), "YYYY-MM-DD")

converting date from mm/dd/yyyy to dd/mm/yyyy in SSRS

I have datetime type from DB table.
In SSRS report, I'm getting datetime format mm/dd/yyyy. I want to change it to dd/mm/yyyy.
I have added expression like:
=FormatDateTime(Format(Fields!TransactionDate.Value,"dd/MM/yyyy"),DateFormat.ShortDate)
But, this is showing #Error in the report.
How to correct this?
If the field in the data set is datetime, then the expression to use is
=FORMAT(Fields!Dataset_Field_Name_Here.Value,"dd/MM/yyyy")
From looking at the expression, the 'DateFormat.ShortDate' is using the language set for the report? Goto report -> properties -> Localization -> Language. I set it to en-GB so that it will display dates in the format l require. However this value if l remember correctly can be overridden by the language settings on the client computer displaying the report.
Got solution:
=CDate(Fields!TransactionDate.Value).ToString("dd/MM/yyyy")
Go to textbox properties: Before changing the Custom to dd/MM/yyyy (as shown), select option Date and select format MM/dd/yyyy i.e. 01/31/2000 in RS 2008. Hope it helps.
Format(Cdate(Fields!TransactionDate.Value),"dd/MM/yyyy")

PL/SQL Date Insert

I am firing a insert query in 'dd/mm/yyyy' format but it is storing date into MM/DD/YYYY format.
I just want to know why it is happening?
This is insert query i am using.
insert into txnblackout(endtime,idtxn,blackoutflag,starttime,startdate,typeuser,id_entity,idsequence,idapp,enddate)
values('83520','LGN','D','7920',TO_DATE('30/12/2012','dd/mm/yyyy'),'ECU','B001','4','A1',TO_DATE('30/12/2012','dd/mm/yyyy'))
If you don't want to change the Windows date format (as suggested by Colin 't Hart), you can
use an explicit to_char() date format in your query (as suggested by Robert Hanson)
set your client NLS settings
configure your client (since you seem to be using PL/SQL developer):
Tools -> Preferences -> NLS options -> Date -> check user defined + enter your format
Personally, I'd set the client NLS settings.
This is a front-end issue: it's displaying the dates in that format. Because they are date fields, the dates really do represent the dates that you inserted.
I note in the bottom right hand corner of your screenshot that the date there is displayed in MM/DD/YYYY order. Change this setting in Windows and it will more than likely display correctly in your front-end tool.
The important bit can be gleamed by your insert, which includes "TO_DATE('30/12/2012','dd/mm/yyyy')". This is converting the string '30/12/2012' to an internal date object format that is specific to the DB. You can't control the underlying storage format. What you can control however is how that internal date object is converted back to a string by using date formatting functions when you call select.
select to_char(some_date, 'dd/mm/yyyy') my_date from some_table;
In the visual interface you referenced it is simply showing the default date to string conversion.

Resources