I want a result 2000-02-05 with below query in snowsql.
alter session set TWO_DIGIT_CENTURY_START=2000;
select cast ('05-FEB-00' as date) from dual;
But I am getting 0001-02-05.
I am using existing script to load date in snowflake which works for oracle. I know I can get expected result using to_date function but I don't want to do so. If I have to then I have change many place in script which is hectic.
I want solution using cast function. Do anyone know what is happening here?
You first need to specify the non-default date format for your input data. In the case of the example above:
alter session set date_input_format = 'DD-MON-YY';
Then
alter session set TWO_DIGIT_CENTURY_START=2000;
select cast ('05-FEB-00' as date) from dual;
yields:
2000-02-05
Related
I am trying to convert the date I have got using the GETDATE() function into YY-MON-DD format such as 04 Nov 2022. I have used TO_VARCHAR() to convert into the date returned by the GETDATE() into a string. The output is correct till the TO_VARCHAR() is used i.e. SELECT TO_VARCHAR(GET_DATE, 'DD-MON-YY') returns the desired format.
When I try to wrap it around TO_DATE() function; the date format changes into 2022-11-04.
SELECT TO_DATE(TO_CHAR(GETDATE(), 'DD-MON-YY'), 'DD-MON-YY')
How can I resolve the problem and correct the format!?
A date will display in the default format for your session. If you want to display it in a different format you would need to use the TO_CHAR(date_col, fmt) construct. So your select statement just needs to be:
SELECT TO_CHAR(GETDATE(), 'DD-MON-YY')
wrapping a TO_DATE round it doesn't achieve anything if all you are trying to do is display that column as an output of your SELECT statement
You need set the desired format at session level
alter session
set date_output_format='DD-MON-YYYY';
select getdate()::date --getdate() returns a timestamp not a date
select current_date() --alternatively
As Nick suggested, use the select to format dates instead of tinkering with session parameters
I have wrote a code in SF as below
select to_date(date_part(day,current_date)||'-'||monthname(current_date)||'-'||right(date_part(year,current_date),2),'DD-MON-YY')
the output need to be 16-Feb-22. when i run this one, it gives you 16-02-2022. How to convert this to 16-Feb-22? But this need to be in date format. Not the string format
I was doin this as an exception of TO_DATE(current_date,'DD-MON-RR')
can anyone help?
alter session set DATE_OUTPUT_FORMAT = 'DD-MON-YY';
select current_date();
alter session unset DATE_OUTPUT_FORMAT;
You can use the TO_VARCHAR function with a date format:
select to_varchar(current_date, 'DD-MON-YY');
If you convert it back to a date, it will display using the default for the session, so you'll need to convert it this way or alter the session default for display.
I have a Timestamp_NTZ column where I know the data are in Central (America/Chicago) timezone. How do I convert it into Timestamp_TZ format during select.
I couldn't just convert to varchar and add timezone offset and back to timestamp_tz because of different offset during daylight saving.
I found following two approaches but they involves more code/typing. Looking for elegant solution.
Alter session to the required timezone and use TO_TIMESTAMP_TZ() function. The drawback of this approach is that its two separate query and it cannot be used in stored procedure with run as Owner.
alter session set timezone = 'America/Chicago';
select TO_TIMESTAMP_TZ(column_name) from table_name;
Use TIMESTAMP_TZ_FROM_PARTS function. This involves more typing and looks complicated.
select TIMESTAMP_TZ_FROM_PARTS(Year(column_name), Month(column_name), Day(column_name), Hour(column_name), Minute(column_name), Second(column_name), 0, 'America/Chicago') from table_name;
Is there a simple way to do this in snowflake, something like:
select TO_TIMESTAMP_TZ(column_name, 'America/Chicago') from table_name;
You should be able to use the CONVERT_TIMEZONE for this. The 2 argument version Doesn't expect TIMESTAMP_TZ as you mentioned in your comment. You can see below that I'm passing in a TIMESTAMP_NTZ for demonstration purposes but you could pass in a string like 2020-01-27T08:00:00Z too if you want:
select CONVERT_TIMEZONE( 'America/Chicago' , TO_TIMESTAMP_NTZ('2020-01-27T08:00:00Z'));
The above returns
2021-01-26 02:00:00.000000000 -06:00
I currently have a set of drop down parameters that pass dates to a query. There is a display value available to select for each Work Week.
Create Date Parameter:
Display in the drop down: 2016 01 (1/3/2016) P01-16 Q1-16
Value being passed to the SQL is WEEK_BEGIN_DATE: 1/3/2016
New Ship Date Parameter:
Display in the drop down: 2016 01 (12/27/2016) P12-15 Q4-15
Value being passed to the SQL is SHIP_WEEK_BEGIN_DATE: 12/27/2016
I would like to be able to make both of these optional.
I tried the following in the parameter value SQL to get the parameter to allow nulls but I got a data type error. I did start going down the cast as varchar() route but it was getting messy quick.
SELECT 'NULL' AS WEEK_BEGIN_DATE
UNION
SELECT d.WEEK_BEGIN_DATE FROM DATE d
Any suggestions are much appreciated. Let me know if I can provide any other helpful info.
this should work, you don't need the single quotes
SELECT NULL AS WEEK_BEGIN_DATE
UNION
SELECT d.WEEK_BEGIN_DATE FROM DATE d
to make parameters optional you could in where clause write something like
where (WEEK_BEGIN_DATE = #date or #date is null)
If your parameter of dates was a VARCHAR() you would need to do what Kostya stated. But since it's not, you can click "Allow null value" under Report Parameter Properties (rightclick on your parameter and select properties)
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.