I have query database like this:
SELECT * FROM TABLE WHERE start_date = '01-10-2016' //DD-MM-YYYY
But above code error and actually in database date format is YYYY-MM-DD.
So how to create query with format date DD-MM-YYYY?
A proper date column (data type date !) has no format. Then it's enough to get your data input for a date column right, use to_date() for non-standard input format like #Shiva posted. Or better yet, always provide date literals in ISO 8601 format 'YYYY-MM-DD' to begin with, which works with any locale setting.
If you are running a broken design with dates stored as text, then combine to_date() and to_char() to transform any valid date format into any other text format:
SELECT * FROM tbl
WHERE start_date = to_char(to_date('01-10-2016', 'DD-MM-YYYY'), 'YYYY-MM-DD');
May be you can use to_date function to convert the above format into the standard format.
For instance,
SELECT to_date('01-10-2016', 'DD-MM-YYYY');
----------
2016-10-01
1 row
https://www.techonthenet.com/postgresql/functions/to_date.php
Related
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.
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.
I have an excel source with a date column in dd/mm/yyyy format. I need to load a table with data from this source in exactly the same format. using just DATE data type gives me output of yyyy-mm-dd. So how do I store it in dd/mm/yyyy format?
Don't worry about how the data is stored. Let the native DATE type do its thing. Use the CONVERT function to format your data for display purposes.
/* 103 = dd/mm/yyyy */
SELECT CONVERT(CHAR(10), YourDateColumn, 103) AS formatted_date
FROM YourTable;
I'm using this query
SELECT convert(nvarchar(MAX), GETDATE(), 22) AS Date
Result: 08/05/16 12:23:08 PM
But I want result like this 8/5/2016 12:23:08 PM
dd/mm/yyyy hh:mm:ss a
As of SQL Server 2012 the FORMAT function is available allowing you to specify the format of data types and is locale-aware so it will consider date formatting in relation to the session's language or optional culture parameter.
You can achieve your custom formatting like so: FORMAT(GETDATE(), 'd/M/yyyy hh:mm:ss tt')
Note your requested format dd/mm/yyyy hh:mm:ss a is incorrect as in the case of single digits you want to remove zero padding i.e. 10/8/2016 not 10/08/2016. That's why in the format string I use only d and M.
Also, pay attention to #GarethD comment about the cost on larger datasets.
You could use the FORMAT function in T-SQL : https://msdn.microsoft.com/en-us/library/hh213505(v=sql.120).aspx
Here is the code :
SELECT FORMAT(GETDATE(), 'd/M/yyyy hh:mm:ss tt')
In T-SQL is there a way to filter by greater than a date given in dd/mm/yyyy format?
so for example:
SELECT BIRTHDAY FROM ATABLE WHERE BIRTHDAY > 12/12/1990
Since many date formats are dependent on language & regional settings, I recommend to always use the ISO-8601 format of YYYYMMDD - and of course, also put your date literal into single quotes:
SELECT Birthday
FROM dbo.ATable
WHERE Birtday > '19901212'
This works on all SQL Servers - regardless of what language, date and regional settings you have