I have an column in my source table as [Valyyyymmdd] [nvarchar](24) NULL:
Valyyyymmdd
=================
20130503
20120403
00000000
20110523
20100715
I want to get the difference with getdate(), so I used the below query in my source
DATEDIFF(DAY, IIF( [Valyyyymmdd] = '00000000', CONVERT(VARCHAR(8), GETDATE(), 112), [Valyyyymmdd]) , getdate()) as SalesStageAging
but I need to get the Valyyyymmdd and do ssis derived column to get the difference in date resulting in int value.
Kindly provide me the expression which has to be written in derived column expression
Try this:
DATEDIFF("d",[Valyyyymmdd] == "00000000" ? GETDATE() : (DT_DBDATE)(SUBSTRING([Valyyyymmdd],1,4) + "-" + SUBSTRING([Valyyyymmdd],5,2) + "-" + SUBSTRING([Valyyyymmdd],7,2)),GETDATE())
Assuming [Valyyyymmdd] is a string in the format "yyyyMMdd", we split it into the year, month and date parts, insert hyphens between parts to format it as "yyyy-MM-dd" and then cast it as a date i.e. datatype DT_DBDATE. Finally, we use the conditional operator to check if value equals "00000000", and compare either current date, or the date parsed from input with current date to return the result.
Related
I have a data field with values: 2020Q3, 2020Q4, 2021Q1, 2021Q2, etc and I need to compare if Today's date > the data field value in SQL. For example, Today's date > 2020Q3 should return true. Today's date > 2020Q4 should return false.
I tried string tokenizer to parse and compare but no luck. Please advise. Thanks!
If you have a value like 'YYYY"Q"Q', you can get the start date of the quarter using:
select date(concat(left(yyyyq, 4), '-01-01')) + interval 3 * (right(yyyyq, 1) - 1) month
from (select '2020Q3' as yyyyq) t
And then add three months for the end date. I'm not quite sure how you are interpreting >, but that information should give you what you need for the comparisons.
I'm having trouble with the following code:
INSERT into `fun` ( funner)
SELECT YEAR(STR_TO_DATE(SUBSTRING(time,1,4), '%Y'))
FROM `orig`
returning the warning:
Incorrect datetime value: '1880' for function str_to_date
time is a varchar column in the table orig with the format yyyy/mm.
I want to extract the year section from this varchar and translate it into a year datatype using STR_TO_DATE
I would recommend using one of the usual date and time MySQL datatypes, instead of the rarely used YEAR datatype : DATE, DATETIME and TIMESTAMP.
If you want to turn your string to a date datatype, then :
STR_TO_DATE(my_column, '%Y/%m')
You can use the YEAR() function on this date, and it will return an integer value :
YEAR(STR_TO_DATE(my_column, '%Y/%m'))
Finally : if all you want is get the year from a date stored as string, then you can directly extract it the string using SUBSTR :
SUBSTR(my_column, 1, 4)
This returns a string (not an integer), that MySQL will implictely convert to a number when used in numeric context.
You can convert SUBSTRING(time,1,4) to integer:
SELECT CONVERT(SUBSTRING(time,1,4),UNSIGNED INTEGER) FROM orig
there is no need to convert it first to a date and then convert to integer.
I guess you need to return an integer value since you use the YEAR() function.
If not a simple SELECT SUBSTRING(time,1,4) FROM orig will do.
What does time look like? If year has the data type year, then you can insert a date into the column:
INSERT into `clean` (year)
SELECT DATE(CONCAT(SUBSTRING(time, 1, 4), '-01-01'))
FROM `orig` ;
I am not a fan of the year data type. You should just put the entire date into the column.
I want to concatenate Date And Date Name Together. How Can I do it.
I need output like
2015-10-09 Friday
I got the DateName= `SELECT DATENAME(dw,'2015-10-09') as MyDateName`
Is it possible to make it in a single query?
Try this:
select CAST('2015-10-09' as varchar(10)) + ' ' +DATENAME(dw,'2015-10-09')
SQLFIDDLE DEMO
If your date is already in the varchar format then you dont need to CAST the date and simply try to concatenate them using +
select '2015-10-09' + ' ' +DATENAME(dw,'2015-10-09')
Since the value you provided is already a string, and DATENAME also returns a string, you can just concatenate together with +:
SELECT '2015-10-09 ' + DATENAME(WEEKDAY,'2015-10-09') as MyDateName
Assuming that in reality this is a parameter or a column of datatype datetime, you'll need to convert it to varchar first. There is no built in style for converting to varchar which contains the Day name, so you'll have to do the work yourself in two parts.
See MSDN page on cast and convert for the built in formats.
For the sake of my example, I'll create a datetime variable to use for tests:
DECLARE #d DATETIME; SET #d = '20151009';
Then to convert it to VARCHAR:
For the date part, ODBC is closest to the format you've asked for with the format yyyy-mm-dd hh:mi:ss(24h). So to get the date in that format as a varchar, you can use CONVERT with that style, value, 20.
SELECT CONVERT(VARCHAR(10), #d, 20)
Note I have converted it to VARCHAR(10) to truncate the timepart of the datetime that you don't want in the output. Then if you concatenate this with a space and the day name you already worked out, you can get your output:
SELECT CONVERT(VARCHAR(10), #d, 20) + ' ' + DATENAME(WEEKDAY, '2015-10-09') AS MyDateName
(Replace my variable with a column name and add a FROM statement if you're after output from a table)
You can try this code:
SELECT
CAST(CAST(GETDATE() AS DATE) AS VARCHAR) + ' ' + DATENAME(DW, GETDATE()) AS [DATE_WITH_WEEKNAME]
MS Access Table January2015 has a txndate field with the string "2015-01-01 11:48:00"
The field type is text.
The string needs to be converted to date/time i.e. it should appear in the same format but as a time.
Running this query:
SELECT Format(datevalue(txndate), "dd-mm-yyyy hh:mm:ss") FROM January2015;
gives the output:
01-01-2015 00:00:00
(the time part is being ignored).
How can I fix this?
You can get your desired result with one Format() instead of two.
SELECT Format(CDate(txndate),"dd-mm-yyyy hh:nn:ss") AS Expr1
FROM January2015;
Actually Format() will accept your ymd date string without the need to first convert it to Date/Time, so you could eliminate CDate() if you prefer.
SELECT Format(txndate,"dd-mm-yyyy hh:nn:ss") AS Expr1
FROM January2015;
Note however the datatype of that calculated field will be text, not Date/Time because Format() always returns a string.
SELECT Format(DateValue(txndate),"dd-mm-yyyy") & " " & Format(TimeValue(txndate),"hh:nn:ss") AS Expr1
FROM January2015;
I have an issue in regards to trying to run a sql statement that returns the values of a column called month(of which I have defined as a varchar type, but only has integer values 1-12) as the associated month name. So, for example, the query would return a value of 1 as january. The issue I have is I am trying to use date_format
select date_format(month,'%M')from db.table name
but the values return as null. I was informed that the month values have to be a 'date' type in order for date_format to work. However, the values in this column 'month' are simply integers. So I run into the issue of not being able to assign the date type to the month columns because they're just integers and not correct format for dates? How could I take these single integers and return the month then?
Syntax
DATE_FORMAT(date,format)
Requires date as first param
Check out MySQL date function here:
http://www.w3schools.com/sql/sql_dates.asp
For this you can use this,
SELECT col as MonthNumber,
MONTHNAME(STR_TO_DATE(col, '%m')) as MonthName
FROM table_name
WHERE col <= 12