sql developer data export date format is null - sql

I'm using SQL developer connected to a db2 database.
I want to export a table to a list of insert statements. This works, but the problem is that dates are shown like:
to_timestamp('2016-02-08 11:07:54.01','null'),
There is no date format specified, so executing insert statements like this doesn't work.
I did set the date format in Preferences->Database->NLS
for 'Date format','Timestamp format' and 'Timestamp TZ format' to:
YYYY-MM-DD HH24:MI:SS.FF3
But somehow SQL developer keeps spitting out null values. Is there some other setting I need to check?

Related

Sql in Snowflake changing date from 106(13-APR-2020) to 105(13-04-2020)

So I have a couple date fields in a table that are formatted as (DD-MON-YYYY) and I need to convert them to (DD-MM-YYYY) The field itself is already a VARCHAR. In Snowflake, how would I make this change. Here is what I have done so far.
select to_date(end_date, 'dd-mm-yyyy') from
error: Can't parse '31-JAN-2020' as date with format 'dd-mm-yyyy'
An easy way for you to approach this is to convert your string to a date, and then back to a string in the new format, an example follows:
--create an example table to test
CREATE TEMPORARY TABLE xyz (my_dt_string VARCHAR(100));
--insert a couple records
INSERT INTO xyz VALUES ('31-JAN-2020'), ('13-APR-2020');
--test a conversion, from string to date back to string
SELECT my_dt_string,
TO_CHAR(TO_DATE(my_dt_string, 'dd-mon-yyyy'), 'dd-mm-yyyy') converted
FROM xyz;
--looks good? run the following
UPDATE xyz
SET my_dt_string = TO_CHAR(TO_DATE(my_dt_string, 'dd-mon-yyyy'), 'dd-mm-yyyy');
--take a look
select * from xyz;
I hope this helps...Rich
The to_date() function provides you the ability to convert a VARCHAR field to a DATE field, not specify the format of the date. So, you'd want to run this:
SELECT TO_DATE(end_date, 'DD-MON-YYYY');
This will output the date as a date. If you want to specify how you see a date, you have 2 choices:
First, convert it back to a VARCHAR in your preferred format:
SELECT TO_VARCHAR(TO_DATE(end_date, 'DD-MON-YYYY'),'DD-MM-YYYY');
Second, change your session parameter DATE_OUTPUT_FORMAT to be the format you'd like to see dates displayed:
ALTER SESSION SET DATE_OUTPUT_FORMAT = 'DD-MM-YYYY';
This could also be done for a user, rather than a session using:
ALTER USER SET DATE_OUTPUT_FORMAT = 'DD-MM-YYYY';

Why does MS SQL Server error on inserting the second row but not on the first row?

I have an SQL table that will let me insert some rows with a datetime field but not all rows and I can't see why not. The error I get is
'The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.'
To try and work this out I created a temporary table with 1 colum and tried to add the two datetimes that are causing the issue.
create table #DateTimeTest ( created datetime );
I then try inserting these two rows
insert into dbo.#DateTimeTest (created) VALUES ('2020-06-12 05:46:00.00');
insert into dbo.#DateTimeTest (created) VALUES ('2020-06-19 05:31:00.00');
The first row is inserted without any problems, the second-row errors, but I can't understand why.
(1 row affected)
Msg 242, Level 16, State 3, Line 10
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.
Completion time: 2020-07-15T11:28:43.6451779+01:00
insert into dbo.#DateTimeTest (created) VALUES ('2020-06-19 05:31:00.00');
Apparently, your date format is YDM rather than YMD. You can fix this in several ways.
One method is to use an unambiguous date/time format. In your case, that would include a T:
insert into dbo.#DateTimeTest (created) VALUES ('2020-06-12T05:46:00.00');
insert into dbo.#DateTimeTest (created) VALUES ('2020-06-19T05:31:00.00');
The reason this works is because the YYYY-MM-DDTHH:MI:SS format is the standard format for a date/time format in SQL Server -- unambiguously and not affected by dateformat. Similarly YYYYMMDD (note: no hyphens) is the standard, unambiguous format for a date constant in SQL Server, but YYYY-MM-DD is subject to dateformat.
A second method would be to set the dateformat to YMD:
set dateformat YMD;
Here is a db<>fiddle illustrating this.
A third method would be to explicitly extract the datetime from the string, using a function such as convert() -- and perhaps a few string operations as well.

