Extract date part from datetime in SQL Server Compact 3.5 - sql

I am using SQL Server Compact 3.5 version for a small application. I want to extract the date part from a column (which is of datetime data type) and I used following query to try and do that:
string qry = "SELECT DISTINCT DATE(Finger_Record) as [Working Date] FROM dt_raw_data ";
When I execute it, I am getting a error saying DATE function is not a function in SQL Server Compact. Then how can I do this? Please help me.

Try this code it is work for me
select distinct CONVERT(VARCHAR(10),Finger_Record,110) as [Working Date] from dt_raw_data

Try this code:
string qry = "SELECT DISTINCT CONVERT(nvarchar(10), Finger_Record, 101) as [Working Date] FROM dt_raw_data ";

Related

SQL Server datetime Format Isn't Working

TimeOpened is a column in my dataase table of type datetime2.
My convert statement is:
convert(nvarchar, TimeOpened, 114)
My output is supposed to be just the time (24 hours based) without the date, i.e.
5/16/2016 1:38:00 PM --> 13:38:00
but in practice the output I get is 01:38:00
Why is this happening?
Additionally I would like to know how to remove the seconds. Basically I want my output to be 13:38.
Thanks.
Try this (SQL Server 2012 or later):
SELECT FORMAT(TimeOpened,'dd/MM/yyyy HH:mm:ss')
For sql server 2008 you can cast to time and then convert to char(5):
SELECT CONVERT(char(5), CAST(TimeOpened As Time))
SELECT FORMAT(cast(TimeOpened as time),N'hh\.mm') as mytime
you can see more examples on https://msdn.microsoft.com/en-us/library/hh213505.aspx#ExampleD
try this
SELECT LEFT(CONVERT(VARCHAR,TimeOpened,108),5)
Solved it.
The problem was that when I inserted the date I used c# datetime.now instead of getdate()

SQL Server Query: Convert to dd/mm/yyyy from datetime

I'm querying a max datetime to bring in the most recent record from another table surrounding a common ID coloumn. I would like to however format this datetime in the standard UK dd/mm/yyyy format. How would I do so? I just cant seem to get this...
Here is my code:
(SELECT TOP 1 MemberPayments.CoverFinishDay
FROM
Members LEFT JOIN MemberPayments
ON Members.MemberID = MemberPayments.MemberID
AND CoverFinishDay = (
SELECT MAX(CoverFinishDay)
FROM MemberPayments
WHERE Members.MemberID = MemberPayments.MemberID
))
CoverFinishDay is stored in standard american datetime. currently the query works, just in the wrong format. I need the output in dd/mm/yyyy
This will help you:
SELECT CONVERT(VARCHAR, GETDATE(), 105)
This is for SQL Sever
If you are using SQL Server 2012 you can convert any date using FORMAT function. In your case you can do like this:
SELECT FORMAT (MemberPayments.CoverFinishDay, 'd', 'en-GB')
For mor info you can access the page: http://msdn.microsoft.com/en-us/library/hh213505(v=sql.110).aspx

Convert custom numeric datevalue to real date in sql query

