SQL invalid datatype time - sql

I am trying to create a table in Oracle SQL*Plus and it won't take the datatype time, but has no problem taking the datatype date.
drop table order;
create table orders (
order_id char(4) not null,
order_date date,
order_time time, -- invalid datatype
cash_time char(3),
primary key(order_id)
);
That's weird... why is that? How can I fix it, or are there alternatives to using time?

In Oracle there is no datatype as TIME. You can use TIMESTAMP or DATE. So I think you need to change TIME to TIMESTAMP or DATE and things will work for you.
I think you are getting confused with this TIME datatype.

I quickly looked at the orcle datatype documentation and didn't see a time datatype and i think it's the problem.
why you don't use the date datatype as a single field instead of creating two field for date and time.

Related

PostgreSQL: Alter column type without data conversion for compatible types

I have a large table shipments with the column time which has the data type timestamp(0) with time zone, i.e. all microseconds are rounded by PostgreSQL to seconds.
I'd like to convert time's data type to allow microseconds, i.e. timestamp with time zone, but the table is large, so I'd like to alter the type:
ALTER TABLE shipments
ALTER COLUMN time
TYPE timestamptz;
...but do not convert data stored in this column, taking into account timestamp(0) and timestamp are perfectly compatible - because in my case data conversion will take a lot of time. How can I accomplish this?
Thanks in advance!
ALTER TABLE shipments ALTER COLUMN time TYPE timestamptz
won't convert the data which are already stored in your table.
Only the new inserted data and updated data will be stored with the microseconds.
See the demo in dbfiddle.
Widening the value is not a problem. But changing from 'without timezone' to 'with timezone' probably is. It will not rewrite the table if your setting of "timezone" at the time you run the command is 'UTC'.
If the setting is not UTC, then it will need to rewrite the table so it can apply the offset. (I don't know that I agree this is what it should do, I haven't thought it through before. But nonetheless that is what it does do.)

SQL - Conversion failed when converting date and/or time from character string

I have 3 tables in the database that I'm working on. Out of 3, two of the tables have columns that include dates. When I checked the information schema of the columns I found that dates have the wrong data type. If you see the picture below, the highlighted columns should be stored as DATE data type.
So, I used the following query to change their data type from varchar to DATE:
ALTER TABLE [dbo].[Customer]
ALTER COLUMN DOB DATE;
ALTER TABLE [dbo].[Transactions]
ALTER COLUMN tran_date DATE;
The error that I get is:
Conversion failed when converting date and/or time from character string.
Please let me know how I can fix this error. Thanks!
What you can do is update the value using try_convert() first and then alter the column name. Note: This will set any invalid values to NULL.
update customer
set dob = try_convert(date, dob);
alter table customer alter column dbo date;
If you want to see the bad values, then before you change the table, run:
select c.*
from customer c
where try_convert(date, dob) is null and dob is not null;
You may have other ideas on how to fix the values.
You can't change from varchar to date or time or datetime by altering the column. Why? Because SQL Server does not know if the field contains '1/1/2020' or 'My dog is cute'. You will have to rebuild the table with the proper data types and then CAST() or CONVERT() the values to a true date time.
Underneath the hood, this makes more sense. A char/varchar uses one byte per character. nchar/nvarchar uses 2 bytes per character. A datetime is a number, not a character. This means you need a routine that changes this into the correct number. If you want to get deeper, the number is the number of ticks (nanoseconds) since midnight on January 1, 0001 in the Gregorian Calendar. More info.

PostgreSQL UPDATE statement treats keyword as a column

sorry for the noob question, just getting started with SQL and I have a problem that I could not resolve.
Basically, I created a table with "date of birth" column and chose wrong TYPE (integer), so every time I am trying to enter date of birth as "1998-01-04" it is just showing 1998. So, I decided to update the column to TEXT type.
Tried these queries
UPDATE users
SET date_of_birth = VARCHAR
It shows me error that there are no VARCHAR columns. Tried with as 'VARCHAR', still not working, tried as 'TEXT' and TEXT, still the same error.
What am I doing wrong?
Strings are as bad or worse than integers for this purpose. You want to use:
alter table alter column date_of_birth type date;
Note: If you have existing data in the column, you need to either use the using clause to convert it to a date. Or NULL it all out.
use this for your alter table statement
ALTER TABLE users
ALTER COLUMN date_of_birth TYPE VARCHAR(50);
I am not sure why you are storing date as varchar whereas you can store it as date, if you want to change that to date then use
ALTER TABLE users
ALTER COLUMN date_of_birth TYPE DATE;

