Convert Date Against Where Clause - sql

I have Following dummy table with data:
ACID srno date(mm/dd/yyyy) name
3 1 04/12/2010 mahesh
3 2 04/12/2010 mahendra
Now if I try with Following SQL Transact:
select srno from dummy
where name = 'mahesh'
and date= convert(datetime,'12/04/2010',101) –- I have date in dd/MM/yyyy Format
and ACID=3
It’s Not returning the srno of the table. That means Date is not execute convert statement as above
What’s the reason?

Try using style 103 instead of 101.
select srno from dummy
where name = 'mahesh'
and date= convert(datetime,'12/04/2010',103) –- I have date in dd/MM/yyyy Format
and ACID=3

If you convert 12/04/2010 using format 101, you get date "December 4, 2010", which is not in your database. Use format 103 to convert a date in format dd/mm/yyyy to DateTime.
The database stores dates using the DateTime type which is format-agnostic. It does have a default format for string conversions, which seems to be mm/dd/yyyy (101) on your database.
However, when you convert a string to add it to your table, you want to specify the format of your input string, in your example dd/mm/yyyy (103).
Take a look at the MSDN article for CAST and CONVERT which details all format styles that you can use with dates.

To be honest, if you want to specify a DATE LITERAL in SQL Server, please stick with the simplest YYYYMMDD format, e.g.
and dummy.date = '20100412'
It is robust and works for all regional, user language and dateformat settings. This assumes the other side of the comparison is already a date column. Even if you had to CAST it, using this format you don't need to specify a format
and dummy.date = cast('20100412' as datetime)

Related

Date format value not updating from previous format to new format after update

