problem with convert text to time in sql server 2008? - sql-server-2008

i have this Ttime as nvarchar(10): "09:52:48" and i have TmpTime as date
and i try to convert like this: "UPDATE MEN SET TmpTime = CONVERT(DATETIME, Ttime ,108 )"
and i get in TmpTime this: "1900-01-01"
why ?
thank's in advance

If you also have a date field, you should to concatenate them before to cast:
CREATE TABLE #Sample ( DateField varchar(10), TimeField varchar(10) );
GO
INSERT INTO #Sample VALUES ('2009-01-24', '09:52:48');
SELECT CONVERT(DATETIME, DateField + ' ' + TimeField) as Converted FROM #Sample
And you'll get:
Converted
-----------------------
2009-01-24 09:52:48.000

You have a column defined as "date" and then you are sending only a time value into it
The date portion defaults to zero which is 01 January 1900 in SQL (in the CONVERT). Then the time is ignored for a date column.
What do you expect to happen?
(The same would happen whether or not you use CONVERT or not because the column is "date")

Related

MySQL STR_TO_DATE() function returns null

I wanted to convert my date format From MMMM dd,yyyy to yyyy-MM-dd.
I tried using the following:
SET #dt_to = STR_TO_DATE(dateTo, '%d-%m-%Y');
but returns a NULL value.
How will I convert my date to yyyy-MM-dd format in MySQL?
EDITED:
I am creating a procedure in which the value of dateTo was received in the parameter. It is a date in MMMM dd, yyyy format. E.g. October 10, 2015.
NOTE:
The whole query does not return NULL when I use:
SET #dt_to = dateTo;
To convert the date format first you need to use STR_TO_DATE to convert the input string to a date value
SET #dt_to = STR_TO_DATE(dateTo, '%M %d,%Y');
and then convert that date value to your required format
SET #dt_converted = DATE_FORMAT(dt_to, '%Y-%m-%d');
or all in 1 go
SET #dt_to = DATE_FORMAT(STR_TO_DATE(dateTo, '%M %d,%Y'), '%Y-%m-%d');
If it's returning null then that means the extracted datetime value is illegal. You can try like below. See MySQL Documentation for more information.
SELECT STR_TO_DATE('October 10, 2015','%M %d,%Y');

Copy datetime to same table after conversion

Very new to SQL 2 weeks in. I have date in table have added extra columns in table, copied date field to each column. Alter the dates for 6 &18 weeks from original date. I need to convert the date to uk short date & copy result to table preferably replacing existing data in the columns.
COLUMNS are REF_DATE this is original date
Six_wk
Eighteen_wk
this is the SQL so far
ALTER TABLE [Frontsheets].[dbo].[j38_spfit_op_activity_noc]
ADD
APPOINTMENT_DATE nvarchar(11),
APPOINTMENT_TIME datetime,
Six_wk datetime,
Eighteen_wk datetime
go
update [Frontsheets].[dbo].[j38_spfit_op_activity_noc] set APPOINTMENT_DATE = APPT_DTTM, Six_wk = REF_DATE, Eighteen_wk = REF_DATE, APPOINTMENT_TIME = APPT_DTTM
go
UPDATE [Frontsheets].[dbo].[j38_spfit_op_activity_noc] SET Six_wk = DATEADD( "d" , 42, REF_DATE ), Eighteen_wk = DATEADD( "d" , 126, REF_DATE )
Go
Select REF_DATE = convert(nvarchar(10),REF_DATE, 103),
Six_wk = CONVERT(varchar(10), Six_wk, 103),
Eigheen_wk = CONVERT(varchar(10), Eighteen_wk, 103)
from [Frontsheets].[dbo].[j38_spfit_op_activity_noc]
Now I cant seem to save the converted data to my table?

SQL Work Week in format yyyyww

