order by DATEDIFF gives Conversion failed error - sql-server

I'm editing a query i build in ms sql.
I need to order the items on date, but that gives an error.
locations_aanvang gives 2012-08-12(yyyy-mm-dd). so i extend it to 2012-08-12 00:00:00 +1:00 for EST time.
So to order the i need to convert it to unix timestamp (right?).
The query is:
SELECT TOP 6 * FROM jd_lighthouses
WHERE locations_aanvang != ''
ORDER BY (SELECT DATEDIFF(s, '1970-01-01', locations_aanvang+' 00:00:00 +1:00')) DESC
And the error i get is:
[Microsoft][SQL Server Native Client 10.0][SQL Server]Conversion failed when converting date and/or time from character string.
What am i doing wrong? and can i do this easier?
Thanks in advance!
Kind regards,
Bram Hammer

Because we can'r clarify to question, only help is that:
if locations_aanvang is character type then you can avoid error changing code like this:
DATEDIFF(s, '1970-01-01', locations_aanvang + ' 00:00:00')
Since you use SQL Server Native Client 10.0 i assume you have SQL Server 2008 and DATE datatype. To get newest locations use:
ORDER BY CAST(locations_aanvang as DATE) DESC

Hmmm. Let me take a stab.
I think MS SQL warns against sorting by a column/calculation that
isn't directly in return results of your SQL. This is definitely so
in Oracle.
I'm assumming you can plug in a literal value for your
variable and it runs just fine?

Related

SQL Server SQL to PostgreSQL SQL

I need to convert the following SQL server code to PostgreSQL code.
Any help would be appreciated.
SQL Server SQL:
CAST(DATEADD(ww,DATEDIFF(ww,0,trans_date),-1)as date) as week
I think what that code does is to "round" the value of trans_date to the beginning of the week. In Postgres you can do that using the date_trunc() function:
date_trunc('week', trans_date)
Note that this always returns a timestamp, if you need a real date value, cast the result:
date_trunc('week', trans_date)::date
If it should be the day before the beginning of the week, just subtract one day from the result:
date_trunc('week', trans_date)::date - 1

SQL Server Incorrectly Parsing Date

SQL Server 2005
SELECT TOP 10 * FROM abc WHERE aDate = '2014-01-20'
When querying the above in SSMS it would normally return results where aDate is 20 January 2014. However for another user on the same server, it returns a date conversion error and only works when running the following query:
SELECT TOP 10 * FROM abc WHERE aDate = '2014-20-01'
I've checked regional language settings on the local machine and it's exactly the same as mine. Any ideas welcomed.
It is not the regional language settings on the machine that count in this case but the one defined on the database's options.
Anyway, to avoid having to rely on the regional language settings when parsing datetime in queries I would encourage you to use an invariant ISO date format : {d 'yyyy-MM-dd'}. Note there is also one for specifying the hours (ts).
It was account specific, the setting was stored as 'British - English' as opposed to 'English'. Changing this to 'English' resolved the problem. Thank you for your responses.
This error occurred as the SQL server tries parse the date value 20 as month and it causes error as 20 is not a valid month .Always It is good practice to use the date format 'dd-MMM-yyyy' which will work with any type of SQL COLLATION and regional language settings.

MS Access / Excel SQL Server query producing Conversion error DATETIME

I've looked around for a solution for this, but none of them seem to fix my problem
SELECT TOP 10 [Appointment_Date]
FROM dbo.RF_Performance_Referrals_Main
WHERE (([Appointment_Date]) < '7/21/2014')
ORDER BY [Appointment_Date] DESC
Above is the Simplified version of the query I am trying to. I keep get an error
[Microsoft][ODBC SQL Server Driver][SQL Server]Then Conversion of a
varchar datatype to a datetime data type resulted in an out of range
value
I thought maybe there is a problem with my connection...
so I tried it on Excel and got the exact same error...
I checked the SQL Server table that particular field is set to DATETIME
So why is it giving an error?
I tried all sorts of stuff on Cast/Convert, but none of them seem to work i still get the same error. I don't really get why.
Please help if fix this.
Thanks a lot in advance
SQL Server's default date time syntax is YYYY-MM-DD HH:MM:SS as in (1900-01-01 00:00:00)
You either have to cast your string to a date or reformat your input to the expected datetime defaults:
http://msdn.microsoft.com/en-us/library/ms187819.aspx: Assuming US Local and standard defaults.
Change to
SELECT TOP 10 [Appointment_Date]
FROM dbo.RF_Performance_Referrals_Main
WHERE (([Appointment_Date]) < #2014/07/21#)
ORDER BY [Appointment_Date] DESC

Search By Date in SQL Server 2012

We just upgraded to SQL Server 2012 from 2005. While I'm a novice, something this simple couldn't be this difficult. I used to be able to pull data from a table based on the date vs date and time. As it now stands I have:
Select * from receipts_table where receipt_cancel_date = '2013-09-20'
before we upgraded this would work fine. How can I run this and actually get the desired results as I know there's receipts with a cancel date of 2013-09-20.
Thanx
If you are passing string for a date parameter, best format is ISO (yyyymmdd) format. Otherwise even though your string work in some servers it might not work in another depending on the culture of the server. ISO format is culture independent.
Also remove the time part from receipt_cancel_date column by converting it to a DATE (if DATETIME) for comparison purpose.
Try this:
Select * from receipts_table
where convert(date, receipt_cancel_date) = convert(date,'20130920')
Or use 120 style with your format:
Select * from receipts_table
where convert(date, receipt_cancel_date) = convert(date,'2013-09-20',120)

Datetime function to get specific dates 12:00AM time

if any date time value provided to sql server can i get it's midnight value with some function in sql server.. for example if i provide 2013/07/03 01:34AM , i want to get it to 2013/07/03 12:00 AM.Is there a way to do it?
SQL Server 2008+
SELECT CAST(CAST('2013/07/03 01:34AM' AS date) AS datetime)
For older versions, see this Best approach to remove time part of datetime in SQL Server Never use anything that requires float or int or varchar conversions
This should give you what you need:
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, InputDateField), 0)
Should be slightly quicker than cast:
Most efficient way in SQL Server to get date from date+time?

Resources