Current quarter variable issue - sql

I have the below Query I am trying to genialize the dates to calculate current quarter in Sienese.
.Am I doing something wrong and what will be the correct way?
I have tried concat(year(current_date()),'Q',quarter(current_date())).It works in dbvisualizer but not in sisense.

The message is indicating a value you are trying to convert to date is null, it is not a matter of how you convert it, it is that null can never be converted to anything. You can review the data and change the conditions of what gets returned to avoid that, or use ISNULL() for error control possibly by specifying a date to use by default..
https://www.w3schools.com/sql/func_sqlserver_isnull.asp

Related

Test for zero datetime in SSIS Conditional Split

A value coming back from a DB2 query passes a zero date as 0001-01-01 00:00:00.000000 and I am having a problem testing for this value in a Conditional Split condition. I tried a few different things but either they stay red or cause an error when run. I even tried testing for the date being less than the SQL start date. Neither of these work.
(DT_Date)DRIVER_TERMDATE < (DT_DATE)"1753-1-1"
DRIVER_TERMDATE != "0001-01-01 00:00:00.000000"
The minimum date for the DT_DATE data type is 1899-12-30, which is after that date and trying to convert it to this data type will cause an error. If you're importing the DRIVER_TERMDATE as text, trying adding a condition for this string, for example
DRIVER_TERMDATE == "0001-01-01 00:00:00.000000"
However it looks like you already tried checking to see if DRIVER_TERMDATE is not equal to this value, so see if trailing zeros are being removed during from the text during execution by right-clicking the output from the DB2 source, selecting Enable Data Viewer, and examining the data when the package runs. Once you've confirmed the exact string that's being sent in, you can add a condition for this.

linq query convert text to date to compare with another date vb.net

