FormatDateTime(Now(),vbShortDate) giving different date formats in different devices. [duplicate] - sql-server

This question already has an answer here:
Format current date and time in VBScript
(1 answer)
Closed 4 years ago.
I am working in a classic asp project where I am selecting an SQL query from the database. The query was working fine in my own laptop and the date format is '11/14/2018' from this query but it's giving the mentioned error in office system and when I checked the query in SSMS then it's generating the date format as '14-11-2018'. I tried changing the different VB date formats but the result was same.
Here is the query:
sql="select count(*) as total from hc_query a, hc_breakup b where a.querytype='hotel' and a.qdate='" & FormatDateTime(Now(),vbShortDate) & "' and a.t_id=b.pnrno and b.bookstatus='half'" '
Please give some suggestions. Thanks

The problem is not in the code but in the assumptions about the function's output. The documentation for FormatDateTime states about the NamedFormat argument:
vbShortDate : 2
Display a date using the short date format specified in your
computer's regional settings.
That is, the output format depends on local configuration of the computer running the code. Two different computers can generate different output depending on locale configuration.
If you need a consistent output to deal with dates stored as strings in database fields, then you will need to write your own function to ensure consistency.

Related

Converting SAP nVARCHAR to a date

I am using SSRS 2017 to query a SAP hana database using an ODBC connection.
I return a date column BUDAT as 20190101. I am trying to convert this to a date, but in the Sataset screen won't let me use the CONVERT or FORMAT command :
Attempt #1:
CONVERT(DATE, RIGHT(SAPABAP1.AFRU.BUDAT, 2) + SUBSTR(SAPABAP1.AFRU.BUDAT, 3, 2) + LEFT (SAPABAP1.AFRU.BUDAT, 4))
I get an error
Invalid or missing expression
when I "enter" on the code I put in.
Attempt #2
format(SAPABAP1.AFRU.BUDAT, "dd/MM/yyyy")
SQL Server accepts the syntax, but when the query tried to run, I get the error
General error;260 invalid column name;dd/MM/yyy:line1 col 1029 (as pos 1028)
I have spent the last few days off an on trying themes on this code but to no avail
I would like to see the output as dd/MM/yyyy.
Once you consume DATS field in YYYYMMDD format, you will need to convert the String into the real Date value.
Assume SAPABAP1.AFRU.BUDAT is a valid string to be parsed:
For #1 (SSRS scenario), you need to use Format(Cdate(SAPABAP1.AFRU.BUDAT),"yyyyMMdd").
For #2 (SQL Server scenario), you need to use CONVERT(datetime,SAPABAP1.AFRU.BUDAT,112), where "112" refers to yyyyMMdd. You can also omit 112 as yyyyMMdd is ISO format. Alternatively, you can do CAST(SAPABAP1.AFRU.BUDAT as datetime).
Good morning, thank you for taking the time to answer.
2-CONVERT(datetime,SAPABAP1.AFRU.BUDAT,112)- when I enter the code in the sql side in the "column" where all the code goes, when I press enter to accept the code I get a microsoft visual studio error pop up window that says "function argument count error " and am unable to accept the code as you wrote.
2 - Copy and pasting CAST(SAPABAP1.AFRU.BUDAT as datetime) in - press enter I get the error invalid or missing expression.
What I have managed to do is get the date to resemble a date format using :
CONCAT(CONCAT(RIGHT (BUDAT, 2), SUBSTR(BUDAT, 5, 2)), LEFT (BUDAT, 4))
This gives the output from BUDAT 20190104 to 04012019 ( which is close to shat I need ).
Now 1-
Using the above code that works to get 01012019 I then used your code to convert this text to a date then format it ( note I changed the name of the field from Budat to postg_date for clarity ) :
=Format(Cdate(Fields!POSTG_DATE.Value),"ddMMyyyy")
The output in ssrs when its run gives me #ERROR in the output fieldI tried to include a screeen shot but I need 10 reutation points to do so :(
Currently we get the infromation I am looking at via an overnight download from SAP that goes in to a server that ises software called "universe" that is then squirted in to SQL that i Code against.
Do you think SQL 2017 cannot get convert the information directly form SAP in a format it can read

SSRS Date Parameter Mismatching

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.

CONVERT vs CAST when exporting datetime as dates from sql server

I help out a collegue with exporting data from SQL Server 2012. I type manual queries and then copy/paste the results to an Excel sheet that I then share with him. The extract I make will be an excel file that will be manually analysed. No need for automation.
What I try to do is settling for best practice when exporting order statistics grouped by day, trying to learn SQL better. I have a field of type datetime, and I want to convert this to some date format. In my locale, the typical date format is YYYY-MM-DD. I do sometimes share my script with others, that might have another locale. I have found three statements that seem to yield the same values for me.
select top 1
createdat
, CAST(createdat as date) as A
, CONVERT(char(10), createdat,126) as B
, CONVERT(char(10), createdat,127) as C
from dbo.[Order]
resulting in
createdat |A |B |C
2012-12-27 08:23:32.397 |2012-12-27 |2012-12-27 |2012-12-27
From the TSQL MSDN reference (link) I understand that:
A is handled by SQL as type Date, whereas B and C are chars.
B and C should differ by their time zone handling.
But I dont understand:
HOW does B and C handle time zones?
What is the practical difference when copy/pasting to Excel?
Is there practical difference if I share this script with collegues using another locale I should consider?
Should one or the other be preferred?
To answer your questions sequentially:
126 uses the ISO 8601 date standard, which signifies the Year aspect to be the full 4 characters long, rather than just the last two. 127 uses the same date standard, but in Time Zone Zulu, which is a military time zone (4 hours ahead of EST)
There essentially is no difference when copy/pasting to Excel. When opening an Excel doc, the default cell formatting is "General". When you paste any of these date types into Excel, it will register option A as a number (in this case 41270) and based on the pre-existing format from your query will convert it to Date format. Options B and C will first register as text, but since they are in the format of a Date (i.e. they have the "/" marks), Excel can register these as dates as well and change the formatting accordingly.
As long as the person you are sharing your script with uses T-SQL this shouldn't cause problems. MySQL or other variations could start to cause issues.
CAST(createdat as date) is the best option (IMO)
Sources:
SQL Conversion Types
ISO 8601 Details
Zulu Time Zone

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.

What date format is this?

I am trying to query some data out of Home Bank using the data file it produces.
This is a transaction that appears in the file:
<ope date="734309" amount="-14.24" account="4" dst_account="0" paymode="0" flags="1" payee="239" category="2" wording="" info="" tags="" kxfer="0" />
I am interested in the date="734309". I've not seen this format before so don't know how to parse it.
The application is written in C if that is any help.
734309 / 365 = 2011.80548
So I guess it's something like "days since 1 January in the year 1". If you know the actual date that that number should represent, you can reconstruct the precise offset from there.
It's probably the result of the SQL TO_DAYS() function, which represents the number of days since the first day of 1 A.D. (I don't know whether TO_DAYS() is specific to MySQL or if it's a standard SQL function.)

Resources