Using LIKE in ms sql server management studio - sql-server

Question: When I use the LIKE in my query it is gray and my query does not return anything when I know if should.
I am not sure if there is a configuration setting I am missing but normally when I use MS sql server management studio terms like: UPDATE WHERE SELECT ADD all show as blue in my query window.
Example query:
SELECT *
FROM [MainSiteDB].[dbo].[usr_user]
WHERE [MainSiteDB].[dbo].[usr_user].[usr_lastLogin] LIKE '2014-11-10%'
In my table in that field there are many entries like: 2014-11-10 13:19:46.967

Like is not applicable for datetime or date field for SQL server. 'Like' is for varchar/char/text and other text related field
You can use between or <> sign
Select * from tblTable where Date between '2014 Jan 01' and '2014 Jan 31'
OR
Select * from tblTable where Date >= '2014 Jan 01' and Date <= '2014 Jan 31'

You can find in menu in Management studio menu under Tools->Option
then Fonts and color. Then you can change the customize color for
SQL keywords.

you Can Use it :
SELECT *
FROM [MainSiteDB].[dbo].[usr_user]
WHERE DATEADD(dd, 0, DATEDIFF(dd, 0, [MainSiteDB].[dbo].[usr_user].[usr_lastLogin]))= '2014-05-9'

You can cast the date part and then use the LIKE operator. Check the below script:
Select * from TableName Where colName like Cast('2014-09-25' as datetime)

select * from tablename
where CONVERT(varchar(10), getdate(), 105) = #inputdate
Below convert will fetch the datepart alone from your columnname, and then based on your input it will fetch all matching records.
SELECT CONVERT(varchar(10), getdate(), 105) --> 11-11-2014

Try this query. Using LIKE directly in DateTime filed is won't give expected result. Convert the DateTime field to specific format to Varchar and use LIKE.
101 - Format the DateTime to mm/dd/yyy
SELECT *
FROM [MainSiteDB].[dbo].[usr_user]
WHERE CONVERT(VARCHAR(20), [MainSiteDB].[dbo].[usr_user].[usr_lastLogin], 101)
LIKE '05/22/2014%'
105 - Format the DateTime to mm-dd-yyy
SELECT *
FROM [MainSiteDB].[dbo].[usr_user]
WHERE CONVERT(VARCHAR(20), [MainSiteDB].[dbo].[usr_user].[usr_lastLogin], 105)
LIKE '05-22-2014%'

Related

Extract only month from SQL Query

I've a SQl Query. I'm using dropdownlist to display the dates. I'd like to display the month in MMMM format.
SELECT DISTINCT[drdates] (CONVERT(CHAR(4), [drdates], 100) + CONVERT(CHAR(4), [drdates], 120)) FROM [DRReceive_20141229]
SELECT DISTINCT UPPER(LEFT(DATENAME(MONTH,MONTH([drdates])),4))
+ CONVERT(CHAR(4), [drdates], 120)
FROM [DRReceive_20141229]
On a side note I have never seen date values being formatted as MMMMyyyy, a rather strange format to show date values.
But if you wanted something rather simple or usual format like MMMyyyy and if you are using sql server 2012 or later version you can do the following ...
SELECT DISTINCT UPPER(FORMAT ( [drdates], 'MMMyyyy' ))
FROM [DRReceive_20141229]
This will help you to retrieve months first four characters :
select CONVERT(CHAR(4),DATENAME(MONTH, [drdates])) + CONVERT(CHAR(4),[drdates], 120)) [DRReceive_20141229];

Convert a SQL Server datetime to a shorter date format

