Convert string to DateTime format in SQL Server

Last Reply 4 months ago By ArnoldDol

Posted 4 months ago

How to Convert date format

Date is 20190101

i am passing it to Stored Procedure.

i want this date to be save into table in '2019-09-11 00:00:00.000' Format.

how to achieve this?

Posted 4 months ago

Hi chetan,

Use Convert function to insert.

SELECT CONVERT(DATETIME,'20190911',101)

 


Posted 4 months ago

First, created a new table named test_timestamp that has a TIMESTAMP column: t1;

CREATE TABLE test_timestamp (
    t1  TIMESTAMP
);

Second, set the session’s time zone to ‘+00:00’ UTC by using the SET time_zone statement.

SET time_zone='+00:00';

Third, insert a TIMESTAMP value into the test_timestamp table.

INSERT INTO test_timestamp(t1) VALUES('2008-01-01 00:00:01');

Fourth, select the TIMESTAMP value from the test_timestamp table.

SELECT t1 FROM test_timestamp;

Fifth, set the session’s time zone to a different time zone to see what value we will get from the database server:

SET time_zone ='+03:00';

Finally, query data from the table:

SELECT t1 FROM test_timestamp;

MySQL Timestamp timezone changes

As you see, we received a different time value adjusted to the new time zone.