Reporting Services Remove Time from DateTime in Expression
Asked Answered
C

14

70

I'm trying to populate an expression (default value of a parameter) with an explicit time. How do I remove the time from the the "now" function?

Cuevas answered 8/9, 2009 at 18:10 Comment(2)
Check out Method 1: https://mcmap.net/q/281026/-datetime-in-ssrsClaudianus
It is important to note that even if the SQL within <CommandText> returns a Date variable only, the rdl will still use DateTime. Therefore, it must be formatted by the rdl -- see various answers below.Thankyou
C
2

Found the solution from here

This gets the last second of the previous day:

DateAdd("s",-1,DateAdd("d",1,Today())

This returns the last second of the previous week:

=dateadd("d", -Weekday(Now), (DateAdd("s",-1,DateAdd("d",1,Today()))))
Cuevas answered 8/9, 2009 at 18:22 Comment(2)
I stumbled here from google.. but.. this doesn't answer your question. (why do you care about the last second?) However, you did beat RSolberg (who correctly answered it). Maybe you should edit your accepted answer to reference his correct answer?Cavein
DateAdd("s",-1,DateAdd("d",1,Today()) returns the last second of Today, not previous day - and there's a closing backet missing to make it workHerv
S
112

Something like this:

=FormatDateTime(Now, DateFormat.ShortDate) 

Where "Now" can be replaced by the name of the date/time field that you're trying to convert.)
For instance,

=FormatDateTime(Fields!StartDate.Value, DateFormat.ShortDate)
Skeptic answered 8/9, 2009 at 18:21 Comment(4)
As my date parameter was a datetime, I required an additional CDate() around the above expression.Collocate
Won't work if the report is required in Excel format. FormatDateTIme is string function. Sure it strips off the time, but the result is a string date column which when sorting will be sorted as a string from left to right. The user would have to convert the column to a real Date using Text to Columns.Riancho
The point is, you cannot do what the OP asked in Date only format. You're stuck with string dates.Riancho
This gave me exactly what I needed. I had a DOB column that SSRS assumed needed to add a time to. Microsoft really needs to update this software.Rwanda
A
46

Since SSRS utilizes VB, you can do the following:

=Today() 'returns date only

If you were to use:

=Now() 'returns date and current timestamp
Amygdaline answered 8/9, 2009 at 18:32 Comment(2)
=Today() does not return date only in SSRSKiwi
To clarify, Today() returns the date only in terms of value, but the datatype returned is DateTime.Bilbao
M
29
=CDate(Now).ToString("dd/MM/yyyy")

Although you are hardcoding the date formart to a locale.

Meredithmeredithe answered 19/1, 2010 at 17:18 Comment(2)
+1 for the concise answer that, as a matter of fact, was very helpful to me.Thrawn
As I mentioned above, this works but turns the column into a string. It's still not a true DATE only datatype.Riancho
M
23

If you have to display the field on report header then try this... RightClick on Textbox > Properties > Category > date > select *Format (Note this will maintain the regional settings).

Since this question has been viewed many times, I'm posting it... Hope it helps.

enter image description here

Mir answered 20/11, 2015 at 9:31 Comment(3)
What if it gets ignored for whatever reason? E.g. date is still shown with hours and minutes. Any idea why this could be happening?Heteromerous
If you have a function specified for the value of the TextBox/Placeholder then it will override the formatting options on this screen.Prussian
Saved my day. Event though it is easily visible in the options many wont look here for formatting option. Thanks a lot.Allottee
D
9

If expected data format is MM-dd-yyyy then try below,

=CDate(Now).ToString("MM-dd-yyyy")

Similarly you can try this one,

=Format(Today(),"MM-dd-yyyy") 

Output: 02-04-2016

Note:
Now() will show you current date and time stamp

Today() will show you Date only not time part.

Also you can set any date format instead of MM-dd-yyyy in my example.

Disposition answered 4/2, 2016 at 12:17 Comment(0)
I
8

Just use DateValue(Now) if you want the result to be of DateTime data type.

Irritating answered 13/4, 2012 at 8:35 Comment(0)
E
5

In the format property of any textbox field you can use format strings:

e.g. D/M/Y, D, etc.

Exotoxin answered 9/9, 2009 at 16:2 Comment(1)
The question is referring to the default value of a report parameter, so setting the format on a textbox won't work. Also, "D/M/Y" won't give the expected result, try "dd/MM/yyyy" instead.Herv
G
3

One thing that might help others is that you can place: =CDate(Now).ToString("dd/MM/yyyy") in the Format String Property of SSRS which can be obtained by right clicking the column. That is the cleanest way to do it. Then your expression won't be too large and difficult to visually "parse" :)

Galileo answered 27/2, 2013 at 15:43 Comment(0)
A
2
    FormatDateTime(Parameter.StartDate.Value)
Agglutinate answered 8/9, 2009 at 18:11 Comment(1)
You are missing the second parameter of the expression FormatDateTime(DateField, DateFormat)Arabist
C
2

Found the solution from here

This gets the last second of the previous day:

DateAdd("s",-1,DateAdd("d",1,Today())

This returns the last second of the previous week:

=dateadd("d", -Weekday(Now), (DateAdd("s",-1,DateAdd("d",1,Today()))))
Cuevas answered 8/9, 2009 at 18:22 Comment(2)
I stumbled here from google.. but.. this doesn't answer your question. (why do you care about the last second?) However, you did beat RSolberg (who correctly answered it). Maybe you should edit your accepted answer to reference his correct answer?Cavein
DateAdd("s",-1,DateAdd("d",1,Today()) returns the last second of Today, not previous day - and there's a closing backet missing to make it workHerv
S
2

I'm coming late in the game but I tried all of the solutions above! couldn't get it to drop the zero's in the parameter and give me a default (it ignored the formatting or appeared blank). I was using SSRS 2005 so was struggling with its clunky / buggy issues.

My workaround was to add a column to the custom [DimDate] table in my database that I was pulling dates from. I added a column that was a string representation in the desired format of the [date] column. I then created 2 new Datasets in SSRS that pulled in the following queries for 2 defaults for my 'To' & 'From' date defaults -

'from'

    SELECT  Datestring
    FROM    dbo.dimDate
    WHERE   [date] = ( SELECT   MAX(date)
                       FROM     dbo.dimdate
                       WHERE    date < DATEADD(month, -3, GETDATE()
                     )

'to'

    SELECT  Datestring
    FROM    dbo.dimDate
    WHERE   [date] = ( SELECT   MAX(date)
                       FROM     dbo.dimdate
                       WHERE    date <= GETDATE()
                     )
Swaine answered 19/6, 2014 at 16:8 Comment(0)
A
2

This should be done in the dataset. You could do this

Select CAST(CAST(YourDateTime as date) AS Varchar(11)) as DateColumnName

In SSRS Layout, just do this =Fields!DateColumnName.Value

Aflutter answered 30/7, 2015 at 22:0 Comment(1)
Won't strip off the time component is the DateColumnName is a DateTime datatype. The OP wants the date only.Riancho
S
2

My solution for a Date/Time parameter:

=CDate(Today())

The trick is to convert back to a DateTime as recommend Perhentian.

Stannic answered 18/9, 2015 at 1:14 Comment(0)
P
2

Just concatenate a string to the end of the value:

Fields!<your field>.Value & " " 'test' 

and this should work!

Prady answered 14/7, 2017 at 0:44 Comment(1)
Hi Nathan, Would you please elaborate on your editing ?. sorry i am kind of new to stackoverflow. Regards!Prady

© 2022 - 2024 — McMap. All rights reserved.