I have a datetime column in SQL Server that gives me data like this 10/27/2010 12:57:49 pm and I want to query this column but just have SQL Server return the day month and year - eg. 2010 10 27 or something like that.
What are the functions I should be researching?
Should I be trying to convert to another date data type? Or simply convert it to a string?
Have a look at CONVERT. The 3rd parameter is the date time style you want to convert to.
e.g.
SELECT CONVERT(VARCHAR(10), GETDATE(), 103) -- dd/MM/yyyy format
Try this:
print cast(getdate() as date )
If you need the result in a date format you can use:
Select Convert(DateTime, Convert(VarChar, GetDate(), 101))
In addition to CAST and CONVERT, if you are using Sql Server 2008, you can convert to a date type (or use that type to start with), and then optionally convert again to a varchar:
declare #myDate date
set #myDate = getdate()
print cast(#myDate as varchar(10))
output:
2012-01-17
If you have a datetime field that gives the results like this 2018-03-30 08:43:28.177
Proposed: and you want to change the datetime to date to appear like 2018-03-30
cast(YourDateField as Date)
With SQL Server 2005, I would use this:
select replace(convert(char(10),getdate(),102),'.',' ')
Results: 2015 03 05
The shortest date format of mm/dd/yy can be obtained with:
Select Convert(varchar(8),getdate(),1)
Just add date keyword.
E.g. select date(orderdate),count(1) from orders where orderdate > '2014-10-01' group by date(orderdate);
orderdate is in date time.
This query will show the orders for that date rather than datetime.
Date keyword applied on a datetime column will change it to short date.
For any versions of SQL Server: dateadd(dd, datediff(dd, 0, getdate()), 0)
The original DateTime field : [_Date_Time]
The converted to Shortdate : 'Short_Date'
CONVERT(date, [_Date_Time]) AS 'Short_Date'

How to get Time from DateTime format in SQL?

I want to get only Time from DateTime column using SQL query
using SQL Server 2005 and 2008
Default output:
AttDate
==
2011-02-09 13:09:00
2011-02-09 14:10:00
I'd like this output:
AttDate Time
==
2011-02-09 13:09:00 13:09
2011-02-09 14:10:00 14:10
SQL Server 2008:
SELECT cast(AttDate as time) [time]
FROM yourtable
Earlier versions:
SELECT convert(char(5), AttDate, 108) [time]
FROM yourtable
Assuming Sql server
SELECT CONVERT(VARCHAR(8),GETDATE(),108)
SQL Server 2008+ has a "time" datatype
SELECT
..., CAST(MyDateTimeCol AS time)
FROM
...
For older versions, without varchar conversions
SELECT
..., DATEADD(dd, DATEDIFF(dd, MyDateTimeCol, 0), MyDateTimeCol)
FROM
...
The simplest way to get the time from datetime without millisecond stack is:
SELECT convert(time(0),getDate())
Try using this
Date to Time
select cast(getdate() as time(0))
Time to TinyTime
select cast(orig_time as time(0))
Try this, it will work:
CONVERT(VARCHAR(8),DATETIME,114)
For your reference.
Try this:
select convert(nvarchar,CAST(getdate()as time),100)
Get date of server
SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), GETDATE(), 100), 7)) FROM TABLENAME WHERE ...
or
If it is stored in the table
SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), datename, 100), 7)) FROM TABLENAME WHERE ...
Result:
11:41AM
select AttDate,convert(char(5), AttDate, 108) [Time] from yourTableName
To get the time from datetime, we can use
SELECT CONVERT(VARCHAR(20), GETDATE(), 114)
select cast (as time(0))
would be a good clause. For example:
(select cast(start_date as time(0))) AS 'START TIME'
I often use this script to get Time from DateTime:
SELECT CONVERT(VARCHAR(9),RIGHT(YOURCOLUMN_DATETIME,9),108) FROM YOURTABLE
If you want date something in this style: Oct 23 2013 10:30AM
Use this
SELECT CONVERT(NVARCHAR(30),getdate(), 100)
convert() method takes 3 parameters
datatype
Column/Value
Style: Available styles are from 100 to 114. You can choose within range from. Choose one by one to change the date format.
on MSSQL2012 or above
cast(dateadd(ms,datediff(ms, [StartDateTime], [StopDateTime]),0) as Time(0))
...or...
convert(time(0),dateadd(ms,datediff(ms, [StartDateTime], [StopDateTime]),0) )
SQL Server 2012:
Select TRY_CONVERT(TIME, myDateTimeColumn) from myTable;
Personally, I prefer TRY_CONVERT() to CONVERT(). The main difference: If cast fails, TRY_CONVERT() returns NULL while CONVERT() raises an error.
You can use this:
SELECT CONVERT(VARCHAR(5), GETDATE(),8)
Output:
08:24
Declare #date Datetime = '06/18/2021 14:24:31';
Select FORMAT(#date, 'h\:m tt', 'en-US') As Timey
Output:
2:24pm
select convert(char(5), tbl_CustomerBooking.CheckInTime, 108) AS [time]
from tbl_CustomerBooking
select substr(to_char(colUmn_name, 'DD/MM/RRRR HH:MM:SS'),11,19) from table_name;
Output: from
05:11:26
05:11:24
05:11:24

sql select datetime stamp as m/d/y only. (without h m s)

When I select two rows with a DATETEIME stamp, I only want the m/d/y data and nothing after that.
It has to changed during the select (not afterwards).
To remove the time you just need to do the following Assuming SQL Server
Pre SQL 2008
select DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0) yourdate
FROM yourtable
SQL 2008
select CAST(getdate() as date) yourdate
FROM yourtable
See Most efficient way in SQL Server to get date from date+time?
or
Best approach to remove time part of datetime in SQL Server
Is this for export? If you only want the text you can use a variety of coversion formats available on MSDN.
select convert(varchar, getdate(), 101)
-- output: 07/05/2011
Otherwise, if you're using sql 2008, you can just cast the datetime to date:
select cast(getdate() as date)

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