I've read a couple of posts on converting from dd/mm/yyyy to yyyy-mm-dd but the code is not working (CONVERT AND CAST). Please see example below and kindly advise if you can help as the error I'm getting is 'Conversion failed when converting date and/or time from character string'. If I change my date_engaged to '2010-09-12' it will work but I need to convert it somehow because if i say select getdate(), it will spit out this date format (yyyy-mm-dd).
It's interesting because the code is not working for me on my work PC and I'm using Microsoft SQL server Management studio 2008, but at home, it works perfectly fine.
create table test(
name varchar (10),
date_engaged nvarchar (20),
timestamp2 nvarchar (20),
LOS nvarchar (20)
)
insert into test (name,date_engaged) values ('JJ','12/09/2010')
update test
set timestamp2=CAST(DATEADD(month,0,dateadd(month,datediff(month,0,getdate()),-1)) AS DATE)
update test
set LOS=DATEDIFF(day,date_engaged,timestamp2)/365.25
thx
You need to take a look at the MSDN docs for CONVERT where you will find that we need to provide the format also so that it can format the date as per our reruirement. The reason why it is working on your home system, is may be because your system is configured in the format in which you are giving the dates.
A fix to your problem
DECLARE #x VARCHAR(13) = '12/09/2010'
SELECT RIGHT(#x,4) + '-' + LEFT(RIGHT(#x,7),2) + '-' + LEFT(#x,2)
DEMO
On a side note:
It is not recommended to use NVARCHAR or VARCHAR or CHAR to store dates, so as to avoid these types of issues. You can better use DATE datatype to store dates.
Related
Does anyone have any idea how to convert a date formatted "mmm-yy" (varchar) to "dd-mm-yyyy" or only from varchar data type to date data type.
It actually depends on what language or tool you are using, but there is almost certainly a builtin function that will help you do this.
Checkout
MySQL: DATE_FORMAT()
Oracle: SELECT TO_CHAR(SYSDATE, 'DD-MM-YYYY') FROM dual;
SQL Server: SELECT CONVERT(VARCHAR(10), GETDATE(), 120);
Or if you are doing this using a programing language i.e., Python, JavaScript etc. Just use the built in string replace methods to change the date into your desired format.
I am dealing with the table which has date information in CHar(20) type. This date is in dd.mm.yyyy HH.MM.SS format but my pgadmin has Month first format. I tried editing posgres config file to change the date format. I tried to use SET timezone and then tried to convert type to timestamp but nothing is working. How can I convert following column into timestamp format? I followed miost of the answers here on stackoverflow but getting out of range error even after using set function or editing config file.
Use to_timestamp:
to_timestamp(stringcol, 'DD/MM/YYYY HH24:MI')
To change the data type of the column, which is highly commendable:
ALTER TABLE mytable ALTER date1 TYPE timestamp
USING CAST (to_timestamp(date1, 'DD/MM/YYYY HH24:MI') AS timestamp);
I'm trying to convert this, which works:
create_timestamp for column
CREATETS TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
to something that works like this, but this code is not working:
date_created for column
DTCREATE NUMERIC(8,0) NOT NULL DEFAULT VARCHAR_FORMAT(CURRENT_TIMESTAMP, 'YYYYMMDD'),
Can anyone advise DDL to accomplish what I'm going for? Thank you.
When asking for help with Db2, always specify your Db2-server platform (Z/OS , i-series, linux/unix/windows) and Db2-server version, because the answer can depend on these facts.
The default-clause for a column does not have syntax that you expect, and that is the reason you get a syntax error.
It's can be a mistake to store a date as a numeric, because it causes no end of hassle to programmers and reporting tools, and data exchange. It's usually a mistake based on false assumptions.
If you want to store a date (not a timestamp) then use the column datatype DATE which lets you use:
DTCREATE DATE NOT NULL DEFAULT CURRENT DATE
How you choose, or future programmers choose , to render the value of a date on the SQL output is a different matter.
You may use BEFORE INSERT trigger to emulate a DEFAULT clause with such an unsupported function instead.
CREATE TRIGGER MYTAB_BIR
BEFORE INSERT ON MYTAB
REFERENCING NEW AS N
FOR EACH ROW
WHEN (N.DATE_CREATED IS NULL)
SET DATE_CREATED = VARCHAR_FORMAT(CURRENT_TIMESTAMP, 'YYYYMMDD');
Postgres (V11.3, 64bit, Windows) truncates trailing zeros for timestamps. So if I insert the timestamp '2019-06-12 12:37:07.880' into the table and I read it back as text postgres returns '2019-06-12 12:37:07.88'.
Table date_test:
CREATE TABLE public.date_test (
id SERIAL,
"timestamp" TIMESTAMP WITHOUT TIME ZONE NOT NULL,
CONSTRAINT pkey_date_test PRIMARY KEY(id)
)
SQL command when inserting data:
INSERT INTO date_test (timestamp) VALUES( '2019-06-12 12:37:07.880' )
SQL command to retrieve data:
SELECT dt.timestamp ::TEXT FROM date_test dt
returns '2019-06-12 12:37:07.88'
Do you consider this a bug or a feature?
My real issue is: I´m running queries from a C++ program and I have to convert the data returned from the database to appropriate data types. Since the protocol is text-based everything I read from the database is plain text. When parsing timestamps I first tokenize the string and then convert each token to integer. And because the millisecond part is truncated, the last token is "88" instead of "880", and converting "88" yields another value that converting "880" to integer.
That's the default display format when using a cast to text.
If you want to see all three digits, use to_char()
SELECT to_char(dt.timestamp,'yyyy-mm-d hh24:mi:ss.ms')
FROM date_test dt;
will return 2019-06-12 12:37:07.880
It’s a matter of presentation only.
First note that 07.88 seconds and 07.880 seconds is the same amount of time (also 7.88 and 07.880000000 for that matter).
PostgreSQL internally represents a timestamp in a way that we shouldn’t be concerned about as long as it’s an unambiguous representation. When you retrieve the timestamp, it is formatted into some string. This is where PostgreSQL apparently chooses not to print redundant trailing zeros. So it’s probably not even correct to say that it truncates anything. It just refrains from generating that 0.
I think that the nice solution would be to modify your parser in C++ to accept any number of decimals and parse them correctly with and without trailing zeroes. Another solution that should work is given in the answer by a_horse_with_no_name.
My SQL Server 2000 is getting a old date.
I got a column out__days VARCHAR that when people register in my website this column put the date in there like (2003-11-21)
I need to put the actual date there aways, but I try GETDATE() and nothing.
Some one got a solution for this crazy thing?