formatting date on insert to sql? - sql-server

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")

Related

ADF - Change the date format from any format coming from csv to yyyy-MM-dd HH:mm:ss while loading in target sql table taking datetime

I have a situation where I am getting dates in two separate formats, MM/dd/yyyy & yyyy-dd-MM, AND there might be even more different formats as well in csv which will be obviously in string.
Below are the data which currently come as String from CSV-
1/14/2022 0:00
2021-12-31 00:00:00
I am using a Dataflow task in ADF to load the data into Azure SQL where the default format it uses should be yyyy-MM-dd HH:mm:ss.
how can I do this?
ok, i managed to build a quick demo.
Main idea of my solution:
you need to differentiate between valid rows and rows that needs to be modified.
in order to do so, i used case condition.
the idea is to add a derived column with a name 'Date' and modify only needed rows.
Input Data:
i created a csv file and saved my data as a dataset in ADF.
ADF:
In source, i select my dataset as an input.
in a derived column activity:
added a new derived column with a name 'Date' , value :
case(contains(split(Date,''),#item=='/'), toString(toTimestamp(Date,'MM/dd/yyyy H:mm'),'yyyy-MM-dd HH:mm:SS'), Date)
in toTimestamp method, i added first the dateFormat of my input Date and in toString the desired format that i want to cast the date to it.
Output:
P.s
You can cast all possible date formats that will appear in your data in that way.
you can read more about it here:
https://learn.microsoft.com/en-us/azure/data-factory/data-flow-expressions-usage#toTimestamp

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.

Date Picker format vs Database timestamp

Is it possible to compare two dates if they are formatted in different ways? One date is coming from a ColdFusion UI input which is in the following format:
mm/dd/yyyy
My timestamp in my MSSQL database is in the following format:
yyyy/mm/dd
Background: I need to write a query that compares the dates and returns rows with a timestamp after the user selected date. Would I need to reformat either date?
Example:
<cfset start_date = 'form.sDate'>
<cfquery name="sortDate" datasource="RC">
SELECT *
FROM my_db_table
WHERE Submission_Date > #start_date#
</cfquery>
First off, always use cfqueryparam in your queries when dynamically including content. Second, you should be able to use start_date "as is" to compare the date to the date in the database
So, something like:
<cfquery name="sortDate" datasource="RC">
SELECT *
FROM my_db_table
WHERE Submission_Date > <cfqueryparam value="#start_date#" cfsqltype="cf_sql_date">
</cfquery>
Last, you can always test the raw sql in MSSQL Management Studio to test the date comparison.
(Too long for comments...)
There are a few subtle distinctions here it is important to understand.
You are working with a string and a date object, not two dates. Also, SQL Server does not actually store dates in yyyy/mm/dd format. Dates are stored as big numbers internally. Since those are difficult for humans to process, whatever IDE you are using displays them in a more human friendly way, such as yyyy/mm/dd format. But again, that does not mean the database actually stores them as strings.
Since the datepicker value is a string, you should validate it first, then convert the string into a date object. Comparing apples and oranges (ie dates and strings) is common gotcha. That is almost always where date queries go awry. That is why converting the string into a date object is important. It ensures the database compares the same things ie two dates and that the results will always be what you expect.
Finally, use cfqueryparam to pass the value to your database correctly. Be sure to use the cfsqltype which corresponds to the db column: cf_sql_date (date only) or cf_sql_timestamp (date and time).
Technically, if the strings are always in mm/dd/yyyy format you can pass in the string "as is" and CF will automatically convert it for you. However, trusting the input will always be in the right format is not a great idea. Validating input and using cfqueryparam will ensure the most consistent results.

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.

date comparison in oracle for the year ends with 00

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')

Resources