save string to a date field in sql server - sql-server

I have the following string that I need to save to a sql server database column that has a date datatype.
'2017-05-25T00:00:00'
Can I use cast within a insert script to save this value and is this the best way?

yes You can insert the value like this
INSERT INTO YourTable(ColumnName)
SELECT CAST(StringFiled AS DATETIME)
You Can cast it as Date,DateTime or any other Date type you need

You can use any of the following options.
insert into myTable
values (CAST('2017-05-25T00:00:00' as datetime))
insert into myTable
values (Convert(varchar(30),'2017-05-25T00:00:00',102))

Related

Casting an inserted string to a date in the same insert

SQL Server 2017 - although this post is quite old, I still referred to it: Using cast in insert statement (the answer with 6 votes), but I'm getting an error when trying to perform this simple INSERT statement:
INSERT INTO dbo.Table (Name, DateOfBirth, DateOfBirth_Normalized)
VALUES ('John Doe', '1943-09-08', CAST(DateOfBirth AS DateTime))
The error I'm getting is that the DateOfBirth inside the CAST statement is an invalid column name.
How can I achieve this?
FYI, DateOfBirth is nvarchar(50) and DateOfBirth_Normalized is DateTime.
EDIT 1: this is for a stored procedure, so the DateOfBirth value will be different for every INSERT and therefore can not simply be passed as the string value, as suggested in the comments.

SQL Server date conversion from string value

In my table, I have a string-represented date column in the following format: {^2013/05/29}.
How do I convert this to standard datetime format in SQL Server? This contains string characters that are not part of what a datetime string usually has.
That format is recognizable as a strict format by VFP only. Is that stored in SQL Server as text? If so:
Select cast(substring(myColumn, 3, 10) as date) as myDate from myTable;
would do the conversion.
If you mean it is stored like that in a VFP table and you want to convert a date then:
select ctod(myColumn) as myDate from myTable;
If the data is always in the format {^yyyy/MM/dd} then you could use:
CONVERT(date,REPLACE(SUBSTRING(YourDateColumn,3,10),'/',''))
Ideally, however, you should be fixing your column to be the correct datatype:
CREATE TABLE Test (DateColumn varchar(13));
INSERT INTO Test VALUES ('{^2013/05/29}');
GO
SELECT *
FROM Test;
UPDATE Test
SET DateColumn = CONVERT(date,REPLACE(SUBSTRING(DateColumn,3,10),'/',''));
SELECT *
FROM Test;
ALTER TABLE test ALTER COLUMN DateColumn date;
SELECT *
FROM Test;
GO
DROP TABLE Test;
SELECT CAST(REPLACE('^2018/05/29','^','') AS DATETIME2)

Convert to mm/dd/yyyy and select max value

Using SQL Server 2014, I have a date field named LAST_BASELINE_UPDATE_DATE that is stored as datetime.
I used the CONVERT function to convert to mm/dd/yyyy:
convert(date,LAST_BASELINE_UPDATE_DATE,101) as BASELINE_UPDATE_DATE
What I want to be able to do is then select the MAX value of BASELINE_UPDATE_DATE without creating a table. Is this even possible?
It's a little unclear from your post what your data is and what you're trying to get out. Here are a couple solutions, hopefully one of which is applicable
Assuming you want your result as a string formatted mm/dd/yyyy you can do this
select convert(varchar(10), max(LAST_BASELINE_UPDATE_DATE), 101))
from YourTable
If you just need it as a date, just do
select max(LAST_BASELINE_UPDATE_DATE)
from YourTable
if LAST_BASELINE_UPDATE_DATE is already a string (formatted mm/dd/yyyy) and you want it as a date,
select max(convert(date, LAST_BASELINE_UPDATE_DATE, 101))
from YourTable
I think you are complicating this. Just do the conversion on the max datetime values.
declare #table table (LAST_BASELINE_UPDATE_DATE datetime)
insert into #table
values
('20160701 12:21'),
('20160705 03:21'),
('20160401 19:21'),
('20161201 04:21')
select
convert(varchar(10),max(LAST_BASELINE_UPDATE_DATE),101)
from
#table
This method converts your single returned row, which is the max() value of your datetime columns, as opposed to converting every row and then finding the max value.

Automatic adding current time in SQL Server 2012

I have table with a column of datatype time(4).
When I'm inserting values into the table I need to auto insert just the current time with milliseconds (without date) into that column. I have tried with time stamp, date time... but without any success.
If you want to get the current time in SQL Server 2012, just use the CAST operator to achieve this:
SELECT
CAST(SYSDATETIME() AS TIME(4))
This will get the current date & time, and cast this to just the time, as you need it.
To achieve automatic entry of time values when a new row is added, you can use the above expression in a default constraint. For example:
DECLARE #T AS table
(
SomeValue integer NOT NULL,
TheTime time(4) NOT NULL
DEFAULT CAST(SYSDATETIME() AS time(4))
);
INSERT #T (SomeValue)
VALUES (1);
SELECT
SomeValue,
TheTime
FROM #T;
As far as I understand, this is what you may be looking for:
SELECT GETDATE() 'Today',
CONVERT(TIME(4),GETDATE()) 'time_only'
You could use the above conversion in your insert statement as following:
INSERT INTO TABLEA (
column1
,column2
,record_insert_time
)
VALUES (
value1
,value2
,CONVERT(TIME(4), GETDATE())
);

Ms Sql Convert and insert between 2 databases

I have two databases.I insert data from DATABASE_2 TO DATABASE_1 however i need to convert some column.
I must convert Customer_Telephone_Number from varchar to bigint after that insert it.
So,
My Question is in below.
SET IDENTITY_INSERT DATABASE_1.dbo.CUSTOMER_TABLE ON
INSERT INTO DATABASE_1.dbo.CUSTOMER_TABLE
(
Customer_Id,
Customer_Telephone_Number
)
Select
Customer_Id,
Customer_Telephone_Number // This is varchar so i need to convert it to Big int.
from
DATABASE_2.DBO.CUSTOMER_TABLE
Any help will be appreciated.
Thanks.
If the data stored is without spaces or other non numeric symbols:
Select
Customer_Id,
CONVERT(BIGINT,Customer_Telephone_Number)
from
DATABASE_2.DBO.CUSTOMER_TABLE
For instance, if there is value with (222)-3333-333 it would fail. If the value was 2223333333 it would succeed

Resources