convert datetime is not running to view, - sql-server

i tried to this query. but its not running. how can i do?
SELECT dbo.isemri_data.sipnum,
dbo.kayitlar.id,
dbo.kayitlar.makina_id,
dbo.kayitlar.personel,
dbo.kayitlar.isemrino,
dbo.kayitlar.tarih,
dbo.kayitlar.gercek_hiz,
dbo.kayitlar.durus_kod,
dbo.kayitlar.miktar,
dbo.kayitlar.fire
FROM dbo.isemri_data
LEFT OUTER JOIN dbo.kayitlar
ON dbo.isemri_data.isemrino = dbo.kayitlar.isemrino
WHERE CONVERT(VARCHAR(10), dbo.kayitlar.tarih, 104) BETWEEN
CONVERT(VARCHAR(10), '12.08.2015', 104) AND
CONVERT(VARCHAR(10), '19.08.2015', 104)
AND CONVERT(VARCHAR(3), dbo.kayitlar.makina_id) = 'M1'
AND dbo.kayitlar.isemrino LIKE '%'
if delete
CONVERT(varchar(10),dbo.KAYITLAR.TARIH,104) BETWEEN convert(varchar(10),'12.08.2015',104) and convert(varchar(10),'19.08.2015',104)
query is running.
or if run to only main table allready running my query.
i cant find what is the problem.

Since you have mentioned the data type is Datetime , DO NOT convert it to anything else in there where clause, do it in select if you want to.
SELECT dbo.isemri_data.sipnum,
dbo.kayitlar.id,
dbo.kayitlar.makina_id,
dbo.kayitlar.personel,
dbo.kayitlar.isemrino,
dbo.kayitlar.tarih,
dbo.kayitlar.gercek_hiz,
dbo.kayitlar.durus_kod,
dbo.kayitlar.miktar,
dbo.kayitlar.fire
FROM dbo.isemri_data
LEFT OUTER JOIN dbo.kayitlar
ON dbo.isemri_data.isemrino = dbo.kayitlar.isemrino
AND dbo.kayitlar.tarih >= '20150812' -- use ANSI date YYYYMMDD
AND dbo.kayitlar.tarih <= '20150819' -- use ANSI date YYYYMMDD
AND CONVERT(VARCHAR(3), dbo.kayitlar.makina_id) = 'M1'
AND dbo.kayitlar.isemrino LIKE '%'
Converting your datetime to a string means sql server treats them values as strings and not a date/datetime values hence you will get unexpected results Also sql server will not be able to make use of any indexes defined on that column if there are any.
Keep date/datetime values as it is and change the format of date in presentation layer for more easy to eyes date/datetime format.

i found answer. if i use
dbo.kayitlar.tarih BETWEEN
CONVERT(DATETIME, '12.08.2015', 104) AND
CONVERT(DATETIME, '19.08.2015', 104)
running query.
thanx for your help

Related

To check the old dates present or not

I have list of dates like 19/2/2017, 20/8/1975, 02/03/1989, 04/08/2015 . I need a query by using SQL server to find year which is less than current year.
eg: 19/2/2017, 20/8/1975, 02/03/1989
by using the query, it should display these two 20/8/1975, 02/03/1989
Hope this helps
SELECT * FROM YourTable WHERE YEAR(DateFiled) < YEAR(GETDATE())
To guarantee you convert the dates correctly, independently of the SQL Server ##DATEFORMAT
DECLARE #BadDateStorage table (BadData varchar(12));
INSERT #BadDateStorage (BadData)
VALUES ('02/03/1989'), ('20/8/1975'), ('019/2/2017');
SELECT CONVERT(smalldatetime, BadData, 103)
FROM #BadDateStorage
WHERE CONVERT(smalldatetime, BadData, 103) < '20170101'

How to convert mm/dd/yyyy militarytime -> mm/dd/yyyy hh:mm:ss AM/PM