How to order by date when a column is of type nvarchar in Microsoft SQL Server

I have an issue where I created a column sent_Date of type nvarchar while it's storing date and time.
Now when I try to sort it by date, it's not doing so correctly.
I am using this query:
select *
from tbl_skip
where sent_date > '9/27/2020 7:29:11 PM'
order by SENT_DATE desc
Like the comments have said, the real solution here is fix your design. That means changing the column's data type an nvarchar to a date and time data type, I'm going to use a datetime2(0) here, as your data is accurate to a second, so seems the most appropriate.
Firstly we need to convert the value to as ISO value. I'm also, however, going to create a new column called Bad_Sent_Date, to store values that could not be converted. Experience has taught many of us that systems that incorrectly use string data types to store dates (or numerical data) rarely have good data integrity rules on the value (because if they did, it wouldn't be an nvarchar) to start, so have bad values like '29/02/2019' or mix styles, such as having both '09/29/2020' and '29/09/2020'.
Based on the single example we have, I will assume your data is supposed to be in the format MM/dd/yy hh:mm:ss AM/PM:
ALTER TABLE dbo.tbl_skip ADD Bad_Sent_Date nvarchar(30) NULL;
GO
UPDATE TABLE dbo.tbl_skip
SET Bad_Sent_Date = CASE WHEN TRY_CONVERT(datetime2(0),Sent_date,101) IS NULL THEN Sent_date END,
Sent_Date = CONVERT(nvarchar(30),TRY_CONVERT(datetime2(0),Sent_date,101),126);
GO
Now we have an ISO format, we can change the table's data type:
ALTER TABLE dbo.tbl_skip ALTER COLUMN Sent_date datetime2(0) NULL;
Note that if you do have constraints on the column Sent_date, or it isn't NULLable, you will first need to DROP said CONSTRAINTs, change the column to be NULLable and then recreate said CONSTRAINTs after you have altered the column.
You can also review the "dates" that failed to be converted with the following:
SELECT bad_sent_date
FROM dbo.tbl_skip
WHERE bad_sent_date IS NOT NULL
AND Sent_date IS NULL;
Once that's all done, then your query simply needs an update to use an unambiguous date literal, and it'll work:
SELECT *
FROM tbl_skip
WHERE sent_date > '2020-09-27T19:29:11' --'9/27/2020 7:29:11 PM'
ORDER BY SENT_DATE DESC;
You can convert the data from string to datetime.
Please note i used 100 as an example to convert to date time. You can use below link to see if its behaving correctly. link -https://www.w3schools.com/sql/func_sqlserver_convert.asp
select *
from tbl_skip
where sent_date > convert(datetime,'9/27/2020 7:29:11 PM',100)
ORDER BY CONVERT(datetime,SENT_DATE,100) desc
You should be able to convert it to a datetime
select *
from tbl_skip
where sent_date > '9/27/2020 7:29:11 PM'
order by convert(datetime,SENT_DATE) desc
Just make sure the data in column is legit. If so, it would make sense to convert the column type to a datetime.
alter table tbl_skip alter column SENT_DATE datetime
If the data is mixed, you may need to fix it or use something like
order by try_convert(datetime,SENT_DATE) desc

How to show timestamp's value as datetime

please tell me how to show the value of timestamp field of a table in datetime format (human understandable). and also how to use it in comparison while querying it.
Thanks
This can't be done. The TIMESTAMP datatype is misnamed - the name has been deprecated in favour of ROWVERSION in SQL 2008, which is a much clearer name.
TIMESTAMP stores an incrementing value which changes every time a row is updated.
If you want last updated dates stored against each row, you will need to add a DATETIME or SMALLDATETIME to your table and update it with the current date on each update.
You mean timestamp data type I think - http://msdn.microsoft.com/en-us/library/ms182776%28v=SQL.90%29.aspx
You can't convert it to datetime. According to the documentation in the link above :
> The timestamp data type is just an incrementing number and does not preserve a date or a time. To record a date or time, use a datetime data type.