I have a datetime column, dates are shown like "11/13/2012 1:48:27 PM" I need to convert it to "11/13/12".
I know I can convert it using
convert(varchar(10), datevalue, 1)
but when I sort the column after converting it's not sorting by date. I'm not sure if convert(datetime, datevalue, 1) is supposed to do anything but nothing happenend and I still got the default date value.
I tried this
select convert(varchar(10), datevalue, 1)
from table
order by convert(datetime, datevalue, 1) desc
It works but my bigger query selects distinct and I'm getting an error
ORDER BY items must appear in the select list if SELECT DISTINCT is specified
Try something like:
SELECT CONVERT(varchar(10), datevalue, 1)
FROM table
ORDER BY datevalue desc
Try this:
SELECT CONVERT(DATETIME, datevalue, 103)
FROM table
ORDER BY CONVERT(DATETIME, datevalue, 103)
select convert(varchar(10), datevalue, 1)
from table
order by datevalue desc
You don't need to convert date in order by statement. Only column name would work if its datatype is 'datetime'
Related
I need to select the distinct date time from 2 columns.
First convert the date time into new format 'yyyy-mm-dd hh:mm:ss' and select only the distinct value. Such as: convert and take the only 1 time value 12:59:43 among 3 results.
But when i try to add format or convert function into this, it was failed or not showing right results. Any help please? Thank you.
The original code i use below:
SELECT DISTINCT *
FROM
( SELECT TOP 100000000
[Start] AS Station_Start_Date,
[End] AS Station_End_Date
FROM ***
WHERE *** = 1476541 AND [End] IS NOT NULL AND [Start] IS NOT NULL
GROUP BY
[End],
[Start]
ORDER BY [Start] DESC)
try the following:
SELECT DISTINCT convert(varchar(19), t.Station_Start_Date, 120) Station_Start_Date
, convert(varchar(19), t.Station_End_Date, 120) Station_End_Date
FROM #t t
or
SELECT convert(varchar(19), t.Station_Start_Date, 120) Station_Start_Date
, convert(varchar(19), t.Station_End_Date, 120) Station_End_Date
FROM #t t
GROUP BY convert(varchar(19), t.Station_Start_Date, 120), convert(varchar(19), t.Station_End_Date, 120)
Please find the db<>fiddle here.
I have saved my date as a nvarchar(50) datatype in SQL Server. When I run this query:
select [Client_code], Date_of_receipt
from [T_Receving]
I am getting output like this:
but I want to filter my records by particular date, so I wrote a query like this
select
convert(date, [Date_of_receipt], 103) as 'Date_of_Receipt'
from
[T_Receving]
where
convert(date, [Date_of_receipt], 103) between '2015-03-06' and '2018-05-06'
but its showing an error
Conversion failed when converting date and/or time from character string
You have to convert to datetime and then convert back to varchar
declare #dtv varchar(20) = '2018-17-04'
declare #dtvdt datetime = convert(datetime, #dtv, 103)
select #dtvdt;
select convert(varchar(20), convert(datetime, #dtv, 103), 103), #dtv
where convert(datetime, #dtv, 103) between '2015-03-06' and '2018-05-06'
You clearly have some bad data. Use try_convert():
select try_convert(DATE, Date_of_receipt, 103) as Date_of_Receipt
from T_Receving
where try_convert(DATE, Date_of_receipt, 103) between '2015-03-06' and '2018-05-06' ;
In SQL Server 2008, you have to work harder to find the culprits. You can start with:
select date_of_receipt
from T_Receving
where date_of_receipt not like '[0-3][0-9]/[0-1][0-9]/[0-9][0-9][0-9][0-9]'
This will find most instances of bad formats. If it still persists, you will have to dig deeper to find bad day or month numbers.
I'm collecting data between two date 01/12/2014 and 31/12/2014 but my sql data type in nvarchar
is my query right?
SELECT * from customer where date >= convert(datetime, '01/12/2014', 105)
AND date <= convert(datetime, '31/12/2014', 105)
Result
Msg 242, Level 16, State 3, Line 1
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
can any one solve this problem...
as I know you must separate different parts of a DATE with "-" not with "/" in format 105. here is an example:
SELECT convert(datetime, '23-10-2016', 105) -- dd-mm-yyyy
so you must rewrite your code as:
SELECT * from customer where date >= convert(datetime, '01-12-2014', 105)
AND date <= convert(datetime, '31-12-2014', 105)
The format your string are in, 'dd/mm/yyyy' is 103, not 105 (which is 'dd-mm-yyyy'). So, simply use the correct format:
SELECT *
FROM customer
WHERE [date] >= CONVERT(datetime, '01/12/2014', 103)
AND [date] <= CONVERT(datetime, '31/12/2014', 103)
If your date type is nvarchar why don't you try like this:
SELECT * FROM customer
WHERE date >= '01/12/2014'
AND date <= '31/12/2014'
Do we really need to convert?
SELECT * FROM DBO.CUSTOMER
WHERE CAST([date] AS DATE) >= '01/12/2014' AND
CAST([date] AS DATE) <= '31/12/2014'
I suggest you to use this:
(I think converting to varchar is make more sense)
SELECT *
FROM customer
WHERE CONVERT(varchar, [date], 103) BETWEEN '01/12/2014' AND '31/12/2014'
In Date and Time Styles; 103 is for with British/French standard with century (yyyy) like this dd/mm/yyyy.
In this query i am unable to order by right
select CONVERT(varchar(10), cast(StartDate as date),101) +' - '
+ CONVERT(varchar(10), cast(EndDate as date),101) Duration
from dbo.Calendar
order by CONVERT(DATE, StartDate, 101) desc;
It gives me the following result which is not ordered right
mm/dd/yyyy
05/06/2013 - 06/29/2013
01/14/2013 - 04/26/2013
08/27/2012 - 12/15/2012
06/25/2012 - 08/18/2012
04/30/2012 - 06/23/2012
01/09/2012 - 04/23/2012
05/02/2011 - 08/22/2011
Instead of converting your order by to a varchar, leave it as a datetime.
select
CONVERT(varchar(10),
cast(StartDate as date),101) +' - ' + CONVERT(varchar(10),
cast(EndDate as date),101) Duration
from
dbo.Calendar
order by StartDate desc, EndDate;
Edit:
Added EndDate in your orderby as well, in case there are start dates that are the same
I misread your query initially. You cast StartDate to a DateTime already, so the order is correct.
Adding DESC to your datetime means that the newest or most recent value is first, oldest is last.
How do I convert hh:mm:ss to hh:mm in SQL Server?
select Count(Page) as VisitingCount,Page,CONVERT(VARCHAR(8),Date, 108) from scr_SecuristLog
where Date between '2009-05-04 00:00:00' and '2009-05-06 14:58'
and [user] in(select USERNAME
from scr_CustomerAuthorities )
group by Page,Date order by [VisitingCount] asc
CONVERT(VARCHAR(5),Date, 108)-- Gets only HH:mm
In general, the set of timestamps is not well-ordered, this means you cannot get a "last" timestamp whose time part up to minutes is 2009-05-06 14:58.
In SQL Server, which keeps the time part of a datetime as a number of 1/300 second fractions after midnight, this "last" timestamp would be 2009-05-06 14:58:59.997, but this is not guaranteed to be compatible with future releases of with other TIMESTAMP storage methods.
That means you'll need to split your BETWEEN condition into two conditions, one of which being strict less than the next minute:
select Count(Page) as VisitingCount,Page,CONVERT(VARCHAR(8),Date, 108) from scr_SecuristLog
where Date >= '2009-05-04 00:00:00'
AND Date < DATEADD(minute, 1, '2009-05-06 14:58')
and [user] in(select USERNAME
from scr_CustomerAuthorities )
group by Page,Date order by [VisitingCount] asc
This solution will efficiently use indexes on Date
SELECT Convert(varchar(5), GetDate(), 108)
Using varchar(5) will automatically truncate the date to remove the seconds.
I dont think there is a built in function; usually do something like this
SET #time = '07:45'
SET #date = CONVERT(DATETIME,#time)
SELECT #date
SELECT LEFT(CONVERT(VARCHAR,#date,108),5)
For this specific need you should use the between method as noted by Quassnoi's answer. However, the general problem can be solved with:
select dateadd(second, -datepart(second, #date), #date)
One way would be to use the RIGHT() function to crop the Date. Something like:
RIGHT(CONVERT(VARCHAR(8),Date, 108),5)
This will only work if number of characters is constant e.g. there is a leading zero if applicable. (Sorry havn't got SQL server here to test).
A better way is to use the T-SQL datepart function to split and then re-concatinate the date parts so:
DARTPART("hh", CONVERT(VARCHAR(8),Date, 108))+":"+DARTPART("mi", CONVERT(VARCHAR(8),Date, 108))
References:
http://msdn.microsoft.com/en-us/library/ms174420.aspx
http://msdn.microsoft.com/en-us/library/ms187928.aspx
http://msdn.microsoft.com/en-us/library/ms177532.aspx
To get hh:mm format from today's date:
select getdate()
select convert(varchar(5),getdate(),108)
To get hh:mm format from any datetime column in a table data:
select date_time_column_name from table_name where column_name = 'any column data name'
select convert(varchar(5),date_time_column_name,108) from table_name
where column_name = 'any column data name'
select creationdate from employee where Name = 'Satya'
select convert(varchar(5),creationdate,108) from employee
where Name = 'Satya'
SELECT CAST(CAST(GETDATE() AS Time(0)) AS VARCHAR(5))