Inserting a date into a table adds extra timestamp with it - sql

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

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

Converting date format gives "Conversion failed when converting date and/or time from character string"

The date format in the table is YYYYMMDD and I would like to convert it to the following format but it is failing with an error:
2019-07-23 00:00:00.000
Conversion failed when converting date and/or time from character string
Here is the statement I'm using:
convert(varchar(10), convert(datetime, InstallDate0), 23)
The real problem is the choice of your datatype. varchar is the wrong choice. As a result, it seems that you now have some rows where the value of the "date" has been lost, as it can't be converted to a date.
To properly fix this problem, fix your datatype. Firstly I would create a new column to store the bad values:
ALTER TABLE YourTable ADD BadDate varchar(20); --as it's yyyyMMdd you don't need more than 8 characters, but we'll assume you have some really bad values
UPDATE YourTable
SET BadDate = InstallDate0
WHERE TRY_CONVERT(datetime,InstallDate0) IS NULL;
Now that you've done that, time to update the existing column:
UPDATE YourTable
SET InstallDate0 = CONVERT(varchar(8),TRY_CONVERT(datetime, InstallDate),112);
This'll set every value to the yyyyMMdd format where the value can be converted. NOw you can alter your table:
ALTER TABLE YourTable ALTER COLUMN InstallDate0 date; --AS it's yyyyMMdd, it seems silly to actually use datetime
Now you have a proper datetime column.
You'll then need to inspect the values of BadDate and try to correct them (or admit that any information they held has been lost for ever).
If you "must" have another column with the format, then add a further column:
ALTER TABLE YourTable ADD InstallDate0_f AS CONVERT(varchar(23),InstallDate0,121);
You can determine where the problems are using TRY_CONVERT(). The problem would seem to be the conversion to a datetime, so try this:
select InstallDate0
from t
where try_convert(datetime, InstallDate0) is null;

sql developer data export date format is null

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?

How can I change the format of the dates produced when using ALTER COLUMN to convert a datetime column to varchar?

I have a datetime column that I need to alter to be a varchar column.
Using the statement below, the strings produced have this format: "Jan 18 2010 5:28PM"
ALTER TABLE Thinger
ALTER COLUMN LastUpdateDate varchar(16) NOT NULL
I would like strings produced to have a yyyyMMdd format (giving 20100118) instead. Is there a way to do this?
Thanks
Bad, Bad idea...never ever store dates in varchar columns, now you will get garbage in there in different formats
Also why varchar(16) when you want yyyyMMdd?
if you want the output to be in a different format do it in the presentation layer or use convert
SELECT CONVERT(CHAR(8),GETDATE(),112)
now if you really want to do what you say you want to do
run your script and then do
UPDATE Table
SET LastUpdateDate = CONVERT(CHAR(8),(CONVERT(DATETIME,CONVERT(varchar,LastUpdateDate))),112)
But again..bad bad bad idea
Also the next version of SQL Server will make formatting a lot easier see: Format function in SQL Server Denali CTP3
Try this script:
ALTER TABLE Thinger ADD LastUpdateDateText VARCHAR(16) NOT NULL
GO
UPDATE Thinger SET LastUpdateDateText = CONVERT(VARCHAR(8), LastUpdateDate, 112)
GO
ALTER TABLE Thinger DROP COLUMN LastUpdateDate
GO
sp_RENAME 'Thinger.LastUpdateDateText' , 'LastUpdateDate', 'COLUMN'
GO

SQL: datetime or varchar as parameter datatype for stored procedure

I have a SQL server stored procedure which would accept date as input param to build a query in it.So which datatype i should use for the parameter when defining stored procedure.Whats the difference between using nvarchar(15) and datetime .
1 : create procedure TestProcedure(#startDate datetime)
and
2 : create procedure TestProcedure(#startDate nvarchar(15))
IS there any advantage is i use datetime over varchar in terms of performance
Yes, always use the datetime type. Its more accurate. The datetime type is also always 8 bytes so it is smaller to transfer than 15 characters.
The front end is then responsible for handling cultural/locale issues such as MDY, DMY or YMD component field ordering. (if the database is running under a US locale and the user is in the UK does '3/4/2009' mean April 3 or March 4?)
If you know that only dates will come through the parameter, using the appropriate datatype is better as it's smaller and comparisons can be made quicker. It also prevents invalid values from being supplied.