I have an application which imports a DAT file into some SQL Server tables.
The DAT file sometimes contains incorrect date formats.
Example:
DateCreated = 000100893
The DateCreated column in SQL Server is a datetime type.
The application fails when importing the data due to incorrect format and I have to manually null out these values so I can re-import.
Is there a way in SQL to have restrictions on the datetime column? For example, if the data is not in the correct format, automatically null out the column? I cannot change the datetime datatype for that column because most of the time the dates are correct and I am using this column for other calculations.
You are looking for the TRY_PARSE function:
SELECT TRY_PARSE(DateCreated AS datetime)
It returns null if DateCreated can't be cast into datetime
According to TRY_CONVERT (Transact-SQL), you can do :
SELECT TRY_CONVERT(datetime2, '000100893') AS Result;
Related
I have following value
2020-06-16T13:41:36.000Z
How Can I insert the value into table, What is the column datatype I should use?
I have tried with datetime datatype for column, but that did not work out
The correct data type is datetimeoffset -- although datetime and datetime2 would also work (assuming the values are all in the same time zone). SQL Server stores date/times using an internal format.
create table t (ts datetimeoffset);
insert into t (ts) values ('2020-06-16T13:41:36.000Z');
select * from t;
This returns:
ts
2020-06-16 13:41:36.0000000
This is equivalent to your value but formatted using an arbitrary format. If you want to control the format, then you need to convert the value to a string. One method uses convert() with option 127:
select convert(varchar(255), t.ts, 127) from t
format() provides more flexibility.
You can also add this logic into the table definition:
alter table t add ts_iso8601 as (convert(varchar(255), t.ts, 127));
Here is a db<>fiddle.
I'm having old system which is developed in MS Access and I've a requirement to convert it a web application.
In the old application we have a MS Access Database and we need to import it to SQL Server.
Now there are many tables which have datetime columns with datatype as below:
Access: LongText
SQL Server: nvarchar(MAX)
I've successfully imported the MS Access database to SQL Server but now I'm stuck in datetime conversion issue.
There is a table which have around 86000 records and we need to convert it column to datatype datetime.
currently column have nvarchar(MAX) datatype with
"dd-mm-yyyy" and here is the screenshot for the same:
https://www.screencast.com/t/OP9edlsTdbR
I've tried to fix it by doing google but nothing work as expected.
convert your all string values to appropriate style : 105 : dd-mm-yyyy
ALTER TABLE dbo.TableName ALTER COLUMN DateNeed DATETIME NULL[NOT NULL]
GO
UPDATE TableName SET DateNeed = CONVERT(DATETIME, DateNeed , 105)
This has been asked many times before and I tried quite a bit before posting this question. I have 2 columns on my SQL Server table where the dates are stored as CHAR(8) yyyymmdd format. When I try to insert this to another table with date column, I get:
Conversion failed when converting date and/or time from character string.
The typecasting that I used is:
,CAST(convert(char(10),qrda.BillFromDate,120) AS DATE) AS [BillStartDate]
,CAST(convert(char(10),qrda.BillToDate,120) AS DATE) AS [BillEndDate]
With the above code, I am trying to make it SQL Server readable date format and then typecasting it to DATE so that I can insert it to the destination table without any other transformations. Not sure where I am going wrong with this.
Since date are stored in the following format yyyyMMdd you can insert these values without any need to use CONVERT or CAST functions, since this format can be implicitly converted to DATE.
If the data contains invalid values such as 00000000, you can use TRY_CONVERT or TRY_PARSE function to convert these values to NULL
Example:
CREATE TABLE #TBLTEMP(datecolumn DATETIME)
INSERT INTO #TBLTEMP(datecolumn)
VALUES ('20180101')
INSERT INTO #TBLTEMP(datecolumn)
VALUES (TRY_PARSE('00000000' as DATETIME))
SELECT * FROM #TBLTEMP
Result:
From the example above, you can see that 20180101 was inserted succesfly without any casting, while TRY_PARSE function converted the invalid value 00000000 to NULL.
You can use the following syntax:
INSERT INTO TargetTable(DateColumn)
SELECT TRY_PARSE([CharColumn] as DATETIME
FROM SourceTable
References
Understanding SQL Server’s TRY_PARSE and TRY_CONVERT functions
TRY_PARSE (Transact-SQL)
Sorry for the trouble folks, but looks like this is bad production data where some values are literally '00000000'. I dont know how this flows into our system, but since the source table columns are CHAR(8), this is a valid value. Not so much for me.
Is there a way to insert only date in a datetime column in sql server without the time?
for example
date (datetime)
===============
12-01-2000
16-02-2000
or i should store this as a varchar and cast it when retriving so that i can convert to whatever form i need.
my solution is to store it as varchar and convert it to datetime whenever needed
SELECT CONVERT(VARCHAR(10),GETDATE(),111) -- get datepart only
or
also check this post about creating date type :
create user defined data types:
create type Date from dateTime
http://weblogs.sqlteam.com/jeffs/archive/2007/10/31/sql-server-2005-date-time-only-data-types.aspx
If you are using SQLServer 2008 you can use the date data type.
The following SQL will strip out any time values and set them all to zero. So you won't need to worry whether a time value is there or not.
Select Cast(Floor(Cast(MyDateColumn as float)) as DateTime) as MyDateColumn
From dbo.MyTable
Just use smalldatetime or date. Convert your dates to your format before you update your date values or after you select date values in your app.
You can change format of date format in sql queries or in your app.
Here is a list on date formats in sql
http://www.sql-server-helper.com/tips/date-formats.aspx
Here's a link on date data types
http://databases.about.com/od/sqlserver/a/date_time.htm
Good Luck!
Anyone know how to extract the date from a datetime stamp as part of the where clause?
eg.
select *
from tableA
where date between '01/08/2009' and '31/08/2009'
(Date is a timestamp!)
Many thanks,
Fiona
If this is sql server, it's not possible. The timestamp data type's name is misleading, as it does not store any date information of any kind. All it holds is a sequential value that allows you to establish record order (eg, item A was created before item B), and therefore you don't have enough information in that column alone to know on what day the row was created.
Since the link I provided is Sql Server 2000 specific, also check this link for information on SQL Server 2008:
http://msdn.microsoft.com/en-us/library/ms182776.aspx
timestamp is the synonym for the rowversion data type and is subject to the behavior of data type synonyms. In DDL statements, use rowversion instead of timestamp wherever possible.
To build a real timestamp column in Sql Server, use a DateTime (or DateTime2) data type and set it's default value to getdate() or current_timestamp.
If a real datetime value, not TIMESTAMP/ROWVERSION which is binary(8)...
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, #MyValue), 0)