sql CONVERT to datetime does not change printed date format - sql-server

I have a column in a table updatedDate - which is a datetime data type.
Data sample:
2017-10-15 18:08:22.000
2017-10-15 18:07:44.000
2017-10-15 18:07:17.000
2017-10-15 18:07:10.000
2017-10-14 18:00:54.000
2017-10-13 17:59:23.000
2017-10-13 17:59:13.000
I would like to display a list of DISTINCT dates, in the format of dd/mm/yyyy, but for the life of me... I can't get it. I would think it should be:
SELECT DISTINCT convert(datetime,updatedDate,103)
FROM [tblStudentCourses]
ORDER BY updatedDate DESC
But it does not actually convert to the 103 format... it just gives it to me as the full date and time format as originally, without any CONVERT.
What I want to get would be:
15/10/2017
14/10/2017
13/10/2017
What am i doing wrong?
Thanks!

To display the date in DD/MM/YYYY format, you can use CONVERT(VARCHAR(10), DateColumn, 103).
To maintain the proper ordering, you can wrap it all in a subquery. For example:
SELECT DisplayDate
FROM (
SELECT DISTINCT
DisplayDate = CONVERT(VARCHAR(10), CAST(UpdatedDate AS DATE), 103),
ActualDate = CAST(UpdatedDate AS DATE)
FROM [tblStudentCourses]
) AS T
ORDER BY ActualDate;
Note: Cast the date column to date if it's datetime like your sample data.

SELECT DISTINCT convert(VARCHAR,updatedDate,103)
FROM [tblStudentCourses]
ORDER BY updatedDate DESC
You should use convert to varchar for formating to various date formats

Related

how to have date time column converted into date and displaying as 103 style

