TO_DATE() is returning incorrect format than the format specified in Snowflake - snowflake-cloud-data-platform

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

Related

T SQL Conversion failed when converting date and/or time from character string from VARCHAR(MAX)

I'm using SQL Server 2014. I have a date stored as varchar(MAX) in the format of:
2019-02-18
However, I want it in the British format dd/mm/yyyy (103).
This is my SQL:
SELECT CONVERT(DATE, DateField, 103) AS "JobStartDate"
FROM tblTest
However, I keep getting this error:
Conversion failed when converting date and/or time from character string.
What am I missing?
Update: The date is initially stored as varchar max as it is coming from a 3rd party system. I have no control over this and I completly understand this is the wrong format, but this is what I have been given.
I have a date stored as varchar(MAX)
There's your problem right there.
Not only you are using the wrong data type to store dates, you are also using max which is a known performance killer.
The solution to the problem is to alter the table and store dates in a Date data type - but first, you must look up all the objects that depends on that column and make sure they will not break or change them as well.
Assuming this can't be done, or as a temporary workaround, you must first convert the data you have to Date, and then convert it back to a string representation of that date using the 103 style to get dd/mm/yyyy.
Since yyyy-mm-dd string format is not culture dependent with the date data type, you can simply do this:
SELECT CONVERT(char(10), TRY_CAST(DateField As Date), 103) As [JobStartDate]
FROM tblTest
Note I've used try_cast and not cast since the database can't stop you from storing values that can't be converted to dates in that column.
You want to format the DateField column and not convert it to date.
So first convert it to DATE and then apply the format:
SELECT FORMAT(CONVERT(DATE, DateField, 21), 'dd/MM/yyyy') AS JobStartDate
See the demo.

Ms sql output in date format

I have a requirement. I have datetime field and I want data in datatype=date
Existing date: 2019-11-13 00: 00: 00: 000 ; datatype=datetime
Expected output require: 11/13/2019 (mm/dd/yyyy) ;
datatype= date
Please help me.
If the core requirement is a right type then:
SET DATEFORMAT MDY;
SELECT CAST(GETDATE() as DATE);
Explicit DATEFORMAT added becaise the output depends on a language settings, so can be yyyy/mm/dd or mm/dd/yyyy, some apps can be sensitive to this, as example SSRS.
However, if there is still a requirement to get value in a precisely right format on a database side, then consider to use a FORMAT statement:
SELECT FORMAT(GETDATE(), 'MM/dd/yyyy')
I have datetime field and I want data in datatype=date
If you want the data type as DATE, in that case you can try like following.
SELECT CAST(YourDateTimeColumn AS DATE) from [YourTable]
Formatting part you should be doing in UI.
You can use CONVERT() :
SELECT t.datecol, CONVERT(VARCHAR(10), t.datecol, 101)
FROM table t;
Your desired date format requires varchar type, if you want date only then you can do instead :
SELECT t.datecol, CONVERT(DATE, t.datecol)
FROM table t;
If you don't want to convert the type, then these type of conversation should do in presentation layer instead.

Display a Date that is stored in SQL Server as Char yyyy-mm-dd as mm-dd-yyyy

I have a char column that stores dates as yyyy-mm-dd. I need to display these dates as mm-dd-yyyy.
Thanks in advance.
The root of your problem is because you are using the wrong datatype. Dates should stored as dates. Anything else is nothing but a pain to work with. To get your formatting you will first have to convert your string to a date so
you can format it.
Here is an example.
select convert(date, YourDateColumn, 101)
You can read more about convert here. https://msdn.microsoft.com/en-us/library/ms187928.aspx
If 2012+ you could try
Select Format(GetDate(),'MM-dd-yyyy')
Returns
08-03-2016
To convert the string
Select Format(cast('2016-08-03' as date),'MM-dd-yyyy')
DECLARE #Date varchar(12)='2012-01-29'
SELECT FORMAT(CONVERT(datetime,LEFT(#Date,4) + SUBSTRING(#Date,6,2) + SUBSTRING(#Date,9,2)), 'MM-dd-yyyy', 'en-US' ) AS Result
This is a little bit heavy on processing, but it works.
This answer also assumes that ALL values in the date text column are formatted as YYYY-MM-DD. If you have any values that aren't formatted that way as strings, then you will run into problems.
Obviously you will need to replace the #Date variable with the name of your date text column. I wanted to provide an example you can run easily to check the answer.

display current date -1 in sybase

I am trying to display the previous day's date Sybase using a select query:
select dateadd(day,-1,convert(char(10), getdate(), 23))
this query displays as 2015-06-18 00:00:00.0
I expect the output to be 2015-06-18.
How can I get that?
Try select dateadd(day,-1,convert(Date, getdate(), 365))
Try select convert(char(10),dateadd(day,-1, getdate() ), 23 )
Dateadd expects a date parameter as the third argument. In your example you're feeding it a char(10) . Even though implicit conversion from Char->DateTime is supported in Sybase, I would not code to depend on it in this case.
Well, datetime is a binary type. How it is formatted for display is up to you.
getdate() returns a datetime representing the current date/time. And dateadd() returns a datetime or date value, depending on what it started with (in your case, that would be datetime). And when you run your select statement, it's getting converted to a string using the default format configured for your Sybase instance. Hence your results.
In a nutshell, you are:
Converting the datetime value to char(10) to get an ISO 8601 format date string (yyyy-mm-dd).
Converting that back to a datetime value (so the time component is start-of-day)
Subtracting one day.
The easiest way to get what you want (yesterday's date) is this:
dateadd(day,-1, convert(date,getdate()) )
Which, when formatted for display, will come out as something like (depending on the default format configured for your Sybase instance) yyyy-mm-dd.
Or it might come out like November 29, 2015. If you want to ensure that it is an ISO 8601 date representation, you'll need to be explicit about it and cast it a char or varchar, thus:
convert(char(10) , dateadd(day,-1, convert(date,getdate()) ) , 23 )
which leaves you with a char(10) value containing yesterday's date.
If your version of Sybase doesn't support date, you'll have to fall back to what you were doing, but something like this:
convert(char(10) , dateadd(day,-1, getdate() ) , 23 )
You are telling it to give you hh:mm:ss, so that's what you are getting.
The 23 inside the convert is the format code for yyyy-mm-ddTHH:mm:ss There is no code to get yyyy-mm-dd, the closest you can get is 105 (dd-mm-yy) or 110 (mm-yy-dd).
If you need yyyy-mm-dd, then you'll have to convert the date to a string(char or varchar), and truncate the parts you don't want.
Converting Datetime

How to set date format for the result of a SELECT statement in SQL Server

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.

Resources