When I update a column from a table with a date format of MMM DD,YYYY to a new format. The format doesn't change to the desired format which is YYYY-MM-DD HH:MM:SS. Even when update different value like GETDATE(). The date will change but the format will remain the same.
Current value & format from column the type is varchar
DueDate
Jun 27 2020 12:00AM
Desired format
DueDate
2020-06-27 00:00:00.000
Update statement
update TableName
set
DueDate = CAST([DueDate] AS smalldatetime),
LastSyncDateTime = GETDATE()
where CaseGuid = 'DA2CE6A1-0394-463E-8E8D-962F3A24ADC8'
There is a huge confusion between "Date displaying format" and "Date storing format". The VERY short explanation is that what you mentioned is only a client side displaying format, while SQL Server have specific format which is used for storing dates (remember that the server stores zero and one only).
You can insert dates to a table using different styles (the official name for the displaying format is STYLE), and you can present the dates in the client side using different style, but it will always be stored the same from the "SQL Server point of view" according to the DATE type which is used.
In order to solve your original needs, all that you needed to do is to provide the server the information about the style which you use in the client side (in the query). This is done by using explicit CONVERT with the third parameter, which is the STYLE.
For example if you use in the client side an Israeli format like dd/MM/yyyy, then you should use CONVERT(DATE, '27/02/2021', 103).
For more information on different STYLEs you can check this documentation.
Note: If you want to display the dates in specific format which is not covered by the existing STYLEs then you can use the function FORMAT() in your query. This function is fully flexible to return the data in your specific format. Remember that this function returns the data as string and it will not be date anymore.
For example, let's say that I want to use the format: "Day:dd,Month:MM,Year:yyyy". So if the date is '27/02/2021' then I expect to get "Day:27,Month:02,Year:2021". In this case use below:
DECLARE #D DATE
SET #D = CONVERT(DATE, '27/02/2021', 103) -- convert string to date for storing
select FORMAT(#D, 'Day:dd, Month:MM, Year:yyyy') -- convert date to string for displaying
Solution use the format function
update TableName
set
DueDate = FORMAT (CAST([DueDate] AS smalldatetime),'yyyy-MM-dd HH:mm:ss'),
LastSyncDateTime = GETDATE()
where CaseGuid = 'DA2CE6A1-0394-463E-8E8D-962F3A24ADC8'
https://www.sqlshack.com/a-comprehensive-guide-to-the-sql-format-function/

SQL Table datetime column format

I tried googling to get an answer but in vain. Below is my requirement
User has an option to insert data into a table which has export_date as datetime
When they execute insert statements, I want to ensure that they have keyed in date in "dd-MM-yyyy hh:mm:ss" format. If not, don't allow insert queries to run.
Or allow the user to enter date in any format like dd-MM-yyyy or dd/MM/yyyy but internally convert it into the required format "dd-MM-yyyy hh:mm:ss" and store
Can someone help/guide me?
You can use Set DateFormat
Example
Used data type of date for illustration, but clearly you can use datetime
Set DateFormat DMY
Select try_convert(date,'15/08/2017') -- Returns 2017-08-15
Set DateFormat MDY
Select try_convert(date,'15/08/2017') -- Returns NULL
Set DateFormat YMD
Select try_convert(date,'15/08/2017') -- Returns NULL
You will likely run into issues if you want the user to input the date in the "dd-MM-YYYY" format since if the user inputs in the mm-dd-yyyy format, you'll get different results. "YYYMMDD" is a generic format that SQL Server will always interpret properly.
Once you get the date from the user, you can convert it using the particular format that you want. The following will convert the date to the ISO8601 format:
SELECT
GETDATE() AS UnconvertedDateTime,
CONVERT(nvarchar(30), GETDATE(), 126) AS UsingConvertTo_ISO8601 ;
GO
For more information on the specific date formats, I'd recommend checking out Microsoft's Convert Functions.

Convert text to system date format [duplicate]

This question already has an answer here:
SQL data error: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
(1 answer)
Closed 6 years ago.
I want to convert a text date to system date format. How can I accomplish that?
I have the format where I want to convert it 'MM/DD/YYYY' for example, but it can change, it can be dd/mm/yyyy or yyyy/mm/dd.
EDIT
From SQL Server 2012 and later you might use the FORMAT function if you want to force a date into a formatted text: https://msdn.microsoft.com/en-us/library/hh213505.aspx
With earlier versions you might use something like this:
DECLARE #d DATETIME=GETDATE();
DECLARE #TargetFormat VARCHAR(100)='DD/MM/YYYY';
SELECT CONVERT(VARCHAR(100),#d, CASE #TargetFormat
WHEN 'MM/DD/YYYY' THEN 101
WHEN 'DD/MM/YYYY' THEN 103
--add all formats convert can undertsand
ELSE 101 END )
Previous
Look here: https://msdn.microsoft.com/en-us/library/ms187928.aspx
The third parameter in CONVERT specifies the format.
The MM/DD/YYYY was 101, dd/mm/yyyy was 103
But: You should avoid date literals as they are depending on your system's culture.
From your question I get that you are the owner of the source, so you can change this to the way you need it. I strongly advise you to use independent formats.
Look here: https://stackoverflow.com/a/34275965/5089204
Here is an example
SELECT CONVERT(DATETIME,'04/13/2016',101)
The result is of datetime datatype

Converting Date format in Sql Server

I have a column in my table for storing dates and it is in 12-06-2013 15:32:45. I want to convert it to MM/DD/YYYY format. How can I do it?
Coulmn type is varchar
First you need to CONVERT VARCHAR() to datetime type and and then to CONVERT it to string in desired format:
SELECT CONVERT (varchar (10), CONVERT (date,'12-06-2013 15:32:45' ,103),101)
First 103 is used to interpret current date format, and second - 101 - target format.
If you change target date format from varchar to date then your output in MSMS will be in default display date format of you SQL Server, not the desired format. This is because SQL Server stores dates as integers and converts them before dispalying the value. Therefore if you need to store in certain format, then store in VARCHAR type.
Check out CAST and CONVERT functions on MDSN
or you can do it in your c# code like that :
First you have to take your date from your table and put it in string var "DT" exemple :
string strDT =db.TableTitle.date;
string date = strDT .ToString("MM/DD/YYYY");
DateTime DT = Convert.ToDateTime(date);
then you can use the DT time variable :)

Convert Data from yyyy-dd-mm to mm/dd/yyyy Issue

I have a column in my table with Dates in the format yyyy-mm-dd I want to convert all the dates in that column to the format mm/dd/yyyy
I am using the below query
UPDATE Test.dbo.Status
SET DateIn = CONVERT(DATE,DateIn ,101)
The DateIn column is defined as Date in my table (DateIn DATE NULL)
The query does no change to the data. am I doing some thing wrong here?
You can change the default format in which SQL Server displays a date, but you can't alter the way a DATE value is stored via CONVERT(). You can format a date however you want if you store it as a string, but you lose functionality when you do that and it's not advisable. If you are hell-bent on storing a formatted version, you might want to create a new VARCHAR() field so you can preserve your DATE version.
You're better off formatting the date at the application level.
The reason your query does nothing is that the actual DATE values are equivalent. Notice when you take any valid date format and CAST() it as DATE the resulting format is the same regardless of the input:
SELECT CAST('20040510' AS DATE)
SELECT CAST('2004-05-10' AS DATE)
SELECT CAST('May 10, 2004' AS DATE)
All return: 2004-05-10 on my instance of SQL Server.