Hi i've a column the_date which is having sample data like
1900-01-01 00:00:00.000
1990-01-01 00:00:00.000
1990-01-02 00:00:00.000
1990-01-03 00:00:00.000
1990-01-04 00:00:00.000
1990-01-05 00:00:00.000
1990-01-06 00:00:00.000
1990-01-07 00:00:00.000
now i just want to select only date only and displaying it into 103 style and convert the column into Date format. i've tried this syntaxconvert(varchar,THE_DATE , 103) but then the column is not in Date format.
any help please.
This Works try it once
SELECT CONVERT(varchar,CAST(DATE_TIME AS DATE),103)AS Date_Time From <yourTable>
It's a little confusing because regardless of the datatype, you will always get the same answer with CONVERT. I'll illustrate this with 2 declared variables, 1 datetime datatype, the other date datatype:
DECLARE #mydatetime datetime = '1990-01-01 00:00:00.000'
DECLARE #mydate date = '1900-01-01'
SELECT convert(varchar,#mydatetime, 103) as mydatetime
SELECT convert(varchar,#mydate, 103) as mydate
Produces:
mydatetime
01/01/1990
mydate
01/01/1900
So you don't need to cast to date, then to 103 format.
If you don't like the time with date in your table (datetime) then you can always ALTER the column in the table to the date datatype. This can be done using SSMS or you can do the SQL:
ALTER TABLE mytable ALTER COLUMN mycolumn DATE
Where DATE is the new datatype. And then only the date is stored in the table.
Converting a string (or an equivalent database type) to another type is called "parsing". Converting another type to string is called "formatting".
The DATE or DATETIME type (or equivalent type in the front-end) does not store dates as string and has therefore no format. A number is used to represent the date internally which counts the days since a reference date (1753-01-01 for SQL-Server). The time is represented as decimal fraction.
Of course you always see the date as formatted when you open the table, because it is converted to a text for display, but it is not stored as formatted.
So, what you have to do if your date is given as text, is to parse it using the 121 format (YYYY-MM-DD HH:MI:SS.MMM (24h)) to get a DATE (or DATETIME).
CONVERT(DATE, the_date, 121) or CONVERT(DATETIME, the_date, 121)
If the_date is already of DATE (or DATETIME) type and you want to display it in the 103 format (DD/MM/YYYY)
CONVERT(VARCHAR(10), the_date, 103)
If the date is given as DATETIME type and you just want to strip off the time part and return the result as DATETIME type again, you can do
DATEADD(dd, DATEDIFF(dd, 0, the_date ), 0)
Note that no date format is involved here, since the date does never appear as text.
thanks for all your answer but for anyone who has my same query i got the solution from here
solution

how to compare date using where statement?

i have a table called dbo.reminder in that i'm having a column name rdate i have declared that as
NVARCHAR(50)
i want to sort the table using where condition with the present date how can i do that?
i have tried this query
SELECT rdetail,
rid,
rdate = CONVERT(VARCHAR, CONVERT(DATETIME, rdate, 103), 103)
FROM dbo.reminder
where rdate='23/01/2017'
but its not sorting please help me regarding this ?
Do not store date as NVARCHAR. Change your rate datatype to date and try this way
SELECT rdetail,rid,rdate
FROM dbo.reminder
where rdate = convert(date,'23/01/2017' ,103)
Convert the varchar date input to date and check with your rate column.
Better to pass imput in YYYYMMDD or YYYY-MM-DD format which is universal and does not require any conversion
If you cannot change the datatype then
SELECT rdetail,rid,rdate
FROM dbo.reminder
where convert(date,rdate,103) = convert(date,'23/01/2017' ,103)

How to search by Date in this DD/MM/YYYY format from TimeStamp column in SQL Server?

I have a table called Transaction. One of the column name is Time,datatype is TimeStamp. So the data is looking like this 2015-01-14 23:22:11.000.
Now I want to search by Date in where clause in this format DD/MM/YYYY
for example
select *
FROM Transaction
WHERE Date='25/12/2016' // DD/MM/YYYY
Thanks
Your question is not completely clear, but it appears that you have the need to compare date data in the format DD/MM/YYYY against a timestamp column in your table. One option is to use the CONVERT function to convert both the input and your timestamp column to a common DATETIME format, and then do the comparison.
SELECT *
FROM Transaction
WHERE CONVERT(DATETIME, DATEDIFF(DAY, 0, TIME)) = CONVERT(DATETIME, '25/12/2016', 103)
This will return all records whose date components are '2016-12-25'. Note that both sides of the comparison would have a time component set to midnight of that day.
select *
FROM Transaction
WHERE Date=CONVERT(NVARCHAR(50), '25/12/2016', 103) DD/MM/YYYY
Use canonical format (YYYY-MM-DD) on date columns:
SELECT *
FROM Transaction
WHERE Date=convert(datetime, '2016-12-25')
You can also use time ranges with canonical format YYYY-MM-DD HH:MI:SS:
SELECT *
FROM Transaction
WHERE Date BETWEEN convert(datetime, '2016-12-25 00:00:00')
AND convert(datetime, '2016-12-25 23:59:59')
You have to pass date in dd/mm/yyyy format, take parameter datatype as varchar and convert date like in the following SQL statement
SELECT CONVERT(varchar(25),CreateDate,103) as CreateDate, [UserID],[FirstName]+' '+[LastName] AS 'Name',[NomineeName],[City],[UserDocument]
FROM [dbo].[UserMaster]
WHERE CONVERT(varchar(25),[CreateDate],103) between convert(varchar(25),#datefrom,103) and convert(varchar(25),#dateto,103)

Convert and sort varchar Date dd-MMM-yyyy

I'm using a table that has the date as varchar. example dateformat:
17-Jun-2015
I have tried the following ways to convert and sort the date(dd-MMM-yyyy) to dateTime.
SELECT date, name, author
FROM sometable
ORDER BY CONVERT(DATETIME, date, 106) DESC
I have also tried converting the date in the select statement. doesn't work. The error is
Conversion failed when converting date and/or time from character string.
The conversion types through some similar questions but I have not found any solutions to the format I have. Is there some way of selecting the delimiter between the day month and year??
I also had a browse through this link which has the formats for datetime formats. 106 was the closest to my varchar date. Only my date in the table has '-' between day month and year.
https://msdn.microsoft.com/en-us/library/ms187928.aspx
Appreciate any help.
It should be absolutely fine to use 106 to convert your date format.
But I guess your table contains some of the invalid values in the column causes the error, try to spot them out by TRY_CONVERT:
SELECT date, name, author, TRY_CONVERT(datetime, date, 106) AS convertresult
FROM sometable
WHERE TRY_CONVERT(datetime, date, 106) IS NULL AND date IS NOT NULL
I would use date style 113 to convert to datetime format like this:
DECLARE #date VARCHAR(20) = '17-Jun-2015';
SELECT CONVERT(VARCHAR(20),CAST(#date AS DATETIME),113)

Convert varchar dd-mmm-yyyy to dd/mm/yyyy then order by not working?

I have a situation
I need to convert a varchar value from dd-mmm-yyyy to dd/mm/yyyy
order by above converted date
Using
CONVERT(VARCHAR(10), CAST([Date] AS DATETIME), 103) as ModifiedDate
I got the required format but, when I do order by ModifiedDate
order by result set showing 2013, 2014, 2015 years mixed match. I think its doing order by date. Where I am doing wrong? Can anybody help? Thanks
Since ModifiedDate is a varchar, its ordering is lexical (01/01/1900 < 01/01/1901 < 02/01/1900).
The solution is not to use a varchar for sorting. Convert your varchar-date to a real date (i.e., only CAST([Date] AS DATETIME) without the outer CONVERT) and sort by this expression.
select CONVERT(VARCHAR(10),cast ([Date] AS DATETIME),103) as ModifiedDate
from <table name> order by CONVERT(VARCHAR(10),cast ([Date] AS DATETIME),103)

Resources