mssql 2012 how display specific value - sql

i have query
SELECT DATEDIFF(day,
CONVERT(char(10), GetDate(),101),
CONVERT(char(10), fieldname,101))
from tablename
where isdate(fieldname)=1
the output of query is
how i can display only value like 6 from the result???

SELECT DATEDIFF(day, CONVERT(char(10), GetDate(),101), CONVERT(char(10), fieldname,101))
FROM lablename
WHERE isdate(fieldname)=1
AND DATEDIFF(day, CONVERT(char(10), GetDate(),101), CONVERT(char(10), fieldname,101)) = 6
edit 1
SELECT * FROM
( SELECT DATEDIFF(day, CONVERT(char(10), GetDate(),101), CONVERT(char(10), fieldname,101)) AS DateDiff
FROM lablename
WHERE isdate(fieldname)=1
) A
WHERE DateDiff = 6

Related

Get min and max between two dates

I'm querying the following code, to get min, max and avg, but still give me a problem, because it query all rows that I have from name = 'Jose' and not the min, max, and avg.
SELECT CONVERT(CHAR(10), DATA, 120) AS DATA,
MIN([VALUE]) AS MinValue,
AVG([VALUE]) AS MedValue,
MAX([VALUE]) AS MaxValue
FROM databasename
WHERE (name = 'Jose')
GROUP BY CONVERT(CHAR(10), DATA, 120)
ORDER BY DATA ASC
Example of what I get from the first query:
Image (can't embed image)
To query two dates, I'm using the following code but it don't get any result from query.
SELECT CONVERT(CHAR(10), DATA, 120) AS DATA,
MIN([VALUE]) AS MinValue,
AVG([VALUE]) AS MedValue,
MAX([VALUE]) AS MaxValue
FROM databasename
WHERE [DATA] between '2017-02-01' AND '2018-03-10' AND name = 'Jose'
GROUP BY CONVERT(CHAR(10), DATA, 120)
ORDER BY DATA ASC
I'm using MSSQL database.
UPDATE:
I already can query between two dates, as you can see in the second query. Problem that I'm not getting only min, max and avg.
Image:
try this SQL Query Convert(DATE,'2017-02-01').
SELECT DATA AS DATA,
MIN([VALUE]) AS MinValue,
AVG([VALUE]) AS MedValue,
MAX([VALUE]) AS MaxValue
FROM databasename
WHERE [DATA] between CONVERT(DATE, '2017-02-01') AND CONVERT(DATE, '2018-03-10')
AND name = 'Jose'
GROUP BY CONVERT(DATE, '2017-02-20')
ORDER BY CONVERT(DATE, '2017-02-20') ASC
can you consider small change in your query
SELECT CONVERT(CHAR(10), DATA, 120) AS DATA,
MIN([VALUE]) AS MinValue,
AVG([VALUE]) AS MedValue,
MAX([VALUE]) AS MaxValue
FROM databasename
WHERE CONVERT(CHAR(10), DATA, 120) between CONVERT(CHAR(10), 2017 - 02 - 20, 120) AND
CONVERT(CHAR(10), 2018 - 02 - 25, 120) AND name = 'Jose'
GROUP BY CONVERT(CHAR(10), DATA, 120)
ORDER BY DATA ASC

Extract date from datetime column - SQL Server Compact

I'm using SQL Server Compact 4.0 version, and although it might seem a simple thing to find in google, the examples I've tried none of them work.
My column signup_date is a DateTime with a value 04-09-2016 09:05:00.
What I've tried so far without success:
SELECT FORMAT(signup_date, 'Y-m-d') AS signup_date;
SELECT CONVERT(signup_date, GETDATE()) AS signup_date
SELECT CAST(data_registo, date) AS signup_date
I found that I could use DATEPART function, but that would force me to concat the values, is this the right path to follow? If so, how do I concat as Y-m-d?
SELECT DATEPART(month, signup_date)
SQL Server Compact has no date type.
If you don't want to see the time, convert the datetime value to a string:
SELECT CONVERT(nvarchar(10), GETDATE(), 120)
(This has been tested and actually works against SQL Server Compact)
you were actually on track with the CAST function just a slight error in the syntax. In the CAST function, there needs to be 'as' i.e CAST(data_registo as date)
SELECT CAST(data_registo as date) AS signup_date;
Most of the answers seek to achieve same thing but the explanation to the codes is not enough
CONVERT(date, Date_Updated, 120)
this code does the conversion with mssql. The first item 'date' is the datatype to return. it could be 'datetime', 'varchar', etc.
The second item 'Date_Updated' is the name of the column to be converted.
the last item '120' is the date style to be returned. There are various styles and the code entered will determine the output. '120' represent YYYY-MM-DD.
Hope this helps
The old fashioned way of doing this in SQL Server might work for your purposes:
select dateadd(day, datediff(day, 0, signup_date), 0)
The datediff() gets the number of days (as an integer) since time 0. The dateadd() adds this number back.
If you don't like 0 as a date, you can put any valid date in its place:
select dateadd(day, datediff(day, '2000-01-01', signup_date), '2000-01-01')
EDIT:
If you simply don't want to see the time, convert the date to a string:
select convert(nvarchar(10), signup_date, 120)
(I recommend the YYYY-MM-DD format, but others are available.)
I have tried this and many other solutions. I wanted a generic solution that would work with any LCID. My solution is a bit of convoluted code, but it works perfectly for me. It's a booking system where I needed to find out who was arriving on a particular date. ArriveDate is the column, d is the DATE I want.
SQL = "SELECT * FROM tablename WHERE dateadd(day, datediff(day, 0,
ArriveDate), 0)=' " & Format(d, "yyyy-MM-dd") & " ' "
This will return only date value in original datetime type. So you can do any comparison using the output
SELECT convert(datetime, CONVERT(nvarchar(10), GETDATE(), 120))
Just saw the Question today, a bit late I know :) but maybe this will help..,
select convert(date,(convert(varchar(20),'04-09-2016 09:05:00')))
select convert(nvarchar, getdate(), 1) = 09/25/19
select convert(nvarchar, getdate(), 2) = 19.09.25
select convert(nvarchar, getdate(), 3) = 25/09/19
select convert(nvarchar, getdate(), 4) = 25.09.19
select convert(nvarchar, getdate(), 5) = 25-09-19
select convert(nvarchar, getdate(), 6) = 25 Sep 19
select convert(nvarchar, getdate(), 7) = Sep 25, 19
select convert(nvarchar, getdate(), 10) = 09-25-19
select convert(nvarchar, getdate(), 11) = 19/09/25
select convert(nvarchar, getdate(), 12) = 190925
select convert(nvarchar, getdate(), 23) = 2019-09-25
select convert(nvarchar, getdate(), 101) = 09/25/2019
select convert(nvarchar, getdate(), 102) = 2019.09.25
select convert(nvarchar, getdate(), 103) = 25/09/2019
select convert(nvarchar, getdate(), 104) = 25.09.2019
select convert(nvarchar, getdate(), 105) = 25-09-2019
select convert(nvarchar, getdate(), 106) = 25 Sep 2019
select convert(nvarchar, getdate(), 107) = Sep 25, 2019
select convert(nvarchar, getdate(), 110) = 09-25-2019
select convert(nvarchar, getdate(), 111) = 2019/09/25
select convert(nvarchar, getdate(), 112) = 20190925
select convert(nvarchar, getdate(), 8) = 13:48:36
select convert(nvarchar, getdate(), 14) = 13:49:48:713
select convert(nvarchar, getdate(), 24) = 13:49:57
select convert(nvarchar, getdate(), 108) = 13:50:07
select convert(nvarchar, getdate(), 114) = 13:50:14:490
select convert(nvarchar, getdate(), 0) = Sep 25 2019 1:50PM
select convert(nvarchar, getdate(), 9) = Sep 25 2019 1:50:31:813PM
select convert(nvarchar, getdate(), 13) = 25 Sep 2019 13:50:39:307
select convert(nvarchar, getdate(), 20) = 2019-09-25 13:50:49
select convert(nvarchar, getdate(), 21) = 2019-09-25 13:50:58.923
select convert(nvarchar, getdate(), 22) = 09/25/19 1:51:07 PM
select convert(nvarchar, getdate(), 25) = 2019-09-25 13:51:14.473
select convert(nvarchar, getdate(), 100) = Sep 25 2019 1:51PM
select convert(nvarchar, getdate(), 109) = Sep 25 2019 1:51:32:227PM
select convert(nvarchar, getdate(), 113) = 25 Sep 2019 13:51:38:740
select convert(nvarchar, getdate(), 120) = 2019-09-25 13:51:50
select convert(nvarchar, getdate(), 121) = 2019-09-25 13:51:57.153
select convert(nvarchar, getdate(), 126) = 2019-09-25T13:52:03.627
Use this, i had the same problem
SELECT CAST(data_registo as date) AS "signup_date"
To get a string value, use CONVERT
select convert(varchar(10), signup_date, 11)
Check here for various formats:
https://msdn.microsoft.com/en-us/library/ms187928.aspx
To get a DATE, and just strip out the time, do this
Select Cast (signup_date as DATE)