Inserting a date into a table adds extra timestamp with it

I am trying to insert a column that is in date format such like 2019-09-25 but whenever I run the query it adds the timestamp (2019-08-15T00:00:00.0000000) to it even though I have defined the column to be 'date' format. How can I make sure it only shows the date?
There are few settings which sets by Oracle on your behalf. One of them is date/time format. Your default NLS_DATE_FORMAT is currently set up to return timestamp also along with date. You only need to alter your session to return only date while fetching column with DATE datatype -
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD';
Check like below-
-- FUNCTION for Getting Azure DateTime as per our Time Zone
CREATE OR ALTER FUNCTION dbo.udf_getdate()
RETURNS DATETIME
AS
BEGIN
DECLARE #Current_DateTime DATETIME
SELECT #Current_DateTime=SYSDATETIMEOFFSET() AT TIME ZONE 'India Standard Time' -- IST Format.(give your timezone here)
RETURN #Current_DateTime
END
GO
-- DECLARE LOCAL VARIABLE AND GET THE RESULTS AS EXPECTED AND USE IT
DECLARE #CurrentDateTime DATETIME,#CurrentDate DATE;
SELECT #CurrentDateTime = dbo.udf_getdate();
SELECT #CurrentDate = CAST(#CurrentDateTime AS DATE);
SELECT #CurrentDateTime AS [CurrentDateTime],#CurrentDate AS [CurrentDate];
I've had a lot of trouble with this, and i came to conclusion that it is working properly but the thing is that query editor on Azure portal is in preview mode.
In pictures below you can see that when I inserted a value that has some hours/minutes/sec into a column that is of type 'date'
Once I try to read the same data, hours/minutes/secs will be set to 0, which means it wrote the correct value to the table (only year-month-day), but problem comes when that value needs to be displayed.
Long story short:
Basically i think Azure portal can't display correct value because it is in preview. (but correct value is inserted into database)
in picture below is how Azure portal query editor is displaying data:
and if you export that database, and import it into a local SQL server, same data will be displayed as

Change Date format during SQL

I am moving data to a new database and need to so some formatting during the move to try and save me time.
The current DB saves the date as YYYYMMDD and the new DB wants it in mm/dd/yyyy. Can I do this during the move or will I have to format after?
Thank you all!
You cold use the format function . So I am not sure how you are getting the data to sql 2014 but once the data is there you could use this command.
This is an example selecting from a table that has a date and altering its format .
use AdventureWorks2012
go
select modifieddate as 'original format', FORMAT ( modifieddate, 'd', 'en-US' ) AS 'New format mm/dd/yy'
from [Sales].[SalesOrderDetail]
Query Result
If your data is not format and its just a string you could use the format command to add the separators .
This code create a table with a date as an INT, the selects the data and formats it as a data time into another table .
CREATE TABLE Test_TimeString (Timeint int)
GO
INSERT INTO Test_TimeString VALUES(04242016)
GO
CREATE TABLE Test_Time (Timedate DATETIME)
GO
INSERT INTO Test_Time
SELECT FORMAT(Timeint,'##/##/####')
FROM Test_TimeString
SELECT * FROM Test_Time

Revert CAST(0xABCD AS date)

I am designing a query to build an insert query from parts of the data that is in a database.
I know that you can export the whole database using SQL Server Management Studio, but I only need a part, and I need it in an automated form.
The Insert query that SSMS generates for a certain dataset would be
INSERT INTO tbl (dateCol) VALUES(CAST(0xABCD AS Date))
GO
and I am now trying to cast the date inserted by this statement back to 0xABCD.
I don't know what type 0xABCD is, so I played around:
Casting to int returns Explicit conversion from data type date to int is not allowed error
Casting to string returns ISO format, but not 0xABCD format.
Could I use ISO format from casting to string? If so, why did MS choose to use some kind of hex values with CAST, instead of universally understandable ISO date, in its own SSMS export routine?
SELECT CAST(CAST(0xABCD AS INT) AS DATETIME)
-- 2020-06-01 00:00:00.000
SELECT CAST(CAST(CAST('2020-06-01 00:00:00.000' AS DATETIME) AS INT) AS BINARY(2))
-- 0xABCD