Find Maximum date SQL Server - sql

I am using below query to find max date,getting output as "30-12-2017" , whereas the output should be "15-12-2018" .my sample data is attached, data type is nvarchar and SQL Server 2008.
select MAX(date1) from tblMonth

Your datatype for Date1 seems to be nvarchar. The MAX() on nvarchar's is a string compare. The string 30-12-2017 starts with 30, which has the biggest ASCII code in your example. If you enter 31-11-2017, this would be the result of MAX(date1).
Use a date datatype (i.e. datetime2 or date) instead and it will work as you expect.

Related

String to datetime conversion in sql server

Is it possible to convert 'OCT-20' to '2020/10/01' in sql server. I tried the following cases without any luck.
select convert(date, 'OCT-20',103)
----Conversion failed when converting date and/or time from character string.
alter table MyTable alter column [period] date
----Conversion failed when converting date and/or time from character string.
I am using sql server 2016.
If we assume that the value is always in the format MMM-yy then you could do this:
SELECT CONVERT(date,'01-' + StringDate,106)
FROM dbo.YourTable;
Of course, this has 2 flaws.
The date uses a 2 digit year, so SQL Server could assume the wrong century
It'll only work if the LOGIN is using an English based language, otherwise it'll fail.
The real solution is to fix your design; never store date (and time) values in a varchar, and when ever you do use a varchar for a date (such as a literal in a WHERE) use an unambiguous format such as yyyyMMdd or yyyy-MM-ddThh:mm:ss.nnnnnnn.

How do I convert a string of format dd/mm/yy into datetime in SQL Server 2008?

I have table named PTBV with two columns: Dateptbv varchar(50) and [Column 1] varchar(50).
I'm trying to use the CONVERT() function to get actual datetime values from the string data stored in the Dateptbv column.
Here are examples of the data in that column:
"10/01/13"
"11/01/13"
"14/01/13"
"15/01/13"
"16/01/13"
"17/01/13"
"18/01/13"
"21/01/13"
"22/01/13"
"24/01/13"
"25/01/13"
"28/01/13"
"29/01/13"
"30/01/13"
"01/02/13"
"04/02/13"
Note that the quotation marks in this sample are part of the data and are stored in the column. Otherwise, data is stored in dd/mm/yy format.
Unfortunately, every date style I've tried has resulted in an error message. How can I convert this data into DateTime values?
If I understand your picture correctly, your column Dateptbv is VARCHAR(50) and stores the date as yy/mm/dd wrapped in " characters. First of all: Change this, if ever possible!. Try to store values in appropriately typed columns!
Try
SELECT CONVERT(DATE,SUBSTRING(Dateptbv,2,8)),3) --3 for dd/mm/yy
Read this link to find more details about the abilities of CONVERT.
Try the below script
SELECT CAST(SUBSTRING(Dateptbv,2,8) AS DATE)
FROM PTBV

Convert from varchar into date in SQL Server

This looks easy solution but I can't seem to figure out as to why this is not working for me. I have a column that has data like this:
DateField
----------
12/16/2016
11/06/2016
All I want to do is to convert from varchar into a date column, but I am getting this error:
Conversion failed when converting date and/or time from character string.
Here is my simple query:
select convert (date, DateField) as convertedField
from myTable
Nothing wrong with the two examples you have given. There are some bad dates in your table which cannot be converted to date.
Use TRY_CONVERT function for bad dates it will return NULL
select TRY_Convert(date,DateField)
From myTable
You should always store dates in DATE/DATETIME datatype.
If you want to see the records which cannot be converted to date then
select DateField
From myTable
Where TRY_Convert(date,DateField) IS NULL
If working with a specific date format like mm/dd/yyyy You can specify it in Convert() function like the following
CONVERT(DATETIME,DATAFIELD,101)
If it still is not working, use TRY_CONVERT() to get which rows are throwing this exception:
SELECT *
FROM TBL
WHERE TRY_CONVERT(DATETIME, DATAFIELD, 101) IS NULL
This will return rows that cannot be converted
TRY_CONVERT() will return NULL if conversion failed
Read more about DateTime formats here:
SQL Server CONVERT() Function tutorial
Read TRY_CONVERT MSDN Article
You need to specify the format of date time while formatting. The date in your table is currently in U.S format so you should pass the third argument 101 in your convert function.
SELECT CONVERT(date,[DateField],101) FROM myTable;
Working Fiddle here http://rextester.com/NYKR49788
More info about date time style here: https://msdn.microsoft.com/en-us/library/ms187928.aspx

T-SQL Dates using Convert() function?

I am bit confusing here?
declare #date1 datetime = '2016-01-21 14:10:47.183'
I want to convert '2016-01-21 14:10:47.183' To '21-01-2016'
when I tried: select convert(date,#date1,105)
I am getting: 2016-01-21
But with: select convert(varchar(10),#date1,105)
I am getting: 21-01-2016
Why I am not having same results with above code?
Why should I convert to varchar?
Thanks in advance
This is just presentation matter and should be done in application layer. If you cannot do it in application you could use FORMAT (SQL Server 2012+):
declare #date1 datetime = '2016-01-21 14:10:47.183'
SELECT FORMAT(#date1, 'dd-mm-yyyy');
LiveDemo
Why I am not having same results with above code?
select convert(date,#date1,105)
-- DATETIME -> DATE
-- vs
select convert(varchar(10),#date1,105)
-- DATETIME -> VARCHAR(10) using specific style
If you only to skip time part use SELECT CAST(#date1 AS DATE) and do not bother how it is presented. It is still DATE.
To sum up: in SQL query use DATE as date, in application display it with desired format.
The reason why is because once you put a value in a datetime column (or date or any of the other variations on date-time datatypes) in SQL Server. SQL Server ceases to think of that date as having any particular format. It translates it into numbers, and stores it that way internally.
So when you select a date from a date time column, SQL Server displays it in the default format that you have selected based on your environment/local settings.
If you want to display it in any other format, you have to first convert it to a string, because as far as SQL Server is concerned, dates don't have formats. They are just numbers. The 21st day of March is the 21st day of March, whether you write it as 3/21 or 21/3.
So when you try to convert a date to a date with a different format, SQL Server just ignores you because dates don't have formats. However, if you want to convert that date to a string, SQL Server will be happy to help you display that string in any format you like.
Hope this helps, but sounds like some further research into how SQL Server stores dates would help your understanding.

Change datetime custom format in sql server

I have created a column of datetime type in SQL Server.
The output of this column is:
1994-01-10
But I want to show only year, like this: 1994 (not long datetime)
How can I change this format type of datetime?
My table is tbl_borrowing, my column is VitiBotimit
Thank you
How about this:
SELECT YEAR(VitiBotimit)
FROM dbo.borrowing
DATETIME in SQL Server has no format - it's an 8-byte binary value - and therefore you cannot change the format.
You only get a format (of your choice) when you select the value from the table. If you only need the year - select it that way.