converting datetime with out the time in sql

I am trying to convert the datetime output into a date only and i would like to have it in this format:
mm/dd/yyyy
what am i doing wrong here:
select
DATEADD(DAY, CONVERT(INT,(DATEPART(WEEKDAY, dt) - 1)) * -1,dt) as [WeekBeginDate],
SUM(hours) AS TOTAL_HOURS
from myTable
where
uid = 'myUID' and dt >= CAST(DATEADD(WEEK,-5,DATEADD(DAY,(DATEPART(WEEKDAY, GETDATE()) - 1) * -1,GETDATE())) AS date) group by DATEADD(DAY, CONVERT(INT,(DATEPART(WEEKDAY, dt) - 1)) * -1,dt) order by 1
You can use format 101 for convert:
select convert(varchar(10), dt, 101)
If you want the week begging date using your expression:
select convert(varchar(10), DATEADD(DAY, CONVERT(INT,(DATEPART(WEEKDAY, dt) - 1)) * -1,dt), 101) as [WeekBeginDate]

Subqueries with update statement in sql

I have the following query:
SELECT
CONVERT(datetime, CONVERT(varchar, new_time, 101)) As day,
datepart(hh,new_time) As hour,count(*) As Total
FROM
log_table
WHERE
new_time > GETDATE() - 180
GROUP BY
CONVERT(datetime, CONVERT(varchar, new_time, 101)),datepart(dd,new_time), datepart(hh,new_time)
ORDER BY
CONVERT(datetime, CONVERT(varchar, new_time, 101)), datepart(hh,new_time));
I need to update the table 'tmp_table' based on its results. I tried the following, but it's not working:
UPDATE tmp_table
SET count=Total
WHERE date=day AND hour=hour
FROM
(
select CONVERT(datetime, CONVERT(varchar, new_time, 101)) As day,
datepart(hh,new_time) As hour,count(*) As Total
from log_table
where new_time > GETDATE() - 180
group by CONVERT(datetime, CONVERT(varchar, new_time, 101)),datepart(dd,new_time),
datepart(hh,new_time)
order by CONVERT(datetime, CONVERT(varchar, new_time, 101)), datepart(hh,new_time))
)
I need to get the values "Total", "day" and "hour" from the subquery.
If you only want to update, then you don't need to order by and therefore you can use a CTE and a join as such:
With CTE AS
(
SELECT CONVERT(DATETIME, CONVERT(VARCHAR, new_time, 101)) As day,
DATEPART(hh,new_time) As hour,
COUNT(*) As Total
FROM log_table
WHERE new_time > GETDATE() - 180
GROUP BY CONVERT(DATETIME, CONVERT(VARCHAR, new_time, 101)),
DATEPART(dd,new_time),
DATEPART(hh,new_time)
)
UPDATE tmp_table
SET Count= CTE.Total
FROM tmp_table INNER JOIN CTE
ON temp_table.date=cte.day AND temp_table.hour=cte.hour