I have a database field, Field260, that stores date representations as text. For instance a value for 3/29/2018 would be stored as a string "03/29/2018"
Dim Db = New DataClasses1DataContext
Return From cf In Db.FileDatas
Where cf.Field260 <= System.DateTime.Today
Returns error
System.Data.SqlClient.SqlException: 'Explicit conversion from data type text to datetime is not allowed.'
When I tried to Parse the date
Where DateTime.Parse(cf.Field260) <= System.DateTime.Today
I receive
System.NotSupportedException: 'Method 'System.DateTime Parse(System.String)' has no supported translation to SQL.'
I'm stumped.
I encountered a similar issue - have an app that I used to have a DatePicker for a field, and my database field defined as DateTime, but the users absolutely wanted to copy/paste whatever garbage they wanted into the field. Problem is, I'm allowing them to search for records by that "Date" field.
As a workaround, I do some basic validations on the garbage they enter to attempt to only allow dates (IsDate function captures most errors, but still allows them to enter years like 208 instead of 2018, so I also check that Year(DateValue(txtDate.Text))>2000, which works for me because it's an application-received date, so should never be older than this year). Then I store it as varchar in the database.
When searching for the records, you have two options - either grab all records, then perform a date conversion on the list returned to pare down the results, or hope and pray that your faulty LINQ WHERE clause will get everything you want.
Figured it out cuz I'm a genius.
Return From cf In Db.FileDatas
Where cf.Field260.ToString() <= System.DateTime.Today.ToString("MM/dd/yyyy")
Somehow comparing string to string works, who knew!

Redshift CONVERT_TIMEZONE Issue

I am facing an issue with the time conversion.
Whenever I tried to change the time to timestamp. It does not work.
I have tried many of functions from http://docs.amazonaws.cn/en_us/redshift/latest/dg/Date_functions_header.html
But still a single function did not convert the date time to timestamp. An eg. I need date time (2017-12-11 23:38:11) as timestamp (1513036800) format.
Can you please guys tell me the exact function to convert that?
Also I have tried to use CONVERT_TIMEZONE function.
It works fine, whenever I passed the static date inside this like: CONVERT_TIMEZONE('GMT','GMT -1','2017-12-11 23:38:11')
But if I change the date with my variable name, I am getting an error message:
"ERROR: function convert_timezone("unknown", "unknown", character varying) does not exist HINT: No function matches the given name and argument types. You may need to add explicit type casts."
Can you guys tell me function to convert the date into timestamp?
Can you guys tell me reason of the above error while passing the dynamic variable?
I want to add timezone and date together and then to get the timestamp value of that, is that possible?
Dateadd function gives the same error while passing the dynamic variable.
Per error message it looks like your timestamp is stored in a varchar column. You have to change the column to timestamp or convert the value on the fly to be able to use timestamp functions. I suggest the first because it has better performance. Redshift doesn't support column data type alteration, so to do this you have to recreate the table DDL with timestamp data type for time columns, insert data to the new table and replace the old table with it.
As for dynamic conversion, it's like this:
EXTRACT('epoch' FROM CONVERT_TIMEZONE('GMT','GMT -1',your_column::timestamp))
you just don't need to use ::timestamp part if you convert the column

Format a calculated field as Date SQL Script for Qlik Sense

Database is in oracle, my machine has odbc 32 bits configured with the name PPM and I've created a script but need assistance formatting a calculated field:
LIB CONNECT TO [PPM];
[SCRIPT]:
SQL Select NP.BUSINESS, NP.PLATFORM, NP.CATEGORY,
CASE WHEN IMPLE IS not NULL THEN IMPLE
WHEN CONTRACT IS not NULL THEN CONTRACT
WHEN ANALYSIS IS not NULL THEN ANALYSIS
ELSE IDEA END AS REALDATE,
Imple, Contact, Analysis, Idea are all fields with same format of dates and this code is needed so we can have always the last date input as "Realdate"
This gives back a correct Date in the new field (doesn't exist in the server) but shows many duplicates, using SET TimestampFormat='MM-YYYY'; before, it gives back the dates with the correct format but shows "03-2015" 3 times and many others dates like that. Trying also: date(floor("REALDATE",'MM.YYYY') as "REALDATE2" but since this is a calculated field I don't know how to make reference that field.
Thanks
In QlikView, the function Date#(expression [ , format-code ]) could help you. You can evaluate any expression with a specific date format.
Hope this could help, but your question is not very clear, if you could add more information it would be very useful.
Looks like Qlik is picking up the numeric value and needs to be converted to a date. date# is used to render date strings into a date value.
LIB CONNECT TO [PPM];
[SCRIPT]:
Load BUSINESS
,PLATFORM
,CATEGORY
,date(REALDATE) as REALDATE;
SQL Select NP.BUSINESS, NP.PLATFORM, NP.CATEGORY,
CASE WHEN IMPLE IS not NULL THEN IMPLE
WHEN CONTRACT IS not NULL THEN CONTRACT
WHEN ANALYSIS IS not NULL THEN ANALYSIS
ELSE IDEA END AS REALDATE,
I found a way around after long hours of trying:
DATE#(DATE(Date#([REALDATE], 'MM-YYYY'), 'MMM/YYYY'), 'MMM/YYYY')
Could not format the field inside the script but this does the trick
Thanks for the help!

How can I store date only in datetime field in WebMatrix with Sql Server CE?

I was wondering if there was a way to store a date (example: 01/01/2013) as datetime without SQL Server CE adding the time (example: 12:00:00 AM).
I could always store it as the string "01/01/2013" but I really want to be able to compare the dates on querying the database.
I realize that as long as I only stored the date part, all of the times in the datetime field would have equal values (i.e. 12:00:00 AM), so comparing them wouldn't be a problem and I could just always ignore the time part, however, it seems ridiculous to have this unnecessary data appended to every entry in the table.
Is there a way to store only the date part of the datetime as datetime so that the dates can still be compared in the SQL query or do I just need to live with this overhead and move on?
Side Note:
I just spent the last 30 minutes searching Google and SO for an answer I was sure was already out there, but to my surprise, I couldn't find anything on this issue.
Update:
The conclusion I have come to is that I will just accept the time in the datetime format and let it always default to 12:00:00 AM by only adding the date part during the INSERT statement (e.g. 01/01/2013). As long as the time part always remains the same throughout, the dates will still be easily comparable and I can just trim it up when I convert it to string for screen display. I believe this will be the easiest way to handle this scenario. After all, I decided to use SQL for the power of its queries, otherwise, I might have just used XML instead of a database, in the first place.
No you really can't get rid of the time component. It is part of the data type defined by sql server. I was very annoyed by it until I found that I could still display the dates without the time using JQuery to reformat them with the date formatter plugi:
https://github.com/phstc/jquery-dateFormat
Good Luck!
select CONVERT(date, GETDATE())