How to get 3 letter abbreviation for month in SQL - sql-server

How to get month in 3 letters in SQL.
In SQL Table data is inserted:
2016-01-07 09:38:58.310
I need only month result in 3 letters like below:
Jan

Assuming you're using SQL Server 2012 or newer, you can use the FORMAT function:
SELECT FORMAT([Date], 'MMM', 'en-US')
Adapt the locale as needed.
Since you're on SQL Server 2008, I'd use
SELECT LEFT(DATENAME(MONTH, [Date]), 3)

Try this (I am assuming you are using Sql Server).
Select Convert(char(3), GetDate(), 0)
If you need full name of month, try
Select Datename(month, GetDate())

Or you could just do:
LEFT(GETDATE(), 3)
For instance you could declare a variable:
Declare #MONTH VARCHAR(3)
Set #MONTH = LEFT(GETDATE(), 3)

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'

Find Weekending Date (Friday) based on another Date column in SQL Server?

I am trying to replicate a weekending query that I use in access, in SQL Server and not having much luck with getting ti to perform how I need it to.
Basically, I need to find the week-ending date (with the week-ending date being a Friday) of another date column and would like it formatted to be American short date (eg. 10/06/2017).
I use the below in Access which gets me the result I need. So if the ACTUAL_DATE is 10/03/2017, the result I would need is 10/06/2017.
Actual_Date_WE: [ACTUAL_DATE] + 7 - Weekday([ACTUAL_DATE], 7)
Thank you! :)
try this
Declare #Date date
SET #Date = '2017-10-01'
select dateadd(day, (7+(6 - datepart(WEEKDAY, #date))) %7, #date)
declare #i date='10/3/2017'
declare #Friday int=6
SELECT convert(varchar,dateadd(day,(#Friday-DATEPART(dw,#i)),#i) ,110)

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)

sql server datetime

i have the following query:
select * from table where table.DateUpdated >='2010-05-03 08:31:13:000'
all the rows in the table being queried have the following DateUpdated:
2010-05-03 08:04:50.000
it returns all of the rows in the table - even though it should return none.
I am pretty sure this is because of some crappy date/time regional thing.
if i swap the date to be
select * from table where table.DateUpdated >='2010-03-05 08:31:13:000'
then it does as it should.
How can i force everything to be using the same settings? this is doing my head in :)
This is sql generated by NHIbernate from my WCF service if that matters.
w://
Use this format "yyyymmdd hh:nn:ss.mmmm" which is locale independent in SQL Server, all versions
Somewhere, it's 5th Feb rather then 3rd May
Why:
"yyyy-mm-dd" is not locale independent in SQL Server with datetime columns
this anomaly is fixed with datetime2 in SQL Server 2008
References:
Tibor Karaszi
Tony Rogerson
Me, on SO :-)
Example:
SET DATEFORMAT DMY --UK
SELECT
MONTH(CAST('2010-03-05 08:31:13:000' AS datetime)), --gives 5
MONTH(CAST('20100305 08:31:13:000' AS datetime)) --gives 3
SET DATEFORMAT MDY --default, USA
SELECT
MONTH(CAST('2010-03-05 08:31:13:000' AS datetime)), --gives 3
MONTH(CAST('20100305 08:31:13:000' AS datetime)) --gives 3
You could try:
select * from table
where Convert(DateTime, table.DateUpdated,103) >= Convert(DateTime, '2010-05-03 08:31:13:000',103)
The answer to this was to upgrade to 2008 and use datetime2
what a PITA!!!

Resources