How to make a Varchar column GETDATE() actual? - date

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?

Related

Postgres INSERT timestamp with UNION ALL error

Good day!
I need to export/import data from SQL Server 2019 to AWS RDS running PostgreSQL 13.3
It's just a few hundred rows from a handful of tables.
This is my first ever encounter with Postgres, so I decided to simply script data as "INSERT ... SELECT", as I would with SQL Server... and I've looked into AWS Glue, RDS S3 Import - it all seems waaay too much for what I need.
I am using DBeaver v21 for of this as I have easy access to both source and destination DBs.
This I tested with success:
CREATE TABLE public.invoices (
invoiceno int8 NOT NULL GENERATED BY DEFAULT AS IDENTITY,
terminalid int4 NOT NULL,
invoicedate timestamp NOT NULL,
description varchar(100) NOT null
);
INSERT INTO public.invoices(InvoiceNo,TerminalID,InvoiceDate,Description)
SELECT 7 as invoiceno , 5 as terminalid , '2018-10-24 21:29:00' as invoicedate , N'Coffe and cookie' as description
-- Updated Rows 1
-- No problem here
I scripted the rest of the data with UNION ALL, like so (shortened example) :
INSERT INTO public.invoices(InvoiceNo,TerminalID,InvoiceDate,Description)
SELECT 7 as invoiceno , 5 as terminalid , '2018-10-24 21:29:00' as invoicedate , N'Coffe and cookie' as description
UNION ALL
SELECT 1000, 5 , '2018-10-24 21:29:00' , N'Tea and crumpets'
and now I get:
SQL Error [42804]: ERROR: column "invoicedate" is of type timestamp without time zone but expression is of type text
Hint: You will need to rewrite or cast the expression.
Position: 118
I do see in the message that it can be "fixed" with a CAST (or rewrite!)....
but how come Postgres can convert 1 row implicitly, yet 2 rows is impossible?
why does this fail when more than 1 row is being inserted? - it clearly knows how to convert text -> date ...
I tried using VALUES, CTE, derived tables with no success.
As I have to spend more time with postgres - I really would like to understand what's going on here. Is my syntax wrong (works fine SQL Server), is DBeaver messing up something with my data, etc...?
Any suggestions would be appreciated.
Thank you
'2018-10-24 21:29:00' is a string value and Postgres is a bit more picky about correct data types then SQL Server.
You need to specify the value as a proper timestamp constant,
timestamp '2018-10-24 21:29:00'
Note that you can write that in a bit more compact form using a values clause:
INSERT INTO public.invoices(InvoiceNo,TerminalID,InvoiceDate,Description)
values
(7, 5, timestamp '2018-10-24 21:29:00', 'Coffe and cookie'),
(1000, 5 , timestamp '2018-10-24 21:29:00' , 'Tea and crumpets');
The reason of such behaviour is in order of compilation.
In situation when you use VIEW first are compiled querys in view and types of columns (names too) in view is taken from the first part of a "view" (the first SELECT command).
So, you have got text instead of timestamp and it doesn't match to inserted table type.
MSSQL compiler is a little bit smarter :-).
In first example you have simple INSERT INTO ... SELECT ....
and compiler at once expect timestamp type - so , it not rise any compilation error (but error can ocure in execution time when the data do not pass rules of automatic conversion).

Replace on Date colum

I'm trying to run a replace and update on a date column in Postgresql.
UPDATE table SET date_time = REPLACE(date_time, '12:', '00:');
I've tried casting as text. But still errors.
The error I get is
function replace(timestamp without time zone, unknown, unknown) does not exist
I'm guessing it's not picking it up due to the column's data type. But I'm not quite sure how to achieve what I'm after now.
Cast date_time to text, do the replacement and then cast the result to timestamp again. BTW, what are you trying to achieve? Isn't it a presentation format issue?
UPDATE "table"
SET date_time = replace(date_time::text, ' 12:', ' 00:')::timestamp;

Using DDL, how can the default value for a Numeric(8,0) field be set to today's date as YYYYMMDD?

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

T-SQL date conversion dd/mm/yyyy to yyyy-mm-dd

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.

make a column in sybase default to the current date/time of row insertion

I have a sybase 15 DB and for one of my tables, I want to make a column default to the current date/time of the row insert. Is this possible?
In a sybase text, the following is said:
ALTER TABLE sales_order
MODIFY order_date DEFAULT CURRENT DATE
On my DB this doesn't do anything, as CURRENT DATE is not recognized.
using getDate() is a valid solution, you must have had a syntax error. Try it like this:
create table test_tbl (
date_data DATETIME default getDate() NOT NULL
)
Try using getDate() instead
... DEFAULT GETDATE() is correct. the case is irrelevant; mixed case may indicate a Java method, but it is a straight TSQL Function. Please post the exact error msg if you want further assistance.
Also, the ALTER TABLE method sets the Default for future INSERTS; if you want the existing data changed, you need to UPDATE (good for small tables) or unload/reload the table (demanded for the large).
Watch the NULL/NOT NULL: you do not want to change that without understanding. Again, the existing/future issue needs address. NOT NULL prevents NULL being explicitly passed as an INSERT VALUE.
CURRENT_DATE is a SQL standard that isn't universally adopted.
As noted elsewhere the getdate() T-SQL function should be used instead.