I'm using the following code and almost getting what I'm looking for:
SELECT sdb.NAME AS DatabaseName
,COALESCE(CONVERT(VARCHAR(10), cast(max(bus.backup_finish_date) as date), 101) + ' ' + convert(varchar(12), max(bus.backup_finish_date), 108), 'Never Restored') as [LastBackupTime]
FROM sys.sysdatabases sdb
INNER JOIN dbo.backupset bus
ON bus.database_name = sdb.NAME
GROUP BY sdb.NAME,
bus.backup_finish_date
Your result should be something like:
mm/dd/yyyy HH:mm:ss
I'm trying to get
mm/dd/yyyy hh:mm:ss AM/PM
I've tried multiple converts, a series of casts, ltrim/right, and even offering homage to the T-SQL overlords. No luck yet.
I've even tried
SELECT sdb.NAME AS DatabaseName
--Code below needs changed to show Date & time--
,COALESCE(CONVERT(VARCHAR(30), MAX(bus.backup_finish_date), 100), 'Never
backed up.') AS LastBackUpTime
FROM sys.sysdatabases sdb
INNER JOIN dbo.backupset bus
ON bus.database_name = sdb.NAME
GROUP BY sdb.NAME,
bus.backup_finish_date
but that gets me (for example) Mar 21 2017 10:47AM. We really prefer 3/21/2017 10:47AM.
Suggestions? I'm still picking this apart but could use some help.
Thanks!
If you are using SQL Server 2012 or later, you can use FORMAT():
Select Format(Max(bus.backup_finish_date), N'MM/dd/yyyy hh:mm:ss tt')
If you are using SQL Server 2012 or later you can use FORMAT, although be wary of doing this on large data sets.
SELECT FORMAT(GETDATE(), 'MM/dd/yyyy hh:mm:sstt')
For earlier versions, or if performance is a concern, For earlier versions, or if performance is a concern, you can concatenate the date in the format MM/dd/yyyy (style 101), with the time in the format hh:mm:ss (style 8) and a case expression to determine AM or PM
SELECT CONVERT(VARCHAR(10), GETDATE(), 101) + ' '
+ CONVERT(VARCHAR(10), GETDATE(), 8)
+ CASE WHEN DATEPART(HOUR, GETDATE()) < 12 THEN 'AM' ELSE 'PM' END
HOWEVER, formatting is a job for the presentation layer. If it was me doing this, then I would just send the native datetime, including nulls back to the presentation layer and let this handle it. It means that in your application layer you can still work with the dates, perform date calculations, or sort etc without the worry that 15/01/2017 is going to appear after 02/02/2017. It also means you can display dates in the end user's preferred locale, rather than yours.
One easiest way is to use format but it is not highly performant:
select FORMAT(Max(bus.backup_finish_date),'MM/dd/yyyy hh:mm:ss tt')
For earlier versions one another naive way of doing is as below:
Select CONVERT(VARCHAR(10), getdate(), 101) + ' ' + LTRIM(RIGHT(CONVERT(CHAR(20), getdate(), 22), 11))
Instead of GetDate() use your date
But that is already mentioned by #GarethD So never mind

Convert varchar to date with malformed values in SQL Server 2008

This whole date code is beyond my understanding but non the less I need to convert a column in a query result from varchar to date. I have some malformed values and I'm trying to filter them out.
SET DATEFORMAT dmy
SET LANGUAGE us_english
select convert(datetime, some_date, 103)
from some_table
where isdate(some_date) = 1
I keep getting
Conversion failed when converting date and/or time from character string
When querying for the malformed values I get these two records
some_date
---------
621
232
Is there any way to validate the format using isdate ?
Thanks, Frustrated Oracle DBA.
EDIT
Tried #Sean Lange suggestions and indeed it looked promising
select convert(datetime, some_date, 103)
from ( select * from some_table where isdate(some_date) = 1 )
But then I added a where clause
select convert(datetime, some_date, 103)
from ( select * from some_table where isdate(some_date) = 1 )
where convert(datetime, some_date ,103) between
convert(datetime, '01/07/2014')
and convert(datetime,'31/07/2014')
How does a where clause being executed before the from?
Since we know that a valid date looks like dd/mm/yyyy how about a simple structure test?
select convert(datetime, some_date, 103)
from some_table
where len(some_date) = 10 --must have exactly the expected number of characters
This could be expanded upon (depending on requirements)
and some_date like '__/__/____'
Or maybe
and some_date like '[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]'
Anything better would probably need you to put together a custom function that does the CONVERT in a try block, returning NULL in the catch block if its an invalid record. Depends on your requirements...
The problem is happening and will continue to happen because you can't be certain which order the sql engine will take here. One time it might filter the rows first and another time it does the conversion. So what you need to do is force the order the engine works. We can do this by using a CTE to get only the rows we want to convert.
with MyCTE as
(
select some_date
from some_table
where ISDATE(some_date) = 1
)
select CONVERT(datetime, some_date, 103)
from MyCTE
If you're positive that these are the only malformed values and all malformed values will look like this, you could just go ahead and use a hackish CASE statement
SELECT (CASE WHEN LEN(datetime) > 3 THEN convert(datetime, some_date, 103) ELSE NULL),
...
It's really not elegant, but datetimes are notoriously difficult to make elegant because there are so many fragmented formats. If possible, it saves more headaches just to make sure that your data is correctly formatted in the first place through form validation and regex stuff, but since that's not always possible, we have crummy hacks like this to patch ourselves through. Best of luck!

Need help on DateTime Conversion in Sql Server 2005

I am facing problem in DateTime converstion.
My input is 09/22/2011, I need to convert into 20110922.
I tried the below one, but failed.
Select CONVERT(VARCHAR(8),'09/22/2011' , 112) as DateConv
Here you go:
SET DATEFORMAT mdy
SELECT CONVERT(VARCHAR(8), CAST('09/22/2011' as DATETIME) , 112) AS DateConv
If your input is actually a dateTime variable like you said (but didn't show in your code example), you can simplify this down to:
SELECT CONVERT(VARCHAR(8), #myDate , 112) AS DateConv
This will do the trick:
select year('09/22/2011') * 10000 + month('09/22/2011') * 100 + day('09/22/2011')
Without any other information, SQL is interpreting the '09/22/2011' as a varchar already, and just passes the data through (ignoring CONVERT's style argument). If you use the following line:
SELECT CONVERT (VARCHAR (8), CAST ('09/22/2011' as DATETIME), 112) as DateConv
it should work as expected since it will then view the '09/22/2011' as a date value. If you were getting the date from a column in a table, it would already know the type and you will not need to worry about the CAST portion of the expression, just use the column name.

Remove time from DateTime sql server 2005

I need date part from datetime. in format of "dd-mm-yyyy"
I have tried follwoing
Query:
select Convert(varchar(11), getdate(),101)
Output:
01/11/2011
Query
SELECT cast(floor(cast(GETDATE() as float)) as datetime)
Output
2011-01-11 00:00:00.000
Query:
SELECT
CONVERT(VARCHAR(MAX),DATENAME(DD,GETDATE())) + '-' +
CONVERT(VARCHAR(MAX),DATEPART(MONTH,GETDATE())) + '-' +
CONVERT(VARCHAR(MAX),DATENAME(YYYY,GETDATE())) `
Output:
11-1-2011 i.e. "d-m-yyyy"
I required output in "dd-mm-yyyy" format.
SELECT CONVERT(VARCHAR(10),GETDATE(),105)
Try:
SELECT convert(varchar, getdate(), 105)
More here.
Here you can find some examples how to do this: http://blog.pengoworks.com/index.cfm/2009/1/9/Useful-tips-and-tricks-for-dealing-with-datetime-in-SQL
Using the CONVERT function "works" but only if you're comparing strings with strings. To compare dates effectively, you really need to keep the SMALLDATETIME data type strongly typed on both side of the equation (ie "="). Therefore 'apros' comment above is really the best answer here because the blog mentioned has the right formulas to use to strip off the time component by "flattening" it to midnight (ie 12:00:00) via rounding and any date column in SQL Server 2005 will always default to 12:00:00 if the date is given without a time.
This worked for me ...
select dateadd(day, datediff(day, '20000101', #date), '20000101')

Resources