I am trying to use DATEADD function in a derived column transformation but it seems the expression i am using is wrong. Initially, i was using this in my SQL query which works fine but i want to use it now in a derived column so i can transform the date value before i bring it into my table. Here is the sql i was using:
select DATEADD(day,myDate,'19600101') as NewDate from myTable
but i want to use it now in derived column so i am replacing the column and using this in the expression:
DATEADD("day", myDate, "19600101" )
I just had the same problem, here is the fix:
instead of:
DATEADD("day", myDate, "19600101" )
try:
DATEADD("day", (DT_DBDATE)myDate, (DT_DBDATE)"19600101")
This will return a database datestamp, which can be cast back to a string if needed. In my case, I am converting from Epoch time (milliseconds since 1/1/1970), so I need this:
DATEADD("s",(DT_I8)[Index Time],(DT_DBTIME)"1/1/1970")
Please see the documentation for dateadd: http://msdn.microsoft.com/en-us/library/ms141719.aspx. I am not sure what you are tryinmg to do with this expression. If you wanted to add one day then it would be:
select DATEADD(d,1,myDate) as NewDate from myTable
If you wanted to subtract one day then it would be:
select DATEADD(d,-1,myDate) as NewDate from myTable
Please explain what you are trying to do. I do not think the first arguement should be inside double quotes. Also "day" is not allowed according to the remarks section in my link.
Related
I am aware that this is a very common and have tried using the solutions suggested on similar questions. However, I cant seem to make it work.
I have a column which contains dates, but it is of varchar datatype.
The data looks like this:
startdate
-----------
15/01/2007
29/06/1998
07/12/2001
and so on..
I tried the following command
try_convert(datetime, startdate) as 'startdate'
But it returns a lot of NULLs even when there is a date populated in the original column.
Can someone please help?
The try_convert method accepts three arguments: Data type, Expression, and Style.
You need to use the Style argument:
try_convert(datetime, startdate, 103) as 'startdate' -- 103 is dd/MM/yyyy
(a list of supported styles can be found here)
Also, You should store date values as Date, not as varchar.
For more information, read Aaron Bertrand's Bad habits to kick : choosing the wrong data type
I have a SQL Server 2008 database with a transaction table. There is a field defined as NVARCHAR(16). The data stored is date and time formatted like this:
2016100708593100
I need to write a query that looks at that field and pulls data between two dates
select ... from...where convert(varchar,
convert(datetime,left(a.xact_dati,8)),101)
between '9/29/2016' and '10/05/2016'
I have tried other converts but nothing returns any data. If I use >=getdate()-1
I get data so I should be seeing something returned. Any suggestions?
Replace WHERE Clause with
... WHERE CONVERT(DATETIME,LEFT(a.xact_dati,8)) BETWEEN '9/29/2016' AND '10/05/2016'
Assuming YMD order just
where cast(left('2016100708593100', 8) as date) between '20160929' and '20161005'
Note this clause is not satisfied.
The problem is you're trying to do a date comparison with varchar data.
You go half way to converting the initial value to datetime but then back to varchar.
You could rely on Sql to implicitly convert the between parameters to datetime.
select 'matched',convert(datetime,left('2016100708593100',8),112)
where convert(datetime,left('2016100708593100',8),112) between '2016/09/29' and '2016/10/10'
Take the date part of the string and CAST it as DATE and then compare it with the proper date format.
select ... from...
where cast(left('2016100708593100', 8) as date) between '2016-09-29' and '2016-10-10'
a very straight and fast approach could be to keep this in BIGINT
Something like this:
cast(left(a.xact_dati,8) as bigint) between 20160929 and 20161005
But anyway this will not be sargable and therefore cannot use an index.
You might even stick to varchar, as the YYYYMMDD format is safe with alphanumerical sorting. Cutting a string with LEFT is sargable (AFAIK):
left(a.xact_dati,8) between '20160929' and '20161005'
I want to do the correct way to get a date from a #parameter that contains the complete datetime, that user gets from a calendar from TFS File.
In the select I would want to use something like CONVERT(varchar(10), #FechaHasta.Value, 120) AS DATE01 and then get the only date, throwing out the time from the parameter.
For the next step, I would want to compare it with another date in WHERE clause, having this code :
Then, I would want to make this work on for looking for between two dates, and the last one, throwing out the time from the datetime.
Thanks.
I am assuming #FetchHasta is a datetime
CONVERT(VARCHAR(10), #FetchHasta, 101)
is what gets you just the date part. i.e. 10/28/2014 1:10 PM would simply become 10/28/2014.
Source
EDIT: Alternatively, How to return the date part only from a SQL Server datetime datatype
I know how to use CONVERT function in SELECT statement to change the format of the Date column:
SELECT
StationID
, CONVERT(varchar, [Date], 101) as Date
, Value
FROM my_table
But I was wondering if I can set the date format in general before running the SELECT statement, when I don't know the name of the date column in the following SELECT statement:
SELECT * from FROM my_table
Is any SET statement or other T-SQL that I can run before my SELECT statement so that I can change the Date format temporarily?
Thank you
No.
In particular, any date columns which you select are not actually formatted at all, but are instead returned down the wire as an actual piece of date data in a binary "format" which is used for dates. If you are seeing them formatted, it's because your client (either management studio or some other tool) is converting them to strings to display.
When you use SELECT *, there is obviously no way to tell SQL Server to do any conversions on any particular columns, so the data is going to be returned in whatever the data types of the underlying query returns. So regardless of whether your data types are really date or not, no manipulation is going to happen at that point anyway.
I'm pretty sure there's no way to do what you're asking. However, there are ways to format the date string when you output it using your programming language.
I'm currently getting the error:
java.sql.SQLException: ORA-01843: not a valid month
which I assume is to do with the way I am setting a timestamp...
So I have a query like this :
select * from A_TABLE where A_TIMESTAMP_COL < '1252944840000'
But it doesn't work...and I don't want to have to convert it to a date ideally. Is there some special syntax to tell Oracle that this is a timestamp?
You can use the to_timestamp() function to convert your string into a timestamp value: http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/functions201.htm#sthref2458
I think you can cast the date column to a char and do something like:
select * from A_TABLE where to_char(A_TIMESTAMP_COL) < '1252944840000'
This should allow you to compare strings, not dates.
Use the java.sql convenience methods to take that milisecond time and turn it into a Date or Timestamp.
You can use this query:
SELECT * FROM A_TABLE WHERE TIMESTAMP < (SYSDATE - 10/1440)
Where (SYSDATE - 10/1440) means SYSDATE - 10 Minutes.
Also see some examples here.