How do I group DATE field by YEAR-MM in SQL Server?

I have a date field in a query and I do want to get GROUP BY report like this:
DATE COUNT
2010-01 10
2010-02 2
...
2010-12 24
2012-13 34
What is the proper syntax to obtain this on SQL Server?
All these conversions to string work, but I find this method more efficient, albeit less readable:
SELECT m = DATEADD(MONTH, DATEDIFF(MONTH, 0, [DATE]), 0), COUNT(*)
FROM dbo.TheTable
GROUP BY DATEADD(MONTH, DATEDIFF(MONTH, 0, [DATE]), 0);
If you don't want to repeat the expression, then:
;WITH x AS (SELECT m = DATEADD(MONTH, DATEDIFF(MONTH, 0, [DATE]), 0)
FROM dbo.TheTable)
SELECT m, COUNT(*)
FROM x GROUP BY m;
This way the output is still a date/time value and can be used that way for other things. And it doesn't involve any messy string conversions.
CONVERT(VARCHAR(7), CreationDate, 120) as Date
You can simply do:
select convert(char(7), #myDate, 20)
Example
declare #myDate as DateTime
set #myDate = '2012-06-23'
select convert(char(7), #myDate, 20)
Output
-------
2012-06
So the full statement would look like:
select convert(char(7), myDate, 20), count(*) as Count
from MyTable
group by convert(char(7), myDate, 20)
Update
The sample data includes the value 2012-13. I am going to assume this is a typo and that the number after the dash represents the month.
SELECT CAST(DATEPART(year, dateCol) as VARCHAR) + '-' + CAST(DATEPART(month, dateCol) as VARCHAR) as Date, count(*) As Count
FROM myTable
GROUP BY CAST(DATEPART(year, dateCol) as VARCHAR) + '-' + CAST(DATEPART(month, dateCol) as VARCHAR)