I have a function which should accept date in a format DD-MON-YY and should display in the format DD-MM-YYYY. The function I have created is :
create or replace function PRINT_IT(abc date)
RETURN DATE
IS
v_date DATE;
BEGIN
if regexp_like(abc,'^[0-9]{2}-[a-z]{3}-[0-9]{2}$')
then
v_date := TO_DATE(abc,'DD-MM-YYYY');
else
dbms_output.put_line('wrong format');
end if;
dbms_output.put_line('The date is ');
return v_date;
END PRINT_IT;
but the value returned is always wrong date format!!
No, it's not. Your date is being output in the format specified by your NLS_DATE_FORMAT. I you want to display if differently then change this parameter for your session:
alter session set nls_date_format = 'dd-mm-yyyy'
Note the word display. That's all this does. That's all you should consider doing. The way a date is displayed in no way effects the way it is stored.
More normally you might use TO_CHAR() with an appropriate format model to display a date, i.e. to_char(my_date, 'dd-mm-yyyy'). It will no longer be a date but a character.
It doesn't look like you want to display a date as you've said. You're returning the value from your function, in which case I would stick with what you have. You only need to transform a date into an appropriate format for display when taking it out of the database, always store it as a date in the database. This in turn means that it doesn't matter what it looks like when stored in the database, merely that it is actually a date.
Related
I have an issue with SSRS where when posting in a DD/MM/YY value via URL string into a Parameter it decides to read the Day value as the Year, the Month as month, but the Year goes into Day value, for example:
I am inputting the date of 30/08/17 via an ERP System which then generates a string to be used as an URL to generate the report, this date value should then go into a parameter called fiAsOfDate which is Date/Time data type, but at this point it is reading the value as 08/17/1930 inside the Parameter list, even though the URL remains at 30/08/17.
This happens prior to the Query being processed, and the fiAsOfDate parameter then gets formatted through to to MMDDYYYY to be processed within the Query, but the issue is specifically when the parameter is having the value loaded from the URL into the parameter value, and I was hoping if anyone could assist me on this please?
I should also add, this original date is coming from an ERP system which will have regional based date formatting, as it is used internationally, so I cannot restrict myself to one input format, and it should be using regional settings, matching that of the Reporting server that it is based on.
Kind Regards,
James W. Acklam.
To avoid regional setting issues you can change the date into a known integer or string format: I use CONVERT(NVARCHAR, YourDate, 112) to get a string '20170830'. The regional settings won't recognize that as a date and so won't auto-parse it into MM/DD/YYYY or DD/MM/YYYY. Of course, you'll need to parse that yourself so you can use it in the report, but at least you know the format.
This issue was down to my own misunderstanding that the DataSource I was pulling data from worked only in DMY format. Converting dates from parameters format to DMY format and processing that through the DataSet's query resolved the issue.
My SSRS report displays a date range (2 parameters) and a multi-selectable value parameter for a field called "MeterNumber".
What I want is for the default value of the multi-selectable parameter to show ALL values until the user specifies one of the values in the list.
I've tried doing this by setting the general tab in the parameter properties to "Allow Multiple Values", and the Available and Default values to equaling the same value fields from the same dataset, but no go.
Any thoughts?
You can use the same dataset that populates the Available Values of the parameter, to populate the Default Values of the parameter.
Then by default, all values are selected when the user first opens the report.
You can assign minimum date and maximum date in your back-end if your date start and date end is empty or null (remember the min date in sql is 1/1/1753 12:00:00 AM and max date is 12/31/9999 11:59:59 PM so your date format should be like that). So when its passed on your parameter it will get all the data within that minimum start date and maximum end date.
This is how you are going to assign min date time in c#
DateTime dateStart = SqlDateTime.MinValue.Value;
and max date
DateTime dateEnd = SqlDateTime.MaxValue.Value;
Do you have any blank and/or null values for MeterNumber in the dataset? This can cause issues with Parameter values. Since in your case you need multiple values, then you can't have null values. Just make sure Allow blank value ("") and Allow multiple values are selected. Then, make sure you don't have NULL values in the MeterNumber column. If you do, then convert to empty string, i.e., ISNULL(AC.MeterNumber, '') as MeterNumber
Just had the exact same problem #Byronyk, however just realised what the issue was (Also aware this post is very old, this is more for those who stumble upon this!)
The issue I found was that I had one extra Value in the available values than in the Default Values, and as such it threw out the default selections.
Once I realised and added it in, the default values were selected again.
I suspect this is the issue, as the date worked fine, the issue was in the 'MeterNumber' parameter
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")
The date stored in my database is 01-01-1900 for field emp_doj(Data Type DATE).
But while retrieving the data, the value is 01-jan-00, even though formatted with dd-mm-yyyy.
I am comparing retrieved date field with some other date in SQL query.
select *
form persons
where per_join = to_date(to_char(i.emp_doj,'DD-MM-YYYY'),'DD-MM-YYYY')
Here the value of to_date(to_char(i.emp_doj,'DD-MM-YYYY'),'DD-MM-YYYY') results in 01-01-00, instead of 01-01-1900
I suspect it's your default NLS settings for the IDE you are using that is merely displaying the dates with a two digit year. Oracle does not store dates in any specific format. It stores them in a binary format internally.
Therefore I suggest you check the settings for your IDE and/or database and you will see the NLS date format set to DD-MM-YY, alter this and set it to DD-MM-YYYY and you will see the dates as you wish to see them.
As for your query, why use TO_CHAR and then TO_DATE? If emp_doj is a date field and per_join is also a date then just compare them directly for equality. If you are trying to cut the time portion off your emp_doj values then just use TRUNC().
For examples and reference on NLS: http://www.orafaq.com/wiki/NLS
More here: http://www.oracle.com/technetwork/database/globalization/nls-lang-099431.html
select to_char(emp_doj,'dd-mm-yyyy') from yourtable
I have got some temporary solutions it currently works for me, I simply changed my select query to
select *
form persons
where to_date(per_join,'DD-MM-YYYY')= to_date(i.emp_doj,'DD-MM-YYYY')
I've written a stored procedure that takes in a date parameter. My concern is that there will be confusion between American and British date formats. What is the best way to ensure that there is no ambiguity between dates such as 02/12/2008. One possibility would be for users to enter a date in a format such as 20081202 (yyyymmdd). Is there any way to validate that without using sub strings? Alternatively dates could be entered as 02-Dec-2008(dd-mmm-yyyy), but again verification is not trivial and there are potential issues with users who do not use English.
Further to the first three answers . . . One issue is that I'm expecting this stored proc to be called directly without a front end so validation ouside of the proc is not an option. Is it a good idea to take the day, month and year as separate parameters?
You won't have any problems whatsoever if you'd use parameters in your sproc:
create proc dbo.Sproc
#date datetime
as
...
If you declare the parameter as being of type DATETIME or one of the other typed date/time types in SQL Server, which you should, then there is no ambiguity; it represents a particular date and time. The type of validation you're talking about should happen outside the stored procedure, not inside.
OK from your comments and edit, it appears the issue is with the way people call the SP rather than actually within it. To that end, you simply need to train your users to use sortable date format, i.e.
yyyy-MM-dd HH:mm:ss
And then there is no ambiguity. Anybody who is allowed near a database should be aware of localisation issues and should always be using a non-ambiguous format like this one when entering dates.
I've ended up taking a string paramater for the date and require users to enter the month as a word. I check the input is a valid date by converting it to date. To ensure the month is entered as a word, I use the like comparator to compare the input string with "%Jan%" or "%Feb%" or "%Mar%" etc.
If your proc accepts the date as a datetime parameter then there is little you can do to validate that the desired format is ddmmyyyy and not mmddyyyy. It all depends on how the user entered the date and how it was passed to SQL.
For example: On a web page i could add a parameter like this
command.Parameters.AddWithValue("#mydate",mydateVar.ToString("dd/MM/yyyy"));
OR
command.Parameters.AddWithValue("#mydate",mydateVar.ToString("MM/dd/yyyy"));
And SQL will just insert what its given as long as the string can be cast to a date correctly. It wont know the format you want to use so it will try to cast to its system default format.
A solution i use although it may not be applicable in your situation is to have users enter all dates in whatever frontend you have in the dd-MMM-yyyy format. I can then be sure of the format before i insert into the DB. I use that format everywhere to keep it all the same throughout the app.
You said that you are expecting this stored proc to be called directly without a front end and validation ouside of the proc is not an option.
In that case the users will be inserting data directly, I also believe that in this case it is for internal use only (as the stored proc is going to be called directly)
So I think you have 2 options
if you have disciplined users you can agree on one of the safe formats: ISO yyyyddmm, or ISO8601 yyyy-mm-dd Thh:mm:ss:mmm if you need a time part as well
otherwise take 3 parameters: year, month, year and perform some validation inside the stored procedure
I say take a datetime and train them to use the ODBC canonical form of a date as in this example:
EXECUTE uspMyProc {d '2009-02-11'}
If you take a date that you have to parse, whether it be a string or the year, month and day as separate integer arguments, then you have to deal with days out of range for the month and year. Some functions that take those automatically advance or move backwards the day on you. Thus the trick of sending 0 for the day and getting the last day of the previous month. Others return an error. But handling that stuff yourself is probably not worth reinventing the wheel.
If you absolutely have to because novices will be running it directly (why would novices be running stored procedures directly?), I'd take three separate arguments and pass the concatenated date as a string in the format YYYY-MM-DD through ISDATE to verify the parameters and exit if it isn't valid.