insert timestamp into MS SQL - sql

I am working on pulling some data in from a Rest-api into my MSSQL database. The issue I am having is the timestamp that I am being given from the api does not appear to be formatted correctly to just insert '2013-09-16T07:00:00+0000'.
example insert:
INSERT INTO [page_fan_adds_unique]([period], [title], [description], [value], [end_time])
VALUES ('day', 'Daily New Likes', 'Daily: The number of new people who have liked your Page (Unique Users)','0', '2013-09-16T07:00:00+0000')
I know changing the format to 2013-09-16T07:00:00+00:00 works but I didn't want to have to manipulate the data before the insert.

You cannot do this without formatting.
From the MSDN about TIMESTAMP:-
Is a data type that exposes automatically generated, unique binary
numbers within a database. timestamp is generally used as a mechanism
for version-stamping table rows. The storage size is 8 bytes. The
timestamp data type is just an incrementing number and does not
preserve a date or a time. To record a date or time, use a datetime
data type.
You may use datetime data instead.

This is what I did. from my PHP code I inserted that date [end_time] = CAST(STUFF('".."',23,3,':00')AS DATETIME2)"
Now when I write my "select" statements I just cast(end_time as datetime) I find that this is a lot easier than trying to cast a string into a datetime in sql.

Related

Can’t insert NULL when variable could be either date time or NULL using labview

I am using labview to query an SQL table, I am manipulating the data and inserting it into a new table. Two of the columns are datetime and can either be a date or a NULL but the format into string needs quotes ‘%s’ to insert the datetime or I get an error whereas the NULL will only insert when I have no quotes %s.
Is there an easy way to solve this for both? As I am sending in rows of about 30 columns and it is only when I come across a NUll in the datetime column that it errors?
Change your SQL query for the select to use an ifnull and return a date you know would be null. I would suggest the LabVIEW epoch of 1st January 1904 formatted to match whatever is normally in your database

GETDATE() insert with spaces SQL Server

I have three tables in database. I have the column Operation_Date in all of them. I set the column default value to (GETDATE()), and its data type is VARCHAR(50) and I don't have any coding in my C# Windows application that inserts value to Operation_Date.
Issue: When I insert rows to tables the operation_Date separates month, day and year with space like this (MAY 2 2020). I get two spaces after month and one space after days.
I tried to make a query on SQL Server to insert and the same problem occurs. I tried changing data type to datetime but with not success.
Note: this issue occurs only in two tables. And I am pretty sure the three tables have the same settings.
Is there anyway to force a certain date format for the default value in SQL Server? And what could make this issue happen?
This is the query I used:
USE [EasyManagementSystem]
INSERT INTO [dbo].[Attendance] ([Employee_No], [Present], [Leave], [Othe_Leave], [Month], [Year])
VALUES ('l-8068', 30, 0, 0, 'MAY', 2020)
Huh? Why would you be storing a date as a string. That is just wrong. There is a perfectly good data type for dates, called date.
If you want this to be set on input, then just give it a default value. Your table creation should look like:
create attendance (
. . .
operation_date date default getdate()
);
Voila! It will just work. If you want the month or year, you can use the month() and year() functions.

Why can't I insert yyyymmdd (stored as text) to a date column

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.

How to prevent CAST errors on SSIS?

The question
Is it possible to ask SSIS to cast a value and return NULL in case the cast is not allowed instead of throwing an error ?
My environment
I'm using Visual Studio 2005 and Sql Server 2005 on Windows Server 2003.
The general context
Just in case you're curious, here is my use case. I have to store data coming from somewhere in a generic table (key/value structure with history) witch contains some sort of value that can be strings, numbers or dates. The structure is something like this :
table Values {
Id int,
Date datetime, -- for history
Key nvarchar(50) not null,
Value nvarchar(50),
DateValue datetime,
NumberValue numeric(19,9)
}
I want to put the raw value in the Value column and try to put the same value
in the DateValue column when i'm able to cast it to Datetime
in the NumberValue column when i'm able to cast it to a number
Those two typed columns would make all sort of aggregation and manipulation much easier and faster later.
That's it, now you know why i'm asking this strange question.
============
Thanks in advance for your help.
You could also try a Derived Column component and test the value of the potential date/number field or simply cast it and redirect any errors as being the NULL values for these two fields.
(1) If you just simply cast the field every time with a statement like this in the Derived Column component: (DT_DATE)[MYPOTENTIALDATE] - you can redirect the rows that fail this cast and manipulate the data from there.
OR
(2) You can do something like this in the Derived Column component: ISNULL([MYPOTENTIALDATE]) ? '2099-01-01' : (DT_DATE)[MYPOTENTIALDATE]. I generally send through '2099-01-01' when a date is NULL rather than messing with NULL (works better with Cubes, etc).
Of course (2) won't work if the [MYPOTENTIALDATE] field comes through as other things other than a DATETIME or NULL, i.e., sometimes it is a word like "hello".
Those are the options I would explore, good luck!
In dealing with this same sort of thing I found the error handling in SSIS was not specific enough. My approach has been to actually create an errors table, and query a source table where the data is stored as varchar, and log errors to the error table with something like the below. I have one of the below statements for each column, because it was important for me to know which column failed. Then after I log all errors, I do a INSERT where I select those records in SomeInfo that do not have an errors. In your case you could do more advanced things based on the ColumnName in the errors table to insert default values.
INSERT INTO SomeInfoErrors
([SomeInfoId]
,[ColumnName]
,[Message]
,FailedValue)
SELECT
SomeInfoId,
'PeriodStartDate',
'PeriodStartDate must be in the format MM/DD/YYYY',
PeriodStartDate
FROM
SomeInfo
WHERE
ISDATE(PeriodStartDate) = 0 AND [PeriodStartDate] IS NOT NULL;
Tru using a conditional split and have the records where the data is a date go along one path and the other go along a different path where they are updated to nullbefore being inserted.

extract date from datetime stamp

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)