I have an application that comes with its own database and there is nothing I can change on that configuration. However we do pull data from the database to generate reports on.
For some reason (which I don't quite grasp), the application stores dates as the following number:
numdate = (int) '1' & <last two digits of year> & <zero-leading month> & <zero-leading day>
eg:
08/10/2008 -> 1081008
01/01/2014 -> 1140101
27/02/2014 -> 1140227
For now I just pull in the number and convert it on the go to a real date.
Is it possible to do this conversion via the sql query somehow?
If it's SQL Server, the following should work:
DECLARE #dateInt INT = 1981008
SELECT CONVERT(DATETIME, SUBSTRING(CONVERT(VARCHAR, #dateInt), 2, 6), 12)
To save you reading the comments, Gunther improved the performance by removing the substring operation in favour of subtraction:
SELECT CONVERT(DATETIME, CONVERT(VARCHAR,#dateInt-1000000), 12)
You could use this if you are using C#.
string dateString = "1981008";
DateTime dt;
DateTime.TryParseExact(dateString.Remove(0,1), "yyMMdd", null, System.Globalization.DateTimeStyles.None, out dt);
In SQL (assuming it is MS SQL), try this:
convert(datetime,'981008',112)

Using cast in SQL with multiple column selections

Basically I'd like to get this command to work:
$sql = "SELECT (EntryDate + TotalTime) as DataLine FROM TimeSheet WHERE EmployeeID='AA01'";
EntryDate is in the database as a text, but TotalTime is a Number. I need to cast TotalTime as a text, because I've noticed if I combine two differing types of values, it just blanks out the output.
I know I'm supposed to use CAST(TotalTime as XXX) but I'm not sure what I can legally cast it to (char/nchar doesn't seem to work, neither does string nor text). I always get an error of the form...
Syntax error (missing operator) in query expression '(EntryDate + CAST(TotalTime as string) as DataLine FROM TimeSheet WHERE EmployeeID='AA01''
Could I get some help? Thank you!
EDIT I would like to note this isn't intended to add together the values of EntryDate and TotalTime together to produce a single value. I simply want it to give me the EntryDate value as well as the TotalTime value combined into a single line that would read something like:
"10/31/12 9.25"
EDIT AGAIN I'm sorry for not specifying before, I'm using MSSQL
Try this instead:
SELECT
CAST(EntryDate AS VARCHAR(25)) + CAST(TotalTime AS VARCHAR(10)) as DataLine
FROM TimeSheet
WHERE EmployeeID = 'AA01';
However, if entrydate is a date and the int totaltime is a time, it may be better to consider converting them as a datetime object by adding them as a date part to the time part depending on the RDBMS you are using. And if possible use a DATETIME datatype to represent both date and time parts instead of two parts. Like so:
SELECT DATEADD(hh, totaltime, entrydate)
FROM TimeSheet
WHERE EmployeeID = 'AA01';
SQL Fiddle Demo
MSSQL:
$sql = "SELECT EntryDate+''+CAST(TotalTime as char(25)) as DataLine FROM TimeSheet WHERE EmployeeID='AA01'";
MySQL
$sql = "SELECT CONCAT(EntryDate,' ',CONVERT(TotalTime,char(25))) as DataLine FROM TimeSheet WHERE EmployeeID='AA01'";

Error trying to execute sql statement between two dates

The SQL:
dim sql
sSql = "SELECT * FROM [dbo].[table] WHERE [sent] = 1 and datesent between '" & dStartDate & "' and '" & dFinishDate & "'"
response.write(sSql)
set oRs = oConn.execute(sSql)
When i execute this sql in sql server 2008 it works fine.
However, when i execute it within my application i get error:
Microsoft OLE DB Provider for SQL Server error '80040e07'
The conversion of a char data type to a datetime data type resulted
in an out-of-range datetime value.
Is there something different i have to do in the application? thanks
Have you tried using ISO format dates?
SELECT * FROM [dbo].[Table]
WHERE [sent] = 1 and datesent between '20120101' and '20120127'
(Strictly speaking an ISO 8601 date for sql server is yyyy-mm-ddThh:mm:ss[.mmm])
Side Note: always use ISO format dates when you output to text (or have literals); that way they can be read unambiguously on all systems).
SQL Server ISO 8601 Format Dates
http://msdn.microsoft.com/en-us/library/ms190977(v=sql.90).aspx
SELECT *
FROM [dbo].[Table]
WHERE
[sent] = 1
and datesent between CONVERT(datetime, '01/01/2012', 101) and CONVERT(datetime, '01/27/2012', 101)
OR
Your column datesent is not DATETIME datatype
Check your datetime setting.
set dateformat mdy
select CAST('01/27/2012' as datetime)
set dateformat dmy
select CAST('01/27/2012' as datetime) --> Exception
You can display the current setting with:
DBCC USEROPTIONS
You should always use the ISO-formats 'YYYYMMDD' for your dates, as this will always be parsed correctly by SQL Server, regardless of localization setting.