varchar yy/mm/dd to date azure sql server - varchar

Im trying to convert a varchar value into a date format.
The varchar value is 99/01/30and the result should be 1999-01-30.
Would somebody help me with this?
I'm trying to do this but :
select convert(date, convert(varchar, '99/01/30', 11),23) as date
but this gives me an error: Conversion failed when converting date and/or time from character string.

This
select convert(date, '99/01/30', 2) as date
will get you what you want. See also this page for more information about what the 2 means

Related

Nodejs Service returning datetime instead of just date when trying to cast datetime field to date of a SQL Server Query

My database is SQL Server. I have the following sql query
SELECT RQI,RQIOver,PCI,PCIOver,PQI,PQIOver,SR,SROver,IRI,IRIOver,RUT,RUTOver,cast(DateCollected as date) as DateCollected,cast(DateCollectedOver as date) as DateCollectedOver from dbo.PF_Condition where SegmentId=12665
The result I am getting for DateCollected is just the date, which I need, when run it in SQL Management Studio. However when I put the same SQL query in a nodejs service which I created the result is not the same.
I needed to extract YYYY-MM-DD from datetime field DateCollected and DateCollectedOver as mentioned in the query above.
Can someone help me with it?
I made it work with the slice operator of javascript and sliced the date part only and displayed in the frontend where I need to. The results returned are of type string so the slice operator worked.
If someone can help why the cast function is not working in the sql query when I put it in the nodejs service it will be really helpful
After casting as DATE, you can then use the LEFT function to truncate the result to 10 characters:
LEFT(cast(DateCollected as date), 10) as DateCollected
which would return a string with the format 'YYYY-MM-DD'

Change date format from database

I'm using Stimulsoft Reports.Ultimate 2016.1. My date format from the database is dd-mm-yyyy. Is there any way that I can change the format into dd/mm/yyyy and display it like that?
You can achieve it by following query. Replace dbDate and table_name with yours.
If dbDate is in varchar format you need to convert it first to datetime and then you can apply the below query.
select convert(NVARCHAR,convert(datetime,<dbDate>),101) from <table_name>
If dbDate is already in datetime format then you can simply apply below query to format date into dd/mm/yyyy
select convert(NVARCHAR,<dbDate>,101) from <table_name>
If you have any suggestions or doubt let me know.

Get the Value of Year from a DateTime Column in Azure U-SQL?

I am using U-SQL script to get the Datetime column data from the Azure DataLake Analytics Table. I am having a date of joining datetime column in a employee table, I want display only the year of joining from that column. There may be a chance of getting null value from the table.
joineddate.HasValue ? joineddate.Value.Year.ToString() : joineddate AS JoinedDate
The above statement shows a error,
Cannot implicitly convert type 'string' to 'System.DateTime'
Please Suggest me a way to do this thing.
Thanks in Advance!
I have used ToString to the datetime column and got the desired output.
string.IsNullOrEmpty(joineddate.ToString()) ? joineddate.ToString() : joineddate.Value.Year.ToString() AS JoinedDate
Thanks.

Oracle: How to convert a string into date format

Have done a lot of search before asking this question, like How to convert a string into date format, How to convert a string into date, but still can't figure it out.
So, here's the question, how to convert these string dates into date in Oracle:
"2016-08-15 10:45:30" (String type) -> 20160815 (Date type)
"20160815104530" (String type) -> 20160815 (Date type)
Any idea will be appreciated.
You are mixing two things here:
The first is the conversion of a String data type, in Oracle VARCHAR2 into a DATE data type.
The DATE data type has a precision of seconds, you can't change that. A DATE data type will always give you the date and time component, i.e year, month, day, hours, minutes and seconds: Oracle SQL Data Type Documentation
However, the second part of what you are asking is about how to format the date when retrieved. This is helpful when running reports, or other kinds of visual display of dates. For example, in the US you would most likely want your date columns appear in the format MM/DD/YYYY while everywhere else in the world you most likely want to stick with DD/MM/YYYY. Oracle lets you do that by telling it what NLS_DATE_FORMAT you want to use. You can set that parameter for each individual session as well as on database level, it is up to you (and your DBA) to decide where and when you want to set that. In your case you can apply this via the ALTER SESSION command:
SQL> ALTER SESSION SET nls_date_format='YYYY-MM-DD';
Session altered.
SQL> SELECT TO_DATE('2016-08-15 10:45:30', 'YYYY-MM-DD HH24:MI:SS') FROM DUAL;
TO_DATE(
----------
2016-08-15
SQL> SELECT TO_DATE('20160815104530', 'YYYYMMDDHH24MISS') FROM DUAL;
TO_DATE(
----------
2016-08-15
You use to_date():
select to_date(substr(str1, 1, 10), 'YYYY-MM-DD')
select to_date(substr(str2, 1, 8), 'YYYYMMDD')

Null and date format handling in talend

I have an excel with a date field but the first row in the excel is blank and few other rows are having a date format as MM/dd/yyyy HH:mm:ss.
The data to be loaded into a Postgresql table with the field of data type timestamp yyyy-mm-dd HH:mm:ss.
The excel cannot be modified as it is being downloaded from the cloud and the data is loaded straight away into the table.
I tried using tConvert type but it cannot accept null or " " values in timestamp. I am facing a Null tMap error during runtime in talend. Even if I try to convert from string to date format in order to pass null in tmap, it is changing the date format and showing error.How can this be handled ?
The talend structure is : tFileInputExcel - > TMAP(date field : MM/dd/yyyy HH:mm:ss) -> tConvertType(date field : yyyy-mm-dd HH:mm:ss) ->TMAP(yyyy-mm-dd HH:mm:ss) -> Postgresql Table
Here is the Excel screenshot:
At first, I do not quite understand why do you want to use tConvertType component. After defining a proper schema Talend is changing your data into Java Date object and from that moment format is not important and you don't have to convert it when you want to put it into Postgres table. At least it should not cause NullPointerException.
Consider following steps:
Sample input file
I've prepared some file with date value/space/empty string, solution I'm describing works also with nulls.
Configure tFileInputExcel component
You have to allow taking null values in by checking the Nullable check box. You should also check trim option.
Examine output
After connecting input component to tLogRow null/empty/space values are handled properly.
I hope this will be helpful.
You can capture date format or null handling in variable within tMAP component
that is
var :TalendDate.formatDate("yyyy-mm-dd HH:mm:ss",row1.columnname)
so data flow would be
tFileInputExcel --->tMAP --->Postgresql Table

Resources