Does anyone know how i can filter a table by next weeks work week in the format 201337 which is yyyyww format.
The table would look like this
ww col1
201336 xx
201337 yy
201338 zz
i want to select 201336 xx. I normally use CURRENT_TIMESTAMP for timeDate:now but not sure how to get that to yyyyww
Try this:
DECLARE #nextWeek datetime
SET #nextWeek = DATEADD(day,7,GETDATE())
SELECT * FROM YourTable
WHERE ww =
CAST(DATEPART(year , #nextWeek) as char(4)) +
CAST(DATEPART(week , #nextWeek) as char(2))
See it working here.

SQL Server equivalent of MySQL DATE_FORMAT()

Hope we're having a good day and all set for Christmas.
Got a quick question. I'm converting a MySQL function into SQL Server, I've got most of the function converted except for one part which has the following:
#description = CONCAT(#description, date_format(#INLastUpdated, '%H:%i %d %b %Y'))
What I'm trying to do is to basically recreate the date_format function to format the date in the same way specified, but I'm not sure how to do it. from what I've seen in the MySQL documentation the format selected would give hour:minute day / short month name / year.
Anyone got any ideas?
You can try this:
DECLARE #description VARCHAR(1000) = 'test'
DECLARE #INLastUpdated DATETIME = '2012-12-21 14:32:22'
SET #description = #description + ' '
+ LEFT(CONVERT(VARCHAR(8), #INLastUpdated, 8), 5) + ' '
+ CONVERT(VARCHAR(20), #INLastUpdated, 106)
SELECT #description
But be careful as format 106 depends on local language settings. Read more on MSDN
The equivalent function is CONVERT. But you're basically out of luck. SQL Server does not allow to cherry-pick the date tokens. You need to browse the available full date built-in formats and choose one, or try to compose an output by string concatenation, as in:
CONVERT(VARCHAR, CURRENT_TIMESTAMP, 103) + ' ' + CONVERT(VARCHAR(8), CURRENT_TIMESTAMP, 114)
Which version of SQL Server? In SQL Server 2012 we now have the FORMAT function.
This, in MySQL
date_format(#INLastUpdated, '%H:%i %d %b %Y')
translates into something like this for SQL Server 2012 using the FORMAT function:
DECLARE #d DATETIME = GETDATE();
SELECT FORMAT(#d, 'HH:mm d MMM yyyy', 'en-US') AS 'DateTime Result';
BOL SQL Server 2012 > FORMAT (Transact-SQL)

adding current date as a parameter to a query in clover ETL

I have a query block for retrieving data from MSSQL Server. the query has some hardcoded date values which needs to be changed everyday to import the daily feed. I need to automate this execution. I am using cloverETL for executing the query right now.
Here is the query (its a query to retrieve sharepoint activity data)
use
DocAve_AuditDB;
DECLARE
#ParameterValue VARCHAR(100),
#SQL
VARCHAR(MAX)
SET
#SQL = STUFF((SELECT 'UNION ALL SELECT COL_ItemTypeName, COL_UserName, COL_MachineIp, COL_DocLocation, DATEADD(SECOND, COL_Occurred / 1000, ''19700101 00:00'') as Date_Occurred, COL_EventAction FROM '+ TABLE_NAME + ' WHERE DATEADD(SECOND, COL_Occurred / 1000, ''19700101 00:00'') BETWEEN '+ '''20120515'''+ 'AND' + '''20120516'''+ 'AND ' + 'COL_ItemTypeName='+ '''Document''' AS 'data()'
FROM INFORMATION_SCHEMA.TABLES
WHERE
TABLE_NAME LIKE '%2012_05%'
FOR
XML PATH('')),1,10,'')
EXEC
(#SQL)
In the above block I want the TABLE_NAME LIKE param i.e. %2012_05% to be a variable retrieved from the current data and also the date values in the between clause
BETWEEN '+ '''20120515'''+ 'AND' + '''20120516'''
to be todays date-1 and todays date
should create a small java program for handling this or it can be done directly in the query itself? if yes how?
Thanks in Advance
Use GETDATE() or CURRENT_TIMESTAMP to obtain the current date (and time).
Use CONVERT() with the 112 format specifier to convert the current timestamp to a string formatted as YYYYMMDD.
Use DATEADD() for calculations (like subtracting one day) on dates/times.
Use SUBSTRING() to subtract parts from the formatted date string to rearrange them to the %YYYY_MM% format.
Or you can use inline ctl notation in DBInputTable:
SELECT 'UNION ALL SELECT COL_ItemTypeName, COL_UserName, COL_MachineIp, COL_DocLocation, DATEADD(SECOND, COL_Occurred / 1000, ''19700101 00:00'') as Date_Occurred, COL_EventAction FROM `date2str(today(), "yyyy_MM")` WHERE DATEADD(SECOND, COL_Occurred / 1000, ''19700101 00:00'') BETWEEN '+ '''`date2str(today(), "yyyyMMdd")`'''+ 'AND' + '''`date2str(dateAdd(today(),1,day), "yyyyMMdd")`'''+ 'AND ' + 'COL_ItemTypeName='+ '''Document''